![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Dec 2005
Posts: 14
Rep Power: 0
![]() |
using char type in place of an operator to perform operation
hi
i just wanted to know if there is any way to use an operator like '+' or '/' if i have it as a type char... for example char op='+'; can i use 'op' in place of '+' at all ... or does op just store the '+' but not the value of the operation ... probably a stupid question but just a thought... thanks |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3
![]() |
im guessing your making some sort of calculator where the user enters the operation to perform. well you cannot use op as the operator. you can simply perform a switch statement on the different values op can take. something like this:
switch(op) case '+' : //perform addition break; case '-' : //perform sub break; case '/' : //perform div break; case '*' : //perform mult break; default: //incorrect operation break; |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 380
Rep Power: 3
![]() |
it would just store the char, so if op = '+', you would not be able to go 1 op 2, to add the two numbers. you could just use an if stament
if(op == '+') 1+2; else if (op == '/') 1/2;
__________________
I am Addicted to Linux! |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3
![]() |
or that...
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Dec 2005
Posts: 14
Rep Power: 0
![]() |
yes.. u're right .. thanks much .. i will do just that
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Dec 2005
Posts: 14
Rep Power: 0
![]() |
that worked perfectly .. thanx!!
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|