Aug 14, 2014 at 8:56pm UTC
Hi,
I'm pretty new at c++ and I was making a calculator and tried to have the user enter an algorithm but when I make the variable:
char algorithm = +;
It always comes up with this error message:
expected primary-expression befor ';' token.
Can you help please?
-lpj.
Aug 14, 2014 at 8:59pm UTC
+ isn't a character.
'+' is a character.
(Learn to separate program data from the program itself.)
Hope this helps.
Aug 14, 2014 at 9:02pm UTC
Yes however is there a way the user could just enter + or - when using instead of '+' or '-'?
Aug 14, 2014 at 9:57pm UTC
Your question doesn't really make sense.... but I guess the answer would be "no".
The user inputs text. You can take that text as one or more series of characters... such as '+' or '-' (each of which is a character).
What exactly are you trying to do? Maybe if we can see the bigger picture we can help more.
Last edited on Aug 14, 2014 at 9:58pm UTC
Aug 14, 2014 at 10:02pm UTC
If you do something like cin>>ch; //ch is of type char
the user would not need to include the quotes in his input.
Aceix.
Aug 14, 2014 at 10:07pm UTC
I think the op is trying to do something like
1 2 3 4
std::cout << "Please enter an equation (number operator number): " ;
std::cin >> lhs >> opr >> rhs;
result = lhs opr rhs;
but they would need something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
std::cout << "Please enter an equation (number operator number): " ;
std::cin >> lhs >> opr >> rhs;
switch (opr)
{
case '+' :
result = lhs + rhs;
break ;
case '-' :
result = lhs - rhs;
break ;
case '*' :
result = lhs * rhs;
break ;
case '/' :
if (rhs != 0)
result = lhs / rhs;
else result = 0; //really undefined
break ;
default : std::cerr << "Invalid operator." << std::endl;
}
Last edited on Aug 14, 2014 at 10:14pm UTC
Aug 14, 2014 at 10:12pm UTC
Yes giblit i have that switch all I was asking was what Aceix explained. Thanks for the help!
Last edited on Aug 14, 2014 at 10:13pm UTC
Aug 15, 2014 at 6:01am UTC
It takes a while to wrap your head around this stuff but one you do it becomes effortlessly easy, which sometimes makes it hard for those of us who have done this for ages to realize exactly where you are stuck. We're glad to have been of help. (When we can.)