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
354 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
  • 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 500 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

776 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