how should I consider operators?

Mar 4, 2009 at 1:16pm
I was wondering how should I consider the operators + - * /
are they characters?
I am writing a program in which a user input an operator (either + - * /) and then a number and he gets the updated total based on the operator and number. If user type X the program exit. I must use while do loop
I am able to get the program started, but I dont know how to handle the operators...are they char?
I am handling them as string...but I am getting an infinite loop after the first input...
Mar 4, 2009 at 1:22pm
The operators are operators, not characters (C++ gets compiled)
You can use something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
char Operator;
cin >> Operator;
switch ( Operator )
{
   case '+':
      total+=number;
      break;
   case '-':
       total -= number;
       break;
   //...
}
Mar 4, 2009 at 1:32pm
ok so as I see from your code
char Operator;

I can handle them as char in the code?

Also I cannot use switch...must use if statements...but the switch you suggested makes a lot of sense! :)
Last edited on Mar 4, 2009 at 1:34pm
Mar 4, 2009 at 2:31pm
You can get characters and than is up to you parsing them and performing operations.

An if()else if() could easily take the place of a switch
Topic archived. No new replies allowed.