Restarting program instead of closing?
Dec 10, 2011 at 3:02am UTC
So I created a simple calculator (I know, how cliche...)
And I was wondering if there's a way to restart it instead of it closing. Like rerunning the program without closing so you can do multiple equations in one.
Here's my code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <iostream>
using namespace std;
int add (int x, int y)
{
cout << "" << x << " and " << y << "\n" ;
return (x+y);
}
int main()
{
cout << "Basic calculator (adding) !\n" ;
int a, b, c;
cout << "Enter two numbers: " ;
cin >> a;
cin >> b;
cout << "\nAdding\n" ;
c=add(a,b);
cout << "\n" ;
cout << a << "+" << b << "=" << c;
cout << "\nRestart when ready.\n\n" ;
return 0;
}
Dec 10, 2011 at 3:07am UTC
Use a do-while loop:
1 2 3 4 5 6 7
do
{
//code here
cout << "Do you want to do another calculation? (y/n) " ;
char yn;
cin >> yn;
}while (yn == 'y' || yn == 'Y' );
Dec 10, 2011 at 6:02am UTC
or
1 2 3 4 5 6 7
do
{
//code here
cout << "Do you want to do another calculation? (y/n) " ;
char yn;
cin >> yn;
}while (toupper (yn) == 'Y' );
Topic archived. No new replies allowed.