Restarting Program

Hi, I have created a calculator, i would like to make it so that it restarts back to beginning or to make is so it doesn't quit at the end, letting you input more numbers to solve.


#include <iostream>

using namespace std;

int a;
int b;
int choice;
int add;
int sub;
int mult;
int div;

int main(){
cout <<"**Welcome to Jon's Calculator**" <<endl;
cout <<"Choose a number " <<endl;
cin >>a;

cout <<"Choose another number" <<endl;
cin >>b;

cout <<"What do you want to do? \n 1.Add \n 2.Subtract \n 3.Multiply \n 4.Divide" <<endl;
cin >>choice;

if (choice==1){
add=a+b;
cout <<"The sum of the two numbers chosen is: " <<add <<endl;
}

if (choice==2){
cout <<"The diffrence of the two numbers is: " <<a-b <<endl;
cout <<"Or: " <<b-a <<endl;
}
if (choice==3){
cout <<"The value obtained multiplying the two numbers is: " <<a*b <<endl;
}
if (choice==4){
cout <<"The value obtained from dividing the first number by the second number is: " <<a/b;
cout <<"With a remainder of: " <<a%b <<endl;

cout <<"The value obtained from dividing the second number by the first number is: " <<b/a;
cout <<"With a remainder of: " <<b%a <<endl;
}
if (choice>4){
cout <<"Sorry, that is not a command i recognize. " <<endl;
}
if (choice<1){
cout <<"Sorry, that is not a command i recognize. " <<endl;
}
}
You want a loop. A do/while or just a while:

1
2
3
4
5
while( condition )
{
  // this code will be repeated over and over as long as
  //  'condition' evaluates to true
}


The easiest way to do this would have a boolean variable that is 'true' initially, then set it to false when you want to loop to exit.
how do i implement that into the code I have already?
1) Put whatever code you want to repeat inside of the while loop body.

2) Choose a condition for the loop to proceed. Put that condition inside the parenthesis.

3) Make that condition false when you want the program to stop looping.
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
#include <iostream>

using namespace std;

int main(){
    int a, b, choice, add, sub, mult; // declare variables here, global are generally 
    //bad unless you know what your doing and WHY/WHEN to use them
    bool repeat = true; 
    
    while(repeat){//this will repeat the program infinitely 
        
    cout <<"**Welcome to Jon's Calculator**" <<endl;
    cout <<"Choose a number " <<endl;
    cin >>a;
    
    cout <<"Choose another number" <<endl;
    cin >>b;
    
    cout <<"What do you want to do? \n 1.Add \n 2.Subtract \n 3.Multiply \n 4.Divide" <<endl;
    cin >>choice;
    
    if (choice==1){
        add=a+b;
        cout <<"The sum of the two numbers chosen is: " <<add <<endl;
    }
    
    if (choice==2){
        cout <<"The diffrence of the two numbers is: " <<a-b <<endl;
        cout <<"Or: " <<b-a <<endl;
    }
    if (choice==3){
        cout <<"The value obtained multiplying the two numbers is: " <<a*b <<endl;
    }
    if (choice==4){
        cout <<"The value obtained from dividing the first number by the second number is: " <<a/b;
        cout <<"With a remainder of: " <<a%b <<endl;
        
        cout <<"The value obtained from dividing the second number by the first number is: " <<b/a;
        cout <<"With a remainder of: " <<b%a <<endl;
    }
    if (choice>4){
        cout <<"Sorry, that is not a command i recognize. " <<endl;
    }
    if (choice<1){
        cout <<"Sorry, that is not a command i recognize. " <<endl;
    }
    }
    return 0; // you forgot this. all functions but void need a return value
}
Topic archived. No new replies allowed.