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:
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
usingnamespace std;
string commands;
int run;
float utadolar;
float utaprecio;
float utatotal;
string calculator;
string cnum;
float ctotal;
string gameselect;
int gnum;
int grnum;
longlong 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";
}
elseif(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;
}
elseif(commands == "exit") {
return(0);
}
elseif(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;
}
elseif(commands == "calculator") {
calculator = "on";
while (calculator != "exit") {
cout <<endl<< "Calculator: ";
cin >> calculator >> cnum;
if (calculator == "print") {
ctotal = cnum;
cout <<endl<< "Result: " << ctotal;
}
}
}
elseif(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";
}
elseif (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;
}
}
elseif(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";
}
}
}
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.
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
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.