How to loop

Hi im new to c++! so basically i have made a simple calculator but i have no idea how to loop it to starting so users can use it again. so how do i loop this calculator to the starting?

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

using namespace std;

int main()

{
    float a , b , ans ;
    char op ;

    cout << "Enter a math expression :) ";
    cin >> a >> op >> b ;

    if ( op == '+')
    {
        ans = a + b ;
        cout << ans << endl;
    }

    else if ( op == '-')
    {
        ans = a - b;
        cout << ans << endl;
    }

    else if ( op == '*')
    {
        ans = a * b;
        cout << ans << endl;
    }

    else if ( op == '/')
    {
        if ( b == 0)
        {
            cout << "Error" << endl;
        }
        else
        {
            ans = a / b;
            cout << ans << endl;
        }
    }
    else
        cout << "Error" << endl;

    system ("pause");
    return 0;
}
Put do { under your variables then before your con maybe set them to default values then after the error message put } while( true ) and somewhere before that if a user hits a certain key break the loop you should google do/while loop
Thx giblit, but would you mind showing me the code?i am not familiar with 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
50
51
52
#include <iostream>

using namespace std;

int main()

{
    float a , b , ans ;
    char op ;

do{
    cout << "Enter a math expression :) ";
    cin >> a >> op >> b ;

    if ( op == '+')
    {
        ans = a + b ;
        cout << ans << endl;
    }

    else if ( op == '-')
    {
        ans = a - b;
        cout << ans << endl;
    }

    else if ( op == '*')
    {
        ans = a * b;
        cout << ans << endl;
    }

    else if ( op == '/')
    {
        if ( b == 0)
        {
            cout << "Error" << endl;
        }
        else
        {
            ans = a / b;
            cout << ans << endl;
        }
    }
    else
        cout << "Error" << endl;

}while(true);

    system ("pause");
    return 0;
}


Also make sure you don't place variables inside loops, because that's just, BAAD... (Personal experience).

EDIT:

There are alot of ways you can do it.

You can also do:

1
2
3
for(; ;){

}


or

1
2
3
while(0 < 1){

}
Last edited on
Btw it might be better to use a switch instead of all those if/else if and don't forget to put a loop terminator ex:
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
bool contLoop = true;
do
{
    //declare input variable
    //output message so they know what to input
    //get input
    switch(op)
    {
    case '+':
        //addition stuff
        break;
    case '-':
        //subtraction stuff
        break;
    case '*':
        //multiplication stuff
        break;
    case '/':
        //division stuff
        break;
    case 'q':
        contLoop = false;
        break;
    }
} while( contLoop == true );
Last edited on
Topic archived. No new replies allowed.