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);
}
}
nayr055 0 Newbie Poster
Bench 212 Posting Pro
nayr055 0 Newbie Poster
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
nayr055 0 Newbie Poster
nayr055 0 Newbie Poster
nayr055 0 Newbie Poster
Hamrick 150 Posting Whiz
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.