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
348 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Python 2.7 - French characters 6 47
allStar challenge 1 41
nestparen challenge 4 56
Which Linux flavors will this run on? 6 35
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now