my else statement problem

Write your question here.
Hi all, im newbie that need your pro help, right now im trying to do a simple calculator that do simple math like, addition,substraction,multiplication and division, the problem is i when my program get the answer, it also showing the else statement that supposedly for user that put wrong input when my program asking for math symbol, so can anybody help me, how to correct this coding? your help much be appreciate, this code only for school process, so i dont need the advance coding, thanks for your help in advance~ :)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  #include <iostream>
#include <string>
#include <math.h>
#include <Windows.h>

using namespace std;

void main(){
	HANDLE hConsole;
	hConsole = GetStdHandle (STD_OUTPUT_HANDLE);

	int input1, input2;
	char math = ('+','-','/','*');
SetConsoleTextAttribute (hConsole, 6);

	cout<<"                             Simple Calculator 3.5v.\n";
	cout<<"                               Develop by Mercier\n\n";

SetConsoleTextAttribute (hConsole, 7);
	cout<<"What type of mathematic operation do you like to use? (+,-,*,/)\n\n";
	cin>>math;cout<<endl;
	
	
	if (math == '+'){

SetConsoleTextAttribute (hConsole, 2);cout <<"                        You have choosen addition operation.\n\n";SetConsoleTextAttribute (hConsole, 7);
		cout <<"What is your first number(s)?\n";
		cin>>input1;cout<<"\n";
		cout<<"What is your second number(s)?\n\n";
		cin>>input2;cout<<"\n";
		cout<<"Your total addition for both numbers is "<<input1 + input2<<endl<<endl;
	}

		if (math == '-'){

SetConsoleTextAttribute (hConsole, 4);cout <<"                        You have choosen substraction operation.\n\n";SetConsoleTextAttribute (hConsole, 7);
		cout <<"What is your first number(s)?\n\n";
		cin>>input1;cout<<"\n";
		cout<<"What is your second number(s)?\n";
		cin>>input2;cout<<"\n";
		cout<<"Your total substraction for both numbers is "<<input1 - input2<<endl<<endl;
	}

			if (math == '*'){

SetConsoleTextAttribute (hConsole, 6);		cout <<"                        You have choose multiplication operation\n\n";SetConsoleTextAttribute (hConsole, 7);
		cout <<"What is your first number(s)?\n";
		cin>>input1;cout<<"\n";
		cout<<"What is your second number(s)?\n";
		cin>>input2;cout<<"\n";
		cout<<"Your total multiplication for both numbers is "<<input1 * input2<<endl<<endl;
	}
	if (math == '/'){

SetConsoleTextAttribute (hConsole, 3);		cout <<"                        You have choosen devision operation\n\n";SetConsoleTextAttribute (hConsole, 7);
		cout <<"What is your first number(s)?\n";
		cin>>input1;cout<<"\n";
		cout<<"What is your second number(s)?\n";
		cin>>input2;cout<<"\n";
		cout<<"Your total devision for both numbers is "<<input1 / input2<<endl<<endl;
	}
	
	else if (math == 'x');{
		cout<<"You can only put these four math symbol:\n";
		cout<<"(+) for addition.\n";
		cout<<"(-) for substraction.\n";
		cout<<"(*) for multiplication.\n";
		cout<<"(/) for devision."<<endl<<endl;
	}

	//unfinish calculator kalau user type lain, error keluar "You not put right input".
	system ("pause");
	
}
Line 63, you have a ; after the if statement. That terminates the if statement.
Lines 64-69 are not considered part of the if statement and will be executed unconditionally.
Last edited on
owh thanks, i try it out :)

edit:

Thanks AbstractionAnon!! Its really work~owh my, thats was my mistake, just very simple mistake can make my program running crazy, i need to be more careful, thanks again!
Last edited on
but how my program want to cout the "You can only put these four math symbol:\n" if user put other than the 4 symbol?
i suggest you use a switch and you put in the default the "You can only put these 4 math symbols"
it should look something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
switch(math)
{
   case '+':
   {
           code for +
   }
   case '-':
   {
          code for -
   }
   ......
   default:
   {
        cout << "You can only put these four math symobls:\n";
   }
}
thanks for suggestion :)
any other suggestions would much appreacite~ to enhance my skill~
Topic archived. No new replies allowed.