?
Solved

Easy question on storing math operations in variables

Posted on 2003-02-24
13
Medium Priority
?
309 Views
Last Modified: 2007-12-19
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:

answer = second  op  first

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
Comment
Question by:arve
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
13 Comments
 
LVL 7

Expert Comment

by:burcarpat
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

by:
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

by:arve
ID: 8013342
Thanks for the example as well, I have never used the switch / case before.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:arve
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

by:arve
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

by:anovickis
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

by:burcarpat
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

arve, ask another question about conversion to int and i'll give you the answer
0
 

Author Comment

by:arve
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

by:burcarpat
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

by:arve
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

by:burcarpat
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

by:anovickis
ID: 8020752
bucarpat

If you are refering to my first answer, then
Due to the limitations of the interface your answer was not visible when I made mine, while it was a trivial answer (mine) it more closely addressed the question(s) asked, IMHO.

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

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question