Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# Easy question on storing math operations in variables

Posted on 2003-02-24
Medium Priority
316 Views
I have 3 variables, they are all chars right now but that may have to be switched.

First  <---contains a number
Second  <--- contains a number
Op  <--- contains an operation ( + - / * )

I want to do the following:

Example:  5 = 3 + 2

Thus all the info needed are in three vars, but I have to make them know that they have to do a math operation between them.

I am thinking either that there is a simple way (like syntax ) to do this or that I have to make a function that will do this.

0
Question by:arve
• 6
• 4
• 3

LVL 7

Expert Comment

ID: 8012872
you'll need a function, probably with a switch statement inside which decides on which operation to perform based on the Op parameter.  simply put, you should convert First and Second to numbers ( you can do this using boost's lexical_cast; if you need more info on this, let me know ) and switch Op to perform the actual operation
0

LVL 2

Accepted Solution

anovickis earned 260 total points
ID: 8012882
do something like

switch (Op) {

case '+' : result = First + Second;
break;

case '-' : result = First - Second;
break;

}

etc
don't forget to trap for illegal "Ops"

0

Author Comment

ID: 8013342
Thanks for the example as well, I have never used the switch / case before.
0

Author Comment

ID: 8013558
Umm, burcapat could you show me the convert First and Second to numbers?  I seem to be getting bad answers...

Like 3 * 2 = divisor symbol

Or 8 + 6 = n
0

Author Comment

ID: 8014092
I tried many work arounds, like subtracting '0' but still I end up with an ascii value and not a real char...for instance I get a capital "D" for 4*5, which is correct if you look at the ascii table, but it doesn't make for pretty output.  If anyone knows how to convert this "D" into 20 or as burcarpat was going to do, convert my chars into ints/floats/whatever so I get real numbers and not ascii ones.
0

LVL 2

Expert Comment

ID: 8014702
for starters I think you should consider storing integer type things in "int"

Let's see your whole program and we can give you some ideas if you want.

for printing int use

printf("%d\n", x);

0

LVL 7

Expert Comment

ID: 8015366
anovickis, it is ee etiquette *not* to repeat another expert's solution ( which is exactly what you did; you took mine and put an example on it ).  if the question asker wants further clarification, s/he will ask for it.  there is a thread going on for this kind if behavior in the expert input area.  it is considered utterly rude

0

Author Comment

ID: 8019076
burcarpat... anovickis posted a minute after yours, so it is entirely possible that your post wasn't even up when anovickis started working on his/her solution.
0

LVL 7

Expert Comment

ID: 8019273
i doubt that given the facts that (a) the solution submitted was wrong/incomplete in the first place as First and Second were actually chars not numbers  (b) his/her record shows s/he is a relatively new expert and that s/he did the same thing ( i.e. repeating a previous comment ) in at least one other c++ question ( 20473843 )  (c) even if that was the case ( i.e. s/he posted the comment w/o seeing mine ), it is still ee etiquette to acknowledge the previous expert by saying something like "sorry, didn't see your post"
0

Author Comment

ID: 8019604
Well, thank you both for your comments and solutions, I did figure it out by using these comments/answer plus other information found on this message board.  I don't really want to debate about who I gave the points to, if I did err, I am sorry.  However, for me, the answer I selected was more helpful, even if it may have been out of line.
0

LVL 7

Expert Comment

ID: 8020098
arve, points are not important for me.  i have plenty of 'em.  yet, from his/her other answers, it seems anovickis knows about c/c++ to a degree and thus it is safe to assume s/he will probably be here in the future, too.  thus, s/he should get acquainted with the ee etiquette.  that's why my original post was aimed at him/her but not you.  another unwritten ee rule is "the question asker is always right" and thus i can't debate your decision anyway.  so, it's not your fault; no need to apologize
0

LVL 2

Expert Comment

ID: 8020752
bucarpat

If you are refering to my first answer, then

If you are refering to the second answer, then
I beleive the arve in this case wanted more clarification for a secondary problem.

I'm here to help not play points games.

0

Author Comment

ID: 8021025
Well thanks for the help, both of you then.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article locâ€¦
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data definâ€¦
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor anâ€¦
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
###### Suggested Courses
Course of the Month10 days, 3 hours left to enroll