?
Solved

How do I modify a C++ class so that variables that are assigned multiple values only apply the last value assigned?

Posted on 2010-11-13
5
Medium Priority
?
367 Views
Last Modified: 2012-05-10
How do I modify a C++ class so that variables that are assigned multiple values only apply the last value assigned?  Currently, the program accepts an expression and it's assignment variables.
ex. (x+y), x=2, y=3;  Output:  Value = 5  or
(x+2), x=1;   Output: Value = 3

Currently, If I assign multiple values to a variable, it applies the first value assigned.
ex. (x+2), x=2, x=10;   Output: Value = 4

How do I modify the class so that last value assigned is applied?
ex. (x+2), x=2, x=10;   Output: Value = 12

I have attached the main files that are used in the program.  I have tried to make the changes in the SymbolTable class but because of my limited expereince with programming and C++ I couldn't get it to compile.
main.cpp
symboltable.cpp
variable.cpp
0
Comment
Question by:NSing9
[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
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:mifergie
ID: 34129180
Looks to me like you have a stack that you are pushing assignments onto.  When you pop them off you first pop off the last item and x gets assigned properly, then you pop the first item off and assign x improperly.  To fix it, change this operation so that it acts as a FIFO - or a ring buffer.
0
 

Author Comment

by:NSing9
ID: 34129204
That's the problem.  I don't know quite how to do that.
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 total points
ID: 34131484
I would make your SymbolTable::insert function a bit more intelligent. ie. check whether the variable name is already in the symbol table, and if so, just change the value. If the variable name is not yet in the symbol table, you can insert a new entry.
0
 

Author Comment

by:NSing9
ID: 34132803
I've tried adding a second insert method that supplies only the variable name.  I was trying to have this method called when the variable name is encountered and have the original insert method check whether the variable is already in the symbol table.  I'm not having any luck with getting this approach to compile.  Any suggestions on coding with your approach?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34134105
Could you show the code you tried (that didn't compile) ?
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
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.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
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