The program code works but after I add, subtract, multiply or divide two numbers the program exits automatically. I don't know how to keep it not to exit automatically, kindly see the code guys. thanks

#include <iostream.h>
#include <conio.h>
int mc(int x, int y) //Multiply two numbers
{
    cout <<"\n\n"<< x <<" times "<< y <<" equals ";
    return (x*y);
}
int ac(int a, int b) //Add two numbers
{
    cout <<"\n\n"<< a <<" plus "<< b <<" equals ";
    return (a+b);
}
int sc(int z, int c) //Subtract two numbers
{
    cout <<"\n\n"<< z <<" minus "<< c <<" equals ";
    return (z-c);
}
int dc(int o, int t) //Divide two numbers
{
    cout <<"\n\n"<< o <<" divided by "<< t <<" equals ";
    return (o/t);
}
void calc(char choice)
{
    int on,tw,thr;
        if (choice == '+') //This whole block checks what the user wants to calculate, and refers to the proper routine to calculate it.
    {
        cout<<"You selected "<<choice<<". Please enter two numbers,\nsepperated by spaces,";
        cout<<"that you want to add."<<endl;//print instructions for the user
        cin>>on;//Get the value of variable on
        cin>>tw;//Get the value of variable tw
        thr=ac(on,tw);//Get the sum of on and tw, and assign that value to thr
        cout<<thr<<"\n\n\n\aThanks for using my calculator!";//Print a thank you message
    }
    else if (choice =='-')
    {
        cout<<"You selected "<<choice<<". Please enter two numbers,\nsepperated by spaces, that you want to subtract."<<endl;
        cin>>on;
        cin>>tw;
        thr=sc(on,tw);
        cout<<thr<<"\n\n\n\aThanks for using my calculator!";
    }
    else if (choice =='*')
    {
        cout<<"You selected "<<choice<<". Please enter two numbers,\nsepperated by spaces, that you want to multiply."<<endl;
        cin>>on;
        cin>>tw;
        thr=mc(on,tw);
        cout<<thr<<"\n\n\n\aThanks for using my calculator!";
    }
    else if (choice =='/')
    {
        cout<<"You selected "<<choice<<". Please enter two numbers,\nsepperated by spaces, that you want to divide."<<endl;
        cin>>on;
        cin>>tw;
        thr=dc(on,tw);
        cout<<thr<<"\n\n\n\aThanks for using my calculator";
    }                                                                                                                                                                            else
    {
        cout<<"\nPlease reenter that value.\n\a";
        cin>>choice;
        calc(choice);
    }
}
void main()
{
    clrscr();
    int one, two, three,x;
    char choice;
    while (choice != 'e')
    {
        cout<<"\nWELCOME! To access my calculator, please follow the instructions carefully.";
        cout<<"\n\n\n";
        cout<<"\nPlease enter the correct number code: ";
        cin>>x;
        if (x!=123)
        {
        cout<<"\n";
        }
        else
        {
        cout<<"\nYou may now use the calculator!";
        break;}
        }
        cout<<"\nPlease enter +,-,*, or / and then two numbers,\nsepperated by spaces, that you wish to\nadd,subtract,multiply,or divide.\n\nType e and press enter to exit.";
        cin>>choice;
        if (choice != 'e')
        {
            calc(choice);
        }
}

the problem might be easier for you to see if you used a more consistant indenting style.

Here it is, reformatted (Courtesy of MSVC++'s auto-format tool):

int main() 
{
    clrscr();
    int one, two, three,x;
    char choice;
    while (choice != 'e')
    {
        cout<<"\nWELCOME! To access my calculator, please follow the instructions carefully.";
        cout<<"\n\n\n";
        cout<<"\nPlease enter the correct number code: ";
        cin>>x;
        if (x!=123)
        {
            cout<<"\n";
        }
        else
        {
            cout<<"\nYou may now use the calculator!";
            break;
        }
    }

    cout<<"\nPlease enter +,-,*, or / and then two numbers,\nsepperated by spaces, that you wish to\nadd,subtract,multiply,or divide.\n\nType e and press enter to exit.";
    cin>>choice;
    if (choice != 'e')
    {
        calc(choice);
    }
}

your if (choice != 'e') statement is not inside any kind of loop, so there is nothing to make it repeat.


I'd also like to point out this bit

char choice;
    while (choice != 'e')

your 'choice' variable is uninitialised when you compare it for inequality with 'e', which causes undefined behaviour (anything can happen...)

But how about the code for the calculator, can i just add it or i should make a new one? i forgot to say that our instructor ask us to make a simple calculator and aside from it, before you can use the calculator it should ask for a password first before you can use it. Kindly see it again guys. Immediate reply will be highly appreciated. thanks

At the end of your code, you have

cout<<"\nPlease enter +,-,*, or / and then two numbers,\nsepperated by spaces, that you wish to\nadd,subtract,multiply,or divide.\n\nType e and press enter to exit.";
        cin>>choice;
        if (choice != 'e')
        {
            calc(choice);
        }
}

This will only run the calculator once. Change the if to a while (with a couple other obvious changes) and you should be good.

Also, see this about your void main()

guys can you help me make a simplier code for calculator??
Guys! at last i've already finished my program, thanks for the help... here it is!!!
 
#include <iostream>
int main()
{
int x;
char choice;
while (choice !='e')
{
cout<<"\nWELCOME! To access my calculator, please follow the instructions carefully.";
cout<<"\n";
cout<<"\nPlease enter the correct number code: ";
cin>>x;
if (x!=123)
{
cout<<"\n";
}
else
{
cout<<"\nYou may now use the calculator!";
break;
}
}
do
  {
  int num1, num2;
  cout << "\nPlease enter a number: ";
  cin >> num1;
  cout << "Please enter another number: ";
  cin >> num2;
  cout << "Please enter a valid operator: ";
  cin >> choice;
  switch (choice)
    {
    case '+':
        cout << "The result is: " << (num1 + num2)<< endl;
        break;
    case '-':
      cout << "The result is: " << (num1 - num2) << endl;
      break;
    case '*':
      cout << "The result is: " << (num1 * num2) << endl;
      break;
    case '/':
      cout << "The result is: " << (num1 / num2) << endl;
      break;
    default: cout << "You have entered an invalid operator." << endl;
    }
  cout<< "do you want another calculation? (y or n)";
  cin >> choice;
  }
while (choice == 'y');
return 0;
}
This question has already been answered. Start a new discussion instead.