Hey guys, So i was bored and decided to make a calculator however ive ran into a problem when the user has finished calculating a sum it will re run from beginning of the code all seems well when i compile and run from Code::Block's however when i run from the .exe it closes instead of restart from the top, Heres my code:
#include <iostream>
#include <windows.h>
#include <cmath>
usingnamespace std;
int main()
{
string func;
int x;
int y;
string opt;
int ans;
cout<< "Welcome to Adiminium's Calculator, Would you like to MULTIPLY, SUBTRACT, ADD or DIVIDE?\n";
Sleep(1000);
cout<< "Please note when typing it is case sensitive!\nNow could you please specify if you want to MULTIPLY, ADD, DIVIDE or SUBTRACT\n";
cin>> func;
cout<< "Great now please enter a number!\n";
cin>> x;
cout<< "Excellent now enter another number!\n";
cin>> y;
if (func == "MULTIPLY") ans = x * y;
if (func == "ADD") ans = x + y;
if (func == "SUBTRACT") ans = x - y;
if (func == "DIVIDE") ans = x / y;
cout<< "Your answer is " <<ans<< " \n";
Sleep(500);
cout<< "Would you like to do another sum YES or NO?\n";
cin>> opt;
if (opt == "YES") {WinExec("real fing", SW_SHOW);
exit(1);
} else {
cout<< "Application will now close!";
Sleep(1000);
}
system("PAUSE");
return 0;
}
Im pretty sure theres a faster way to restart program (I got that method from google)
Sorry if its sloppy, Im a newbie and still picking up c++
int main()
{
string func;
int x;
int y;
string opt;
while(true)
{
if (opt == "NO")
{
cout<< "Application will now close!";
Sleep(1000);
exit(1);
}
int ans;
cout<< "Welcome to Adiminium's Calculator, Would you like to MULTIPLY, SUBTRACT, ADD or DIVIDE?\n";
Sleep(1000);
cout<< "Please note when typing it is case sensitive!\nNow could you please specify if you want to MULTIPLY, ADD, DIVIDE or SUBTRACT\n";
cin>> func;
cout<< "Great now please enter a number!\n";
cin>> x;
cout<< "Excellent now enter another number!\n";
cin>> y;
if (func == "MULTIPLY") ans = x * y;
if (func == "ADD") ans = x + y;
if (func == "SUBTRACT") ans = x - y;
if (func == "DIVIDE") ans = x / y;
cout<< "Your answer is " <<ans<< " \n";
Sleep(500);
cout<< "Would you like to do another sum YES or NO?\n";
cin>> opt;
}