Unknown Integer Problem

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?
Unlike Python, you need to use braces around your if if it encloses more than one line. C++ isn't based on indentation.
Last edited on
braces around what? (start ==1)?
#include <iostream>
using namespace std;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.

Anyway, have fun with your programming!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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;
}


Indention is non existant in c++... so...

1
2
3
4
5
6
something       //level 1
   something    //level 1
   if (bool)    //level 1
      something //level 1
   something    //level 1
something       //level 1 


1
2
3
4
5
6
7
8
9
10
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)

so your final code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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;
}
Last edited on
Thanks. I was going at it all wrong.
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?
after i copied and pasted this solution in Geany editor, added 1 and 1 result i got is 2543606

Here i made calculator for you:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
using namespace std;

int mult(int x, int y);

int main()
{

 int x;
 int y;
int select;
  cout<<"What you want to do?\n 1. Plus\n 2. Minus\n 3.Multiply\n 4.Divide ";
 cin>>select>>x>>y;

if(select == 1)
{
   cout<<"Result : "<<x+y;
}

if(select == 2)
{
 cout<<"Add two numbers to get result: "<<x-y;
}

if(select == 3)
{
 cout<<"Result: "<<x*y;
}

if(select == 4)
{
 cout<<"Result: "<<x/y;
}

cin.get();


  
 
}

Wouldn't it be easier to use the case statement thing rather than a bunch of Ifs?
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;
}

else if(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
Last edited on
Topic archived. No new replies allowed.