calc with loop

I've made a calculator program but I can't get it to work with loops. So I have to rerun it everytime. Can someone give me some example source code.
Can we see your code so far?
yes could we see your code

but if you want here is a possible usage
1
2
3
4
while(someValue != someNumber)
{
    //program
}
Last edited on
`
Last edited on
everything seems fine(i m assuming code mistakes caused by iPhone). just put your code except definitons in a while loop and make a query.
like:

1
2
3
4
5
types values;
while(x != someValue)
{
    //your code
}
Last edited on
//can you add it to this one for me as an example.
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
#include <iostream>

int main()
{
    using namespace std;
    cout << "enter a number: \n";
    int x;
    cin >> x;
    cout << "\n";
    cout << "\n";
    cout << "1 = multiplication  ";
    cout << "2 = division  ";
    cout << "3 = addition  ";
    cout << "4 = subtraction  ";
    cout << "-----------------------------------------------------------------------------------------\n";
    cout << "enter your choice: \n";
    char o;
    cin >> o;
    cout << "enter your last number: \n";
    int y;
    cin >> y;
    if ((o = 1))
        cout << x * y;
    else if ((o = 2))
        cout << x / y;
    else if ((o = 3))
        cout << x + y;
    else if ((o = 4))
        cout << x - y;
    else
        cout << "you entered an invalid operator\a";
    return 0;
}
Last edited on
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
#include <iostream>

//    writing this line here would be better
//instead of inside main().
using namespace std;

int main()
{
    //    for beautiful coding
    //always add your declarations
    //to the beginning.
    char o;
    int x;
    int y;

    //    it will keep looping as long
    //as x isnt equal to -1.
    while(x != -1)
    {
        cout << "enter a number: \n";
        cin >> x;
        cout << "\n";
        cout << "\n";
        cout << "1 = multiplication  ";
        cout << "2 = division  ";
        cout << "3 = addition  ";
        cout << "4 = subtraction  ";
        cout << "-----------------------------------------------------------------------------------------\n";
        cout << "enter your choice: \n";
        cin >> o;
        cout << "enter your last number: \n";
        cin >> y;
        if ((o = 1))
            cout << x * y << endl;
        else if ((o = 2))
            cout << x / y << endl;
        else if ((o = 3))
            cout << x + y << endl;
        else if ((o = 4))
            cout << x - y << endl;
        else
            cout << "you entered an invalid operator\a";
    }

    return 0;
}


i m not changing it much since i assume you are a beginner. but try to develop this one it will help you a lot.

and go through for and while loops. and if necessery more previous topics
Last edited on
im learning from c++ primer plus for about two weeks but I learned a lot of basics online before
Last edited on
theres one problem my code above and i dont know whats wrong. the answer always multiplying the 2 numbers even if i use division
Last edited on
good thing knowing it wasnt your homework :D. i m relieved
try it in your compiler. it always multiplies the 2 numbers
wow i never realized. there are some mistakes. sorry

fixed one:
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
#include <iostream>

//    writing this line here would be better
//instead of inside main().
using namespace std;

int main()
{
    //    for beautiful coding
    //always add your declarations
    //to the beginning.
    char o;
    int x;
    int y;

    //    it will keep looping as long
    //as x isnt equal to -1.
    while(x != -1)
    {
        cout << "enter a number: \n";
        cin >> x;
        cout << "\n";
        cout << "\n";
        cout << "1 = multiplication  ";
        cout << "2 = division  ";
        cout << "3 = addition  ";
        cout << "4 = subtraction  ";
        cout << "-----------------------------------------------------------------------------------------\n";
        cout << "enter your choice: \n";
        cin >> o;
        cout << "enter your last number: \n";
        cin >> y;
        //it should be == instead of =
        //haha sorry i didnt realized it :D
        if ((o == 1))
            cout << x * y << endl;
        else if ((o == 2))
            cout << x / y << endl;
        else if ((o == 3))
            cout << x + y << endl;
        else if ((o == 4))
            cout << x - y << endl;
        else
            cout << "you entered an invalid operator\a";
    }

    return 0;
}


note : "==" is a query while "=" is an assignment.
Last edited on
Create a class say CCalc, and then while the instance of your class is not NULL continue running the program:

1
2
3
4
5
6
7
8
9
10
11
12
13
int main (int argc, char* argv[])
{
    do
    {
         // instantiate instance of your CCalc, say a singleton:
         CCalc* pCal = CCalc::GetInstance();
         // do calculations etc...
         // then when user switches calculator off its destructor is called
         // and pCal is deleted and NULL assigned to pointer
    } while (pCal);

    return 0;
}


HTH
@ajh32 well i doubt he would be able to do that couse he said he just started two weeks ago
@Ceset - well at least have a do{} while(condition); loop that keeps looping until the condition is false and then exits.
well i see no reason about you are being worng but also no reason for me too. it is like chosing the percantage of the milk in a chocolate chosing between do{}while() and while() for a beginner
Last edited on
thanks @Ceset
Topic archived. No new replies allowed.