### Calculator v. 1.6

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495`` ``````#include #include int main () { system("color 3f"); int choice1; int choice2; int choice3; int result; for (;;) { do { cout << "\n"; cout << "-----------------------------\n"; cout << "| Calculator v. 1.6 |\n"; cout << "| 1 for plus |\n"; cout << "| 2 for minus |\n"; cout << "| 3 for divide |\n"; cout << "| 4 for multiplication |\n"; cout << "| 5 to quit |\n"; cout << "-----------------------------\n"; cin >> choice1; } while (choice1 == '1' &&choice1 == '2' &&choice1 == '3' &&choice1 == '4' &&choice1 != '5'); if (choice1 == '5') break; switch (choice1) { case 1: cout << "Plus. Enter two numbers: \t"; cin >> choice2; cin >> choice3; result = choice2 + choice3; cout << "result: \t" << result << "\n"; break; case 2: cout << "Minus. Enter two numbers: \t"; cin >> choice2; cin >> choice3; result = choice2 - choice3; cout << "result: \t" << result << "\n"; break; case 3: cout << "Divide. Enter two numbers: \t"; cin >> choice2; cin >> choice3; result = choice2 / choice3; cout << "result: \t" << result << "\n"; break; case 4: cout << "Multiplication. Enter two numbers: \t"; cin >> choice2; cin >> choice3; result = choice2 * choice3; cout << "result: \t" << result << "\n"; break; case 5: cout << "Quiting Calculator v. 1.6 . . ."; return 0; default: cout << "That is not a valid option.\n"; } } return 0; }``````

It took me 1 hour to program, what do you guys think? Any bug?
Consider the while statement. Do some calculation, then you will realise you should change AND && symbols to OR...
I did consider the `while` statment, but I dont know where exactly
to put it. [second sentence] I dont understand what do you mean by that.
The do-while loop is not functioning.
Try commenting-out (using //) lines 11 and 24. The behaviour of the program is unchanged.

The if statement at line 25 is non-functioning. Again, comment it out and the behaviour of the program is unchanged.

The above problems should be corrected, or that code deleted.

A general comment, there is a lot of repeated code within the switch-case. You might consider ways of having the identical code appear just once. In this case it isn't too important, but it is an interesting exercise.
Sorry, my fault. I skimmed too fast. :)
But, please remove the construct do...while. In my opinion I see it's redundant...

And what's wrong?

EDIT : I agree with Chervil. Your program needs some improvements.

Last edited on
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849`` ``````#include // not int main() { const char PLUS = '+', MINUS = '-', MULTIPLY = '*', DIVIDE = '/', QUIT = 'q' ; while( true ) { char choice ; do { std::cout << "\n" "-----------------------------\n" "| Calculator v. 1.6 |\n" "| + for plus |\n" "| - for minus |\n" "| / for divide |\n" "| * for multiply |\n" "| q to quit |\n" "-----------------------------\n" ; } while( std::cout << "choice? " && std::cin >> choice && choice != PLUS && choice != MINUS && choice != DIVIDE && choice != MULTIPLY && choice != QUIT ) ; if( choice == QUIT ) break ; std::cout << '\'' << choice << "' enter two numbers: " ; int first, second ; std::cin >> first >> second ; std::cout << "result: " << first << ' ' << choice << ' ' << second << " == " ; if( choice == DIVIDE ) { if( second == 0 ) std::cout << "can't divide by zero\n" ; else std::cout << double(first) / second << '\n' ; } else { int result = 0 ; switch( choice ) { case PLUS : result = first + second ; break ; case MINUS : result = first - second ; break ; case MULTIPLY : result = first * second ; break ; } std::cout << result << '\n' ; } } }``````
ok im not that good at C++
Don't take it to heart, it takes time. And I think we all find it useful to see how another programmer tackles the same task.
 ``12345678910111213141516171819202122232425`` `````` while( true ) { char choice ; std::cout << "\n" "-----------------------------\n" "| Calculator v. 1.6 |\n" "| + for plus |\n" "| - for minus |\n" "| / for divide |\n" "| * for multiply |\n" "| q to quit |\n" "-----------------------------\n" ; std::cin >> choice; if (choice == QUIT) break; else if (choice != PLUS && choice != MINUS && choice != DIVIDE && choice != MULTIPLY) { std::cout << "invalid option" << endl; continue; //takes you back to the start of the while loop } ``````

Here's another way you could go about it.
Topic archived. No new replies allowed.