-
I have a problem...
I was going to make a calculator, and whaddayaknow, it didn't work. Whatever I do, I can't seem to fix it.
Here comes the source code...
Code:
[tag]#include <iostream>
using namespace std;
int main()
{
int a;
cout<<"Please enter a number up to a maximum of 10 billions: ";
cin>>a;
int b;
cout<<"Please enter another number up to a maximum of 10 billions: ";
cin>>b;
char c;
cout<<"What would you like to do with the numbers? ";
cin>>c;
if c == +
int d = a + b;
if c == -
int d = a - b;
if c == *
int d = a * b;
cout<<"The answer is... "<< d <<"\n";
cin>>d;
}[/tag]
And here are the problems...
Code:
[tag] error: expected '(' before "c"
error: expected '(' before "c"
error: expected '(' before "c"
error: 'd' was not declared in this scope[/tag]
So, um... Help?
PS. I have only done the first 2 tutorials, so I am aware that I am a newbie, and that you probably think this is the easiest things in the world.
-
I laugh at your problem! j/k
Actually, the problem is that fail to understand if statement syntax. Basic form is:
if (expr)
Also beware that any variable declared inside the if statements - ie
Code:
if (expr)
{
int x;
}
...is not visible outside the if statement, so any variables you want to assign inside the if statement and use outside, you must define outside the if.
Also, when comparing to a character, it must enclosed in quotes, ie '*'.
-
So I should do this? I mentioned i was a newbie, didn't I?
Code:
#include <iostream>
using namespace std;
int main()
{
int a;
cout<<"Please enter a number up to a maximum of 10 billions: ";
cin>>a;
int b;
cout<<"Please enter another number up to a maximum of 10 billions: ";
cin>>b;
char c;
cout<<"What would you like to do with the numbers? ";
cin>>c;
if (c == +)
{
int d = a + b;
}
if (c == -)
{
int d = a - b;
}
if (c == *)
{
int d = a * b;
}
cout<<"The answer is... "<< 'd' <<"\n";
cin>>d;
}
-
Code:
int d;
if (c == '+')
d = a + b;
-
Ok, but it still says that 'd' is not declared...
-
I don't know what you did... this works:
Code:
#include <iostream>
using namespace std;
int main()
{
int a;
cout<<"Please enter a number up to a maximum of 10 billions: ";
cin>>a;
int b;
cout<<"Please enter another number up to a maximum of 10 billions: ";
cin>>b;
char c;
cout<<"What would you like to do with the numbers? ";
cin>>c;
int d;
if (c == '+')
{
d = a + b;
}
if (c == '-')
{
d = a - b;
}
if (c == '*')
{
d = a * b;
}
cout<<"The answer is... "<< 'd' <<"\n";
cin>>d;
}
-
Oh, thank you very much, my hero!
I wonder if it would insult you if i added you as a friend if I have future problems?
-
-
And then there is one last problem. Look at this:
Code:
Please enter a number up to a maximum of 10 billions: 10
Please enter another number up to a maximum of 10 billions: 10
What would you like to do with the numbers? +
The answer is... d
And then the .exe ends... Maybe if i change the icons with the words?
Sorry if I am annoying you...
-
Ah,
cout<<"The answer is... "<< 'd' <<"\n";
should be
cout<<"The answer is... "<< d <<"\n";
When referring to variables, you use their names. When referring to characters, you put them in quotes. When referring to strings, you use double quotes.
-
Arcantos247: the 'd' has to be outside the {}'s because a variable doesn't exist after the curly-bracket that closes the scope it is in. E.g.
Code:
{
int d;
// d exists here
{
int e;
// e exists here, and so does d
}
// e does not exist here, but d still does
{
int f;
// f exists here, and so does d
}
// neither e nor f exist here, but d still does
}
// all of the above variables no longer exist here
-
Thanks for the help, both of you!
-
Structure
Stick with it! you will soon be rewarded with satisfaction of seeing your ideas realised, and your skills will grow quickly, i would recommend declaring all the variables you plan to use at top of main for now, to avoid the confusion explained earlier
-
I recommend that you actually learn about the concept of scopes. It will help you along the way. And it is a very important concept.