[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
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
?
372 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is about my first experience with programming Arduino.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Six Sigma Control Plans
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

656 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