How to loop a program?

I'm fairly new to C++ and have been having a lot of fun recently messing around with the language however, I haven't been able to figure out something.
I would like my program to go back to the line:

cout << "Type Add for Addition" << endl;

and rerun the whole program after either a math operation was completed or the program else{} error occured.

Thanks for your time and help in advance :D.

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
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>
#include <stdio.h>
#include <windows.h>

using namespace std;

int main()
{
    cout << "Calcluator has loaded!" << endl;
    cout << "Type Add for Addition" << endl;
    cout << "Sub for Subtraction" << endl;
    cout << "Mul for Multiplication" << endl;
    cout << "Div for Division or Quit to quit the program" << endl;
    string function;
    getline(cin, function);

    if(function == "Add"){
            int num1, num2, result;
            cout << "Choose a number." << endl;
            cin >> num1;
            cout << "Choose another number." << endl;
            cin >> num2;
            result = num1+num2;
            cout << "The result is " << result;
    }
    else if(function == "Sub"){
            int num1, num2, result;
            cout << "Choose a number." << endl;
            cin >> num1;
            cout << "Choose another number." << endl;
            cin >> num2;
            result = num1-num2;
            cout << "The result is " << result;
    }
    else if(function == "Mul"){
            int num1, num2, result;
            cout << "Choose a number." << endl;
            cin >> num1;
            cout << "Choose another number." << endl;
            cin >> num2;
            result = num1*num2;
            cout << "The result is " << result;
    }
    else if(function == "Div"){
            int num1, num2, result;
            cout << "Choose a number." << endl;
            cin >> num1;
            cout << "Choose another number." << endl;
            cin >> num2;
            result = num1/num2;
            cout << "The result is " << result;
    }
    else if(function == "Quit"){
        exit(0);
    }
    else{
        printf("Error, ");
        printf("Please choose which math function you would like to use!");
    }

    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

void run_my_calculator()
{
    // place the program to be run here
    std::cout << "Type Add for Addition etc.\n" ;
    // ...
}

bool run_once_more()
{
    std::cout << "run the program once again (y/n)? " ;
    char c ;
    std::cin >> c ;
    return c == 'y' || c == 'Y' ;
}

int main()
{
    std::cout << "calculator has loaded\n" ;
    do run_my_calculator() ; while( run_once_more() ) ;
}
The looping works and thanks for the reply, however on my end it seems to conflict with the actual calculator and only does addition operations. It also doesn't let you re choose which operation method you'd like to use on the loop (addition, subtraction etc.)

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <string>
#include <stdio.h>
#include <windows.h>

using namespace std;

void run_my_calculator()
{
    cout << "Calcluator has loaded!" << endl;
    cout << "Type Add for Addition" << endl;
    cout << "Sub for Subtraction" << endl;
    cout << "Mul for Multiplication" << endl;
    cout << "Div for Division or Quit to quit the program" << endl;
    string function;
    getline(cin, function);

    if(function == "Add" or "add"){
            int num1, num2, result;
            cout << "Choose a number." << endl;
            cin >> num1;
            cout << "Choose another number." << endl;
            cin >> num2;
            result = num1+num2;
            cout << "The result is " << result;
    }
    else if(function == "Sub" or "sub"){
            int num1, num2, result;
            cout << "Choose a number." << endl;
            cin >> num1;
            cout << "Choose another number." << endl;
            cin >> num2;
            result = num1-num2;
            cout << "The result is " << result;
    }
    else if(function == "Mul" or "mul"){
            int num1, num2, result;
            cout << "Choose a number." << endl;
            cin >> num1;
            cout << "Choose another number." << endl;
            cin >> num2;
            result = num1*num2;
            cout << "The result is " << result;
    }
    else if(function == "Div" or "div"){
            int num1, num2, result;
            cout << "Choose a number." << endl;
            cin >> num1;
            cout << "Choose another number." << endl;
            cin >> num2;
            result = num1/num2;
            cout << "The result is " << result;
    }
    else if(function == "Quit" or "quit"){
        exit(0);
    }
}

bool run_once_more()
{
    std::cout << "run the program once again (y/n)? " ;
    char c ;
    std::cin >> c ;
    return c == 'y' || c == 'Y' ;
}


int main(){
    std::cout << "calculator has loaded\n" ;
    do run_my_calculator() ; while( run_once_more() ) ;
}
Last edited on
If loops are still troubling you I would suggest reading through the loops tutorial on this site (here is a quick link to it: http://www.cplusplus.com/doc/tutorial/control/#loops )

Also the reason behind your program only executing addition is due to the conditional statements you used. For example, you used if(function == "Add" or "add") but this is not doing what you think it is, in fact what this statement means is the following:

if the statement function == "Add" is true OR the statement "add" is true then execute the body of this if.

This is the issue because you are not checking if function == "add" you are just checking "add" which makes no sense. What this should be converted to is this:
if (function == "Add" or function == "add")
That way you check to see if function is equal to "Add" or if function is equal to "add".

Another equivalent expression to your original if statement which may help you understand why it is wrong is the following:
if (function == "Add" or "add" == true)
This statement isn't valid in C++, but it may give you a better picture of whats going on.
Last edited on
Oh, I see my mistake with my conditions. Thank You very much for the help and time. I'll give the loops a thorough read and try solve the problem. Thanks, you have been an immense help :D.

EDIT: I've fixed the errors incase you wanted to read the code and see how I fixed it I've pasted in the 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <string>
#include <stdio.h>
#include <windows.h>

using namespace std;

void run_my_calculator()
{
    cout << "Calcluator has loaded!" << endl;
    cout << "Type Add for Addition" << endl;
    cout << "Sub for Subtraction" << endl;
    cout << "Mul for Multiplication" << endl;
    cout << "Div for Division or Quit to quit the program" << endl;
    string function;
    cin >> function;

    if(function == "Add" or function == "add"){
            int num1, num2, result;
            cout << "Choose a number." << endl;
            cin >> num1;
            cout << "Choose another number." << endl;
            cin >> num2;
            result = num1+num2;
            cout << "The result is " << result << endl;
    }
    else if(function == "Sub" or function == "sub"){
            int num1, num2, result;
            cout << "Choose a number." << endl;
            cin >> num1;
            cout << "Choose another number." << endl;
            cin >> num2;
            result = num1-num2;
            cout << "The result is " << result << endl;
    }
    else if(function == "Mul" or function == "mul"){
            int num1, num2, result;
            cout << "Choose a number." << endl;
            cin >> num1;
            cout << "Choose another number." << endl;
            cin >> num2;
            result = num1*num2;
            cout << "The result is " << result <<endl;
    }
    else if(function == "Div" or function == "div"){
            int num1, num2, result;
            cout << "Choose a number." << endl;
            cin >> num1;
            cout << "Choose another number." << endl;
            cin >> num2;
            result = num1/num2;
            cout << "The result is " << result << endl;
    }
    else if(function == "Quit" or function == "quit"){
        exit(0);
    }
}


int main(){
    run_my_calculator();

    cout << "Would you like to run the calculator again? (Y/N)" << endl;
    string runoncemore;
    cin >> runoncemore;
    if(runoncemore == "Y" or runoncemore == "y"){
        run_my_calculator();
    }
    return 0;

}
Last edited on
Favour writing many small functions:

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
54
#include <iostream>

char get_choice() {

    std::cout << "Type\n\t+ for Addition\n"
                 "\t- for Subtraction\n"
                 "\t* for Multiplication\n"
                 "\t/ for Division\n" ;

    char choice ;
    std::cin >> choice ; // favour formatted input

    if( choice == '+' || choice == '-' || choice == '*' || choice == '/' ) return choice ;

    std::cout << "invalid choice '" << choice << "'\n" ;
    return get_choice() ;
}

int get_number( std::string tag ) {

    std::cout << "enter " << tag << " number: " ;
    int n ;
    std::cin >> n ;
    return n ;
}

void run_my_calculator() {

    char function = get_choice() ;
    const int num1 = get_number( "first" ) ;
    const int num2 = get_number( "second" ) ;
    std::cout << num1 << ' ' << function << ' ' << num2 << " == " ;

    if( function == '+' ) std::cout << num1 + num2 << '\n' ;
    else if( function == '-' ) std::cout << num1 - num2 << '\n' ;
    else if( function == '*' ) std::cout << num1 * num2 << '\n' ;
    else /* must be division */ if( num2 == 0 ) std::cout << "can't divide by zero!\n" ;
    else std::cout << double(num1) / num2 << '\n' ; // avoid integer division
}

bool run_once_more() {

    std::cout << "\nrun the program once again (y/n)? " ;
    char c ;
    std::cin >> c ;
    return c == 'y' || c == 'Y' ;
}


int main() {

    std::cout << "calculator has loaded\n\n" ;
    do run_my_calculator() ; while( run_once_more() ) ;
}
Topic archived. No new replies allowed.