Working on another c++ assignment and I need to make an input of "X" function as an end to the program without using an exit() command. Just want to know the easiest way to make that happen. Thanks for the help.
#include <iostream>
#include <string>
using namespace std;
int main()
{
double total=0, num=0;
char oper, X=0;
string op;
do
{
cout<<"Enter an operation: + - * / (or enter X to exit):"<<endl;
cin>>oper;
cin.ignore();
switch (oper)
{
case '+':
op="add";
break;
case '-':
op="sub";
break;
case '*':
op="mult";
break;
case '/':
op="div";
break;
case 'X':
op="exit";
break;
default:
return(0);
break;
}
cout<<"Enter a number: "<<endl;
cin>>num;
cin.ignore();
if (op=="add")
{
total=total + num;
cout<<"Current total is "<<total<<endl;
}
else if (op=="sub")
{
total=total - num;
cout<<"Current total is "<<total<<endl;
}
else if (op=="mult")
{
total=total * num;
cout<<"Current total is "<<total<<endl;
}
else if (op=="div")
{
if (num !=0)
{total=total / num;
cout<<"Current total is "<<total<<endl;}
else
{cout<<"Can not divide by zero!"<<endl;}
}
else if (op=="X")
{return(0);}
else
{return(0);}
}
while (oper != X);
return(0);
}