Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
#include <iostream>
#include <stdio.h>
int main(void)
{
printf("Hello, CS 101! \n");
int age{
printf("How old are you? \n")
};
printf("OK, you are",scanf(%i,&age),"years old.");
return 0;
}
Our assignment needs to use scanf and printf to find out how old the user is, then to print a statement stating how old they are.
Our professor didn't go over syntax and gave us the code in class, but I was too far from the board to see. This is what I got on my own, but I have not found good coding resources for c++.
I get the error scope not defined, can someone help me. Also, if anyone has good resources for learning from scratch that would be great. This is my first time coding, thanks.
I fixed your code. Hope it is what you are after:
#include <iostream>
#include <stdio.h>
int main(void)
{
printf("Hello, CS 101! \n");
// declare age as int
int age;
// ask for age
printf("How old are you? \n");
// get user input into age
scanf("%d", &age);
// display entered age
printf("OK, you are %d years old.\n", age);
return 0;
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
I'm working on this function for one of my classes and my pass count works just fine, however, my fail count ALWAYS prints out 12. I've been reading my code top to bottom and just can't seem to find the problem.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
string passAndFailCount(string grades){
int pass_count;
int fail_count;
istringstream myStream(grades);
string grade;
while(myStream>>grade){
int i_grade=stoi(grade);
if (i_grade>=55){
++pass_count;
}
else{
++fail_count;
}
}
cout<<"Pass: "<<pass_count<<endl<<"Fail: "<<fail_count<<endl;
}
int main()
{
string grades;
getline(cin, grades);
passAndFailCount(grades);
}
Your problem are uninitialized variables.
int pass_count = 0;
int fail_count = 0;
and you're set.
For an explanation. Non-global variables (which automatically get initialized to 'default' (0) as per the standard), automatic and dynamic variables (like the one you are using), assume the value of 'whatever was in memory at the time of allocating the variable'.
Memory is never empty, there's always 'something' written in there.
So the memory of your fail_count variable just 'happened to be' 12 during allocation, which is why you start with that value. This value can be anything within scope.
By explicitly assigning a variable, you 'initialize' the variable, putting it into a defined state (0) and your program should start working as expected.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
this program is simple :
1)take an input string .
2) convert it to long .
3) print convert result.
expected an output,but nothing found.
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
string ch;
scanf("%s",ch);
long l=stol(ch);
printf("%l",l);
return 0;
}
Here's how it's done with C++ I/O. There's very little reason for using C I/O in a C++ program.
#include <iostream>
#include <string>
int main()
{
std::string input;
std::cin >> input; // take an input string
long lval = stol(str); // convert to long
std::cout << lval << '\n' // print the result
}
Now this stuff would be covered in the first chapter of any C++ book. A good book will greatly increase how quickly and how well you learn C++.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
hey guys so i am writing a program to sort a list of words alphabetically based upon my limited programming knowledge, i know there are high level functions that make it easier but i am trying to do it the hard way, anyway, i decided i would do it by adding together the alphabetical values of each character in the string and then putting them into a sorted string array based upon least to greatest value and string length, as part of this i am writing a function that will add the letters values together via a loop... here is my source
#include <iostream>
#include <string>
using namespace std;
int ABReturn_value(string a);
int main(int argc,char *argv[]){
cout<<ABReturn_value("a")<<endl;
return 0;
}
int ABReturn_value(string a){
//declare an internal character array containing the alphabet
char alphabet[2][26] = {{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'},{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}};
//variable to hold the acrued value
int value=0;
//look through the provided string's characters and acrue the value based on which address in the refernece array the letter corresponds to
for(unsigned int x = 0;x<=(a.size())-1;x++)
for(int y=0;y<=26-1;y++){
if(a[x]==alphabet[1][y]||a[x]==alphabet[2][y]){
value+=y;
}
}
return value;
}
the problem im running into is based on this test input of the character a i am expecting the function to return me a value of 1 but it is instead returning 40,ive been staring at it and cant seem to figure out what i did wrong so maybe theres something im missing? thanks in advance!
#immibis has answered this, could you tag it as answered please?
alphabet[1] should be alphabet[0] and alphabet[2] should be alphabet[1] – immibis
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm newbie in C++ and I'm trying to evaluate a square root. I've written the following:
#include <math.h>
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
double a;
a=sqrt(2.0);
printf("Square root from 2 is %d\n",a);
return 0;
}
But output is Square root from 2 is 1719614413. I really don't understood this. Please explain me.
You are using C (which mostly compiles as C++ as well), not C++ and you made a mistake doing so. You can either learn C or use C++, where it's harder to make those mistakes:
#include <cmath>
#include <iostream>
int main()
{
double a = std::sqrt(2.0);
std::cout << "Square root from 2 is " << a << std::endl;
return 0;
}
Additional explanation:
printf("Square root from 2 is %d\n",a);
This prints a string and expects an integer (%d) to be passed. You passed a double, wich comes out as garbage. You can use (%lf) for doubles.
If you still want to stick to our example, please consider using %f rather than %d:
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
This is a function to check a number for a power of two. Although the compiler does not print anything, it's also not showing any errors. Please let me know if the logic is correct or not.
Here is my code:
#include <bits/stdc++.h>
using namespace std;
bool isPowerofTwo(long long n)
{
// Your code here
for (int i = 1; i <=n; i<<1)
{
if(i==n){
return true;
}
}
return false;
}
int main()
{
cout << isPowerofTwo(2);
return 0;
}
The expression i<<1 in the third statement (the "iteration expression") of your for loop doesn't actually do anything (that is, it doesn't modify the value of i). You need, instead, i<<=1 (or, in 'long form', i = i << 1).
Also, please read: Why should I not #include <bits/stdc++.h>?. In your code, the only standard header you need is #include <iostream>. Another good post to read is: Why is "using namespace std;" considered bad practice?.