Ok, I am having two issues with this program, and since I am new to the forum, yes, I am a beginner so I am sure the error is so common it makes you want to cry seeing it again, haha. Anyway, the program is supposed to keep working with the numbers and math operators you input. The math is fine (after some simple mistakes were corrected) but even when you type in a valid operator and number, the ELSE statement is still run.

Likewise, the loop does not terminate on X or x as I thought I had designed it to.

double tot, num;
	   char oper;
	   tot = 0;
	   cout << "Current total is " << tot <<endl;
	   do 
		   {
			   cout << "Enter and operation: + - * / (or enter X to exit): "; cin >> oper;
			   if (oper == '+')
			   {
				   cout << "Enter a number: "; cin >> num;
				   tot = tot + num;
				   cout << "Current total is " << tot << endl;
			   }
			   if (oper == '-')
			   {
				   cout << "Enter a number: "; cin >> num;
				   tot = tot - num;
				   cout << "Current total is " << tot << endl;
			   }
			   if (oper == '*')
			   {
				   cout << "Enter a number: "; cin >> num;
				   tot = tot * num;
				   cout << "Current total is " << tot << endl;
			   }
			   if (oper == '/')
			   {
				   cout << "Enter a number: "; cin >> num;
				   tot = tot / num;
				   cout << "Current total is " << tot << endl;
			   }
			   else if (oper != 'X' || oper != 'x' || oper == '/' || oper == '*' || oper == '-' || oper == '+') cout << "Enter a valid number." << endl;
		   }
		   while (oper != 'X' || oper != 'x');

Recommended Answers

All 4 Replies

Hey Jae5086, you must understand that no one is born with a detailed knowledge of C++ and hence everyone was a beginner and we might share a laugh but will not make fun of you :)

Secondly, the problem you are facing is due to the fact that your if statements in line 8, 14, 20, 26 are all unique, and the else statement is working for only the if statement at line 26. you may modify the code as this:

double tot, num;
	   char oper;
	   tot = 0;
	   cout << "Current total is " << tot <<endl;
	   do 
		   {
			   cout << "Enter and operation: + - * / (or enter X to exit): "; cin >> oper;
			   if (oper == '+')
			   {
				   cout << "Enter a number: "; cin >> num;
				   tot = tot + num;
				   cout << "Current total is " << tot << endl;
			   }
			   else if (oper == '-')
			   {
				   cout << "Enter a number: "; cin >> num;
				   tot = tot - num;
				   cout << "Current total is " << tot << endl;
			   }
			   else if (oper == '*')
			   {
				   cout << "Enter a number: "; cin >> num;
				   tot = tot * num;
				   cout << "Current total is " << tot << endl;
			   }
			   else if (oper == '/')
			   {
				   cout << "Enter a number: "; cin >> num;
				   tot = tot / num;
				   cout << "Current total is " << tot << endl;
			   }
			   else if (oper != 'X' || oper != 'x') 
                          /*you just need to check this as everything else has been checked already*/
                                   cout << "Enter a valid number." << endl;
		   }
		   while (oper != 'X' || oper != 'x');

Also, if you are familiar with the switch operator, I would highly recommend using that here instead of multiple if else statements.

double tot, num;
	   char oper;
	   tot = 0;
	   cout << "Current total is " << tot <<endl;
	   do 
		   {
			   cout << "Enter and operation: + - * / (or enter X to exit): "; 
			   cin >> oper;
			   
			   switch(oper){
			   case '+':
				cout << "Enter a number: "; cin >> num;
				tot = tot + num;
				cout << "Current total is " << tot << endl;
			   break;
			   case '-':
				cout << "Enter a number: "; cin >> num;
				tot = tot - num;
				cout << "Current total is " << tot << endl;
			   break;
			   case '*':
			    cout << "Enter a number: "; cin >> num;
				tot = tot * num;
				cout << "Current total is " << tot << endl;
			   break;
			   case '/':
				cout << "Enter a number: "; cin >> num;
				tot = tot / num;
				cout << "Current total is " << tot << endl;
			   break;
			   default:
			   cout << "Enter a valid number." << endl;
			   break;
		   }
		   while (oper != 'X' || oper != 'x');

Please mark the thread as solved if this helped.

It did help, though I am proud to say I actually had just solved it on my own and was posting an update when I saw the reply. As for SWITCH, that is our next lesson :)

I do still need help with the WHILE statement, as the loop doesn't terminate on X or x as it should.

yeah, just saw that. just change it to:

while (oper != 'X' && oper != 'x');

ARG! lol

Thanks for the help, much appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.