Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 29th, 2006, 11:35 PM   #1
c_newbie
Newbie
 
Join Date: Dec 2005
Posts: 14
Rep Power: 0 c_newbie is on a distinguished road
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
c_newbie is offline   Reply With Quote
Old Mar 29th, 2006, 11:44 PM   #2
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
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;
hbe02 is offline   Reply With Quote
Old Mar 29th, 2006, 11:44 PM   #3
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 372
Rep Power: 0 King is an unknown quantity at this point
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!
King is offline   Reply With Quote
Old Mar 29th, 2006, 11:46 PM   #4
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
or that...
hbe02 is offline   Reply With Quote
Old Mar 29th, 2006, 11:51 PM   #5
c_newbie
Newbie
 
Join Date: Dec 2005
Posts: 14
Rep Power: 0 c_newbie is on a distinguished road
yes.. u're right .. thanks much .. i will do just that
c_newbie is offline   Reply With Quote
Old Mar 29th, 2006, 11:56 PM   #6
c_newbie
Newbie
 
Join Date: Dec 2005
Posts: 14
Rep Power: 0 c_newbie is on a distinguished road
that worked perfectly .. thanx!!
c_newbie is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:33 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC