### Calculator

decimals or square roots. What I want to do is make it so that if the person types exit it exits instead of repeating the previous number forever.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````#include using namespace std; #include using namespace std; #include using namespace std; int a; int b; char operation; int res; string Text = "exit"; int result; int main() { cout << "Hello and welcome to the Calculatron 2000. Please input your math problem. Type exit to exit." << endl; do { cin >> a >> operation >> b; int res = 0; if (operation == '+') res = (a + b); else if (operation == '*') res = (a * b); else if (operation == '/') res = (a / b); else if (operation == '-') res = (a - b); if ( ! (istringstream(exit) >> result) ) result = 0; //istringstream says that no constructor matches the argument list. I'm not sure what's wrong with it. if (result != 0) cout << res << endl; else if (result == 0) return 0; } while (result != 0); }``````

The only other question I have are about brace placement which I usually have a hard time on.
Hi Jace, can't help much as I'm so new, but using "float" or "double" instead of "int" will enable decimal calculations. Also, I only type "using namespace std;" once and no problems so far.

I like your calculator and have changed the layout a bit, but have given up tonight on the "exit" strategy, as I also get an endless loop. Good luck! Don
PS. Will give you my layout if you'd like it, let me know.

for square root: use `std::pow(x, 0.5);`

where x is the value to take the square root of.

For exit, just write exit during operation, and check for the string "exit" first, and then exit, else just continue.
Your code shall not be compiled because identifier 'exit' is not defined. So the compiler shall issue an error when parsing the line

`if ( ! (istringstream(exit) >> result) ) result = 0; //istringstream says that no constructor matches the argument list. I'm not sure what's wrong with it. `
If you want to do it that way, then the trick is to read in the data as string data first, no matter what. If you try to coerce the word 'exit' into an int, you'll put cin into a fail state. Try something like this:
 ``1234567891011`` `````` do { std::string userInput; //a place to stick input, be it 'exit' or an operation cout << "Please input your math problem. Type exit to exit." << endl; std::cin.clear(); //clear any error flags on cin std::getline(std::cin, userInput); //get the user's input, allows for spaces if(userInput == "exit") break; //leave the loop if they type exit std::istringstream usrInputStream(userInput); //put the input into a stream usrInputStream >> a >> operation >> b; //read it the same as before, except using //our stream instead of the cin stream //... rest of code ``````

EDIT: You only have to say `using namespace std;` once, not after every `#include `. On your division, you should check that `b != 0` to guard against a divide-by-zero error.

EDIT 2: Also I would replace the entire bottom part of your loop:
 ``123456789`` `````` if ( ! (istringstream(exit) >> result) ) result = 0; //istringstream blah blah... if (result != 0) cout << res << endl; else if (result == 0) return 0; } while (result != 0);``````

with this:
 ``123`` ``````cout << res << endl; } while (true);``````

Zero is a perfectly valid result for any of your operations. You probably shouldn't kick the user out of the program just because they ran a calculation that results in 0. You probably want to keep on doing calculations until the user says "exit".
Last edited on
Thank you all so much! I found booradley60 to be extremely helpful. My program works correctly now.
Topic archived. No new replies allowed.