Can anybody tell what I need to do to make this program restart after it finishes without having to exit? Some kind of loop or something...???

#include <iostream>

using namespace std;

int main ()
{
	double operand1, operand2;
	double sum, difference, quotient;
	char choice;


	cout << "\n\nBASIC CALCULATOR\n"
		 << "a. Addition\n"
		 << "b. Subtraction\n"
		 << "c. Division\n\n"
		 << "d. Quit\n\n";
	
	//Get the choice
	cout << "Insert Option:";
	cin  >> choice;

	
	
	//Switch to deal with four cases
	switch (choice)
	{
		case 'a':	cout << "\n\nInput Operands: " << "\n\n";
					cin  >> operand1 >> operand2;
					sum = operand1 + operand2;
					cout << "The sum is " << sum << "\n\n";
					break;

		case 'b':	cout << "\n\nInput Operands: " << "\n\n";
					cin  >> operand1 >> operand2;
					difference = operand1 - operand2;
					cout << "The difference is " << difference << "\n\n";
					break;

		case 'c':	cout << "\n\nInput Operands: " << "\n\n";
					cin  >> operand1 >> operand2;
					quotient = operand1 / operand2;
					cout << "The quotient is " << quotient << "\n\n";
					break;

		case 'd':	break;

		default:
			cout << "Input Error!";
		
	}
	

	
	return 0;
}

Adding a simple while loop that is set to 1 = (on) and turning it to 0 (off) is how i do it.

#include <iostream>

using namespace std;

int main ()
{
	double operand1, operand2;
	double sum, difference, quotient;
    int exit = 1;
	char choice;
        
    while (exit == 1)
    {
	cout << "\n\nBASIC CALCULATOR\n"
		 << "a. Addition\n"
		 << "b. Subtraction\n"
		 << "c. Division\n\n"
		 << "d. Quit\n\n";
	
	//Get the choice
	cout << "Insert Option:";
	cin  >> choice;

	
	
	//Switch to deal with four cases
	switch (choice)
	{
		case 'a':	cout << "\n\nInput Operands: " << "\n\n";
					cin  >> operand1 >> operand2;
					sum = operand1 + operand2;
					cout << "The sum is " << sum << "\n\n";
					break;

		case 'b':	cout << "\n\nInput Operands: " << "\n\n";
					cin  >> operand1 >> operand2;
					difference = operand1 - operand2;
					cout << "The difference is " << difference << "\n\n";
					break;

		case 'c':	cout << "\n\nInput Operands: " << "\n\n";
					cin  >> operand1 >> operand2;
					quotient = operand1 / operand2;
					cout << "The quotient is " << quotient << "\n\n";
					break;

		case 'd':	        exit = 0;
                            break;

		default:            exit = 0;
			                cout << "Input Error!";
		
	} // exit switch
	
	} // exit while loop

	
	return 0;
}

Cool I see how that works. Thanks! Where should I enter system ("cls") to clear the screen though?

} // exit switch
        system("PAUSE");
	system("CLS");
	} // exit while loop

I use the "PAUSE" to let u see the result first, then u can clear it after

Edited 6 Years Ago by MasterGberry: n/a

Thanks, I really appreciate it. You learn something new everyday...

Cool I see how that works. Thanks! Where should I enter system ("cls") to clear the screen though?

Nowhere!!!! It's unprofessional to clear the screen for no real reason. It's also programming for a specific operating system so it's not portable.

Unless there's an actual need to execute a child program, you should avoid using system() .

Edited 6 Years Ago by WaltP: n/a

you should tell that to my professor...or is there any other way to clear the screen?

I'd like to. When a professor is teaching you C++ with a compiler built almost 30 years ago, what are you really learning? Non-standard coding practices, functions that don't exist today and haven't for decades, techniques you will not be able to use if you ever want to find a job. The least he could do is use a free compiler that is up to current standards. Many exist, then he can at least teach you standard C++ which will help you become a professional.

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