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);
}

What's wrong with the way you did it (other than it should be:

while(oper != 'X');)?

I think the flow of the program could be improved, but terminating without using exit() looks okay. You could use a bool variable if you want, but the concept remains basicly the same.

Well, as everything is in your main function, you could add the following statement at the place where you otherwise would have used the exit() function: return 0; .

BTW, in your switch:

case 'X':
    op="exit";
    break;
default:
    return(0);
    break;

Actually the following block of code is not needed anymore, because your program will also exit, by executing the instructions in the block for default: .

// You can leave this block out of your code
case 'X':
    op="exit";
    break;

Also, I don't see the point of first having a switch, and then having an if - else-if ladder in your program, why not just strip out all those ifs, and directly perform the right operations inside the switch statement?

You'll have to re-organize your code a bit then (for example you'll have to get the number from the user, directly before/after you've got the operator from the user), but after re-organizing, your code will be shorter and much more polished.

Why not give it a try?

There is seldom, if ever, the need to use exit( ) other than for some abnormal ending. If your program runs correctly and completely, the return(0) at the end of main( ) will cause the program to gracefully end.

You have the char "oper" and the string "op" which both connote the same things - why not just use the char in both switches?

Why not put lines 46-72 in an if block - if( oper != 'X' ) so that input for a number only occurs when relevant. Otherwise, skip that block and exit the loop.

I'm not fond of return statements inside the switches/if blocks/loops - let your condition cause the loop to end, then return/exit after that. It's not essential in this particular problem, but as you go on you may have programs where some cleanup/post-processing is needed before exiting.

Thanks for the help. The "switch..case" was shoehorned in to meet the requirements of the assignment. I always appreciate getting input and people finding my mistakes so I can learn what to do next time.

commented: That's the spirit :) +14
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.