This is a calculator that I made that sadly doesn't calculate
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.
#include <iostream>
usingnamespace std;
#include <string>
usingnamespace std;
#include <sstream>
usingnamespace 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);
elseif (operation == '*')
res = (a * b);
elseif (operation == '/')
res = (a / b);
elseif (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;
elseif (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.
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:
1 2 3 4 5 6 7 8 9 10 11
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 usingnamespace 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:
1 2 3 4 5 6 7 8 9
if ( ! (istringstream(exit) >> result) ) result = 0; //istringstream blah blah...
if (result != 0)
cout << res << endl;
elseif (result == 0)
return 0;
} while (result != 0);
with this:
1 2 3
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".