How do I...

Im working on a calculator and im having some issues. Im asking the users to enter an operation (cnum) and then, set a float (ctotal) equal to that operation result. Here's the part from the code with issues:

1
2
3
4
5
6
7
8
9
while (calculator != "exit") {
                cout <<endl<< "Calculator: ";
                cin >> calculator >> cnum;
                if (calculator == "print") {
                    ctotal = cnum;
                    cout <<endl<< "Result: " << ctotal;
                }
            }
        }


And if someone needs it, the entire 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;

string commands;
int run;

float utadolar;
float utaprecio;
float utatotal;

string calculator;
string cnum;
float ctotal;

string gameselect;

int gnum;
int grnum;
long long gx;

int lol;

int main() {
    commands = "none";++
    run = 1;
    cout << "Hi, welcome to ThomCommand 0.4.2" <<endl<< "Use 'help' to get a command list"<<endl<<endl<<endl<<endl;
    while (run == 1) {
        cout << "Enter a command: ";
        cin >> commands;
        if (commands == "ping") {
            cout << endl << "pong" << endl << endl;
            commands = "none";
        }
        else if(commands == "help") {
            cout<<"Use 'help' to get commands"<<endl<<"Use 'ping' to get a 'pong'"<<endl<<"Use 'exit' to exit program"<<endl;
            cout<<"Use 'usd-to-ars' to use a dolar to argentinian peso converter (Spanish)"<<endl<<"Use 'calculator' to use a simple calculator";
            cout<<endl<<"Use 'chelp' to get help with calculator"<<endl<<"Use 'games' to play a game"<<endl;
        }
        else if(commands == "exit") {
            return(0);
        }
        else if(commands == "usd-to-ars") {
                utadolar = 1;
                utaprecio = 1;
                cout << "Por favor, introduci el precio del dolar: ";
                cin >> utadolar;
                cout <<endl<< "Introduci el precio del objeto en dolares: ";
                cin >> utaprecio;
                utatotal = (utaprecio*utadolar)+((utaprecio*utadolar)/100*35);
                cout <<endl<< "El precio del producto, más impuestos de aduana es " << utatotal << " pesos";
                cout << endl<<endl;
        }
        else if(commands == "calculator") {
            calculator = "on";
            while (calculator != "exit") {
                cout <<endl<< "Calculator: ";
                cin >> calculator >> cnum;
                if (calculator == "print") {
                    ctotal = cnum;
                    cout <<endl<< "Result: " << ctotal;
                }
            }
        }
        else if(commands == "chelp") {
            cout << "Use 'calculator' to use calculator" << endl << "On the calculator app, you can use the following operations:";
            cout << endl << "'plus' if you want to make A plus B";
            cout << endl << "'minus' if you want to make A minus B";
            cout << endl << "'x' if you want to make A for B";
            cout << endl << "'divide' if you want to make A divided by B" << endl;
            commands = "none";
        }
        else if (commands == "games") {
            cout <<endl<< "Use 'games' if you want to know the games you can play or 'cancel' if you don't want to play a game" <<endl<< "Select a game: ";
            cin >> gameselect;
            if (gameselect == "games") {
                cout <<endl<< "'guess-the-num' to play 'Guess the Number' game" <<endl<< "Select a game: ";
                cin >> gameselect;
            }
            if (gameselect == "guess-the-num") {
                srand(time(NULL));
                gx = rand();
                grnum = (int(gx)%100);
                grnum = grnum + 1;
                cout<<endl<<"Welcome to Guess the Number";
                cout<<endl<<"Introduce your number: ";
                cin>>gnum;
                while (gnum != grnum) {
                    cout<<endl<<"Introduce your number: ";
                    cin>>gnum;
                    if (gnum < grnum) {
                    cout<<endl<<"That's too small";
                }
                if (gnum > grnum) {
                    cout<<endl<<"That's too big";
                }
            }
                cout<<endl<<"You won"<<endl;
            }
        }
        else if(commands == "lol") {
            lol = 1;
            while(lol == 1) {
                cout << "lolo";
            }
        }
        else {
            cout << endl << "Wrong command, use 'help' to get a command list" << endl << endl;
            commands = "none";
        }
    }
}
Last edited on
What does "issues" mean? An error message would be nice :)

I'm guessing it's the following line:

ctotal = cnum

You're assigning a string to a float. You have to convert the string cnum to a float first.
How do I convert it?
Is there any reason why cnum is defined as a string? If you want a number, it might be better to use a numeric type in the first place.

Im not asking the user a number. Here's an example from how to use the calculator:

Input:
print(cin >> calculator) 2+2(>> cnum)
Output:
Result: (cout << "Result: ")4(<< ctotal)
Ok, I see your intention. However in that case you would need to take the string "2+2" and parse it into its individual parts, in this case, the number 2 the operator + and the number 2.

It depends on how much flexibility you are trying to allow in the type of expression entered. If it it limited to a pair of numbers separated by an operator, it isn't too difficult, indeed you could get those three values directly from cin rather than capturing a string and breaking it up afterwards. But if you want a fully general-purpose expression evaluator, the task becomes considerably larger and more challenging.
For parsing the input, use a regular expression: http://www.cplusplus.com/reference/regex/regex_match/ (this is an example of how to use them, if you have questions on regex in general, "." usually means "any character", "*" means "zero or more of the preceding character. For example, ".*" means "a sequence of zero or more characters". "a*" would mean "a sequence of zero or more a's".).

But I agree with Chervil that you should probably ask the user for the first number, then the operator and then the second number. It would make things a lot easier.

For general type conversion, see: http://www.cplusplus.com/doc/tutorial/operators/
But I want to use more numbers, so users can do "complex" calcules, like: (4*3+2)/2
Then you might want to research algorithms for evaluating such expressions. I'm not trying to be unhelpful, on the contrary there's no point re-inventing something from scratch if the ideas already exist:
https://www.google.com/#q=expression+evaluation+algorithm
Thanks Chervil
Programming Principles and Practice Using C++ by Bjarne Stroustrup covers exactly what you are trying to do. He introduces a simple calculator early on then builds on it in the following chapters until it is able to do complex calculations like what you show.
Topic archived. No new replies allowed.