I very recently started coding with c++(earlier today). I have coded a good bit with Python, but felt that c++ would open more doors for me. I spent a few hours learning the basics and felt i was ready to make a very basic addition calculator. It all seems to be good except for a myster problem i have. The code is:
#include <iostream>
using namespace std;
int main ()
{
int start;
cout << "Do you want to: 1)Add"<<endl;
cin >> start;
if (start ==1)
int x;
int y;
int z;
cout << "Add this number:"<<endl;
cin >> x;
cout << "To this number:"<<endl;
cin >> y;
z += (x + y);
cout << "Which equals:"<<z<<endl;
return 0;
}
. My problem is the "cin >> x;". It says that "x" isnt defined. Can anyone help me?
int main ()
{
int start;
cout << "Do you want to: 1)Add"<<endl;
cin >> start;
if (start ==1)
{
int x;
int y;
cout << "Add this number:"<<endl;
cin >> x;
cout << "To this number:"<<endl;
cin >> y;
cout << "Which equals:"<<(x + y)<<endl;
}
int endgame;
cin>>endgame;
return 0;
}
That should work... You needed to have curly brackets around your if statement.
I also took out int z and made it so it displayed (x+y). For some reason int z kept on being 42 rather than the right answer... Not sure what's wrong but (x+y) fixes it.
The int endgame and cin>>endgame is in there so the program doesn't immediately shut down after you run it.
#include <iostream>
usingnamespace std;
int main ()
{
int start;
cout << "Do you want to: 1)Add"<<endl;
cin >> start;
if (start ==1)
{
int x;
int y;
int z;
cout << "Add this number:"<<endl;
cin >> x;
cout << "To this number:"<<endl;
cin >> y;
z += (x + y);
cout << "Which equals:"<<z<<endl;
}
return 0;
}
something //level 1
if (bool) //level 1
{ //start of level 2
something //level 2
if (bool) //level 2
{ //start of level 3
something //level 3
} //end of level 3
} //end of level 2
something //level 1
notice the braces are what define a level : )
and one other side note... the line that says... z += (x + y)
is not going to work because since z is not initialized z='' and that is not even zero...
so the proper code is: z = (x + y)
#include <iostream>
usingnamespace std;
int main ()
{
int start;
cout << "Do you want to: 1)Add"<<endl;
cin >> start;
if (start ==1)
{
int x;
int y;
int z;
cout << "Add this number:"<<endl;
cin >> x;
cout << "To this number:"<<endl;
cin >> y;
z = (x + y);
cout << "Which equals:"<<z<<endl;
}
return 0;
}
Not only do you not NEED the z variable... you never initialized it in the first place... how can you add a value to something that doesn't have a defined starting value?
Happykiller, it would be more efficient to write if else statements instead of all if statements, that way unnecessary statements won't be evaluated after a correct match is found. For example..
1 2 3 4 5 6 7 8
if(select == 1)
{
cout<<"Result : "<<x+y;
}
if(select == 2)
{
cout<<"Add two numbers to get result: "<<x-y;
}
could be written...
1 2 3 4 5 6 7 8 9 10
if(select == 1)
{
cout<<"Result : "<<x+y;
}
elseif(select == 2)
{
cout<<"Add two numbers to get result: "<<x-y;
}
and so on. Now if selection==1, the rest of the if statements won't even be evaluated.
Wouldn't it be easier to use the case statement thing rather than a bunch of Ifs?
- The switch statement would be more efficient than a bunch of stand-alone if statements, so I would choose to do that instead