I am currently working on a practice script that runs as a calculator. It evaluates expressions such as
8*7
and returns the value associated with it.
I read about infinite loops running as
for the program that you are writing. I've tried to implement this, and changed it so that it will also check for bad input.
Below is the code I've written:
Note: any line breaks in the if statement are unintended, and only put in to keep it wrapped to the size of the page.
1 2 3 4 5 6 7 8 9 10 11 12
|
do {
if((formula_string[0]==1||2||3||4||5||6||7||8||9||0)||
(input_string.find("+")&&
(input_string.find("-")&&input_string.find("*")&&input_string.find("/"))=string::npos)
{
//display error and ask for new input
}
else
{
//rest of code
}
}
|
Again, the content of just the if statement is below as plain text:
if((formula_string[0]==1||2||3||4||5||6||7||8||9||0)||(input_string.find("+")&&(input_string.find("-")&&input_string.find("*")&&input_string.find("/"))=string::npos) |
It's not pretty, but what it basically does is checks to see if the first character of the input string is a number, and also checks to see if there is an operator in the sequence. I can expand this in the future to check to see if there are two numbers or other things like that.
Would this work, is my question.
Any help is appreciated.