Help with restarting a program

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:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <windows.h>
#include <cmath>
using namespace 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++
Last edited on
Can anyone help? Sorry for bumping posts
Where abouts do i place the while loop? at the beggining of code or where the user decides wether to restart?
Try this

#include <iostream>
#include <windows.h>
#include <cmath>
#include <string>
using namespace std;

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

system("PAUSE");
return 0;
}
Thank you very much meeram!
Topic archived. No new replies allowed.