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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

929 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

11 Experts available now in Live!

Get 1:1 Help Now