Calculator Script

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
"operating systems"
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.
I have another question about the program. To process the input, which will take the form of a string, I have decided that the best way to do it would be to set up a loop that will read one character from the beginning of the main input string and add it to a temporary string storing the first number to be used in the operation.

I will then convert the temporary string to they double type using

1
2
istringstream convert(String_var);
convert>>Result;

and use a function to add them together or take whatever action is appropriate.

Would this be a feasible course of action?
You can't use operator || like that. See here:
http://www.cplusplus.com/forum/beginner/33030/#msg177689
What I said about operator!= applies here as well.
Also, you comparing characters with numbers.

It should actually look like this: if (formula_string[0]>='0' && formula_string[0]<='9')...

Ameobea wrote:
Would this be a feasible course of action?

Yes.
Last edited on
So the if statement should now look like this:

if (formula_string[0]>='0' && formula_string[0]<='9')||(input_string.find("+")&&(input_string.find("-")&&input_string.find("*")&&input_string.find("/"))>0)

Which will evaluate whether the input string contains an operator and if the first character is a number, right?
Last edited on
After you've replaced = by ==, it will evaluate to true if the first character in formula_string is a digit and false otherwise.
In the second part of the condition you're testing a boolean value for equality with string::npos, which is always false.
Right--Thanks!
Also, at everyone's leisure, you can answer my second question above.

http://cplusplus.com/forum/beginner/33042/#msg177751

Thanks again!
Topic archived. No new replies allowed.