Problem calling a function

Hi, i'm having some problems with my program, i'm learning c++ alone with no help so i think it's normal make some mistakes, i want to do a c++ basic calculator and then upgrade as long as i learn new stuff. The problem is that when i enter the value the result is the last number the user input, can you help me?
Thanks and sorry my English,
This is just part of the code, if you need more i will send it,
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
int main ()
{
	int long a,b;
	int Option, Result;
	cout << "Basic Calc:\n\n";
	cout << "Operations:\n";
	cout << "1-Add\n" << "2-Subtraction\n" << "3-Division\n" << "4-Multiplication\n";
	cout << "Option: ";
	cin >> Option;
	for (int z=1;z<5;z++)
	{
		if (Option = z)
		{
			cout << "Enter 2 values (ex.:10 (Enter) 20 (Enter)): ";
			cin >> a;
			cin	>> b;
			Result = Option* (a,b);
			cout << Result;
			getchar();
			goto End;
		}
	}
End:
	getchar();
	return 0;
}
@line 12 you assign z to Option, you want to use ==

Don't use goto

Line 17 doesn't make any sense.

Instead of getchar(), do cin.get();
The reason it outputs the last input is because line 12 is doing assignment so Option will get the value 1 in the first iteration of the loop. 1 is considered true so the if statement will run. The comma operator returns the rightmost argument so line 17 is equivalent to Result = Option * b;. Option is 1 so Result will get the value of b and that is what is printed.

You could use break; instead of goto End;.
then i have to write the if statement to all the options?
Topic archived. No new replies allowed.