i was working on a simple calculator and here's the problem I'm having:

i want the whole program to loop over and over until the user decides to stop, so i put a while in there and the loop works great, but when the user is done inputting the numbers there is no pause between it showing the result and the loop starting back up again, so i put a "press any key to continue" command in there, well that's not working. so i was wondering if anyone could tell me how I'm doing it wrong and how to do it right.

Here's the code:

#include <iostream>
#include <cmath>

using namespace std;

int choice;
int result;
int num1;
int num2;

void add();
void sub();
void multi();
void divi();

int main() {
    
    do{
    system ("cls");
    cout << "Do you want to: " << endl;
    cout << "1. Add." << endl;
    cout << "2. Subtract." << endl;
    cout << "3, Multiply." << endl;
    cout << "4. Divide." << endl;
    cout << "5. Exit." << endl;
    cin >> choice;
    
    if (choice == 1){
               add();
               }
    else if (choice == 2){
               sub();
               }
    else if (choice == 3){
               multi();
               }
    else if (choice == 4){
               divi();
               }
    else  {
         cout << "That is not a choice." << endl;
         }
         }
    while (choice != 5);
         

cin.get();
    }
    
void add(){
     system ("cls");
     cout << "Enter the first number. " << endl;
     cin >> num1;
     cout << "Enter the second number. " << endl;
     cin >> num2;
     result = num1 + num2;
     cout << "The total of " << num1 << " Plus " << num2 << " is " << result << endl << endl;
     cout << "Press any key to continue." << endl;
     cin.get();
     
     }
     
void sub(){
     system ("cls");
     cout << "Enter the first number. " << endl;
     cin >> num1;
     cout << "Enter the second number. " << endl;
     cin >> num2;
     result = num1 - num2;
     cout << "The total of " << num1 << " Minus " << num2 << " is " << result << endl << endl;
     cout << "Press any key to continue." << endl;
     cin.get();
     }
     
void multi(){
     system ("cls");
     cout << "Enter the first number. " << endl;
     cin >> num1;
     cout << "Enter the second number. " << endl;
     cin >> num2;
     result = num1 * num2;
     cout << "The total of " << num1 << " Multiplied by " << num2 << " is " << result << endl << endl;
     cout << "Press any key to continue." << endl;
     cin.get();
     }
     
void divi(){
     system ("cls");
     cout << "Enter the first number. " << endl;
     cin >> num1;
     cout << "Enter the second number. " << endl;
     cin >> num2;
     result = num1 / num2;
     cout << "The total of " << num1 << " Divided By " << num2 << " is " << result << endl << endl;
     cout << "Press any key to continue." << endl;
     cin.get();
     }
short choose;
int number;
int n;







 int main()
 {

	
	cout<<"Write a number"<<endl;
	cin>>number;
	cout << "Do you want to: " << endl;
	cout << "1. Add." << endl;
	cout << "2. Subtract." << endl;
	cout << "3, Multiply." << endl;
	cout << "4. Divide." << endl;
	cout << "5. Exit." << endl;
	cout<<number<<endl;
	cin>>choose;

	switch(choose){
	case 1:cin>>n; cout<<number+n<<endl; break;
	case 2:cin>>n; cout<<number-n<<endl;break;
	case 3:cin>>n; cout<<number*n<<endl;break;
	case 4:cin>>n; cout<<number/n<<endl;break;
	default: cout<< "write the correct letter"<<endl;
	}

	system("pause");
	return 1;
 }

what you need to do is change you if statement to switch statement and your while as follow:

bool done = false


while(!done){ put the code inside........}

cin >> choice;
switch(choice){
   case 1:
      ADD();
      break;
   case 2:
      SUB();
      break;
   case 3:
      MULT();
      break;
   case 4:
      DIV();
      break;
   case 5:
     done = true
     break;
     }

Edited 6 Years Ago by Nick Evan: Added closing code-tag

i get the switch statement part but what does the : bool done mean.
and when i run it i still don't have the pause in between the answer and the new calculation.

hi
add "cin.ignore()" after every cin.get() accurance to ignore the line thus stopping to waite for the enter keypress

You should have followed alexchen's code.
Try to keep the code as simple as possible, using fewer variables and cout.

#include <iostream>
using namespace std ;

void main()
 {
	int response;
	int choose;
	int num1, num2 ;
	do
	{
	system ("cls") ;
	cout << "Enter Two Numbers: " << endl ;
	cin >> num1 >> num2 ;
	cout << "Do you want to: " << endl;
	cout << "1. Add." << endl;
	cout << "2. Subtract." << endl;
	cout << "3, Multiply." << endl;
	cout << "4. Divide." << endl;
	cout << "Specify Operation: " ;
	cin >> choose;	

	switch(choose)
	{
	case 1: cout << num1 + num2 << endl ; break ;
	case 2: cout << num1 - num2 << endl ; break ;
	case 3: cout << num1 * num2 << endl ; break ;
	case 4: cout << num1 / num2 << endl ; break ;
	default: cout << "Invalid Operation" << endl ;
	}
	cout << "Press 1 To Continue, 0 to Exit: " ;
	cin >> response ;
	}
	while ( response ) ;
 
	system ("pause") ;
}

I think we are done here, better close the thread.

This article has been dead for over six months. Start a new discussion instead.