I saw the same thing I'm about to post on here earlier, but the difference is that I wrote the program, but it doesn't do anything save compile. The code's a bit sloppy, and I'm just not quite sure where to begin with the bugs.

The actual assignment is as follows:
-------------------------------------------------------------------------------------------------------
PROGRAMMING PROJECT #2
DUE: October 13, 2009 12:00 midnight

A stack machine is a model for executing computer programs. There are compilers that translate a high-level computer language into a set of instructions to be executed using a stack machine model. A stack machine consists of operands, instructions, a stack, and an evaluation model.

Consider the following stack machine instructions and their purpose.
INSTRUCTION PURPOSE EXAMPLE
push push the value onto the stack push 8
pushc push the contents of the variable onto the stack push x
add pop two operands from the stack, add them, and push the result onto the stack push 6
push 10
add
mult pop two operands from the stack, multiply them, and push the result onto the stack push 61
push 38
mult
sub pop two operands from the stack, subtract them (top element…8… is subtracted from the second from the top…5), and push the result (3) onto the stack. push 5
push 8
sub
div pop two operands from the stack, divide them (top element..10… is divided by the second from the top…5), and push the result (2) onto the stack. push 5
push 10
div
assn pops two values from the stack and assigns the value of the 2nd element (4) to the top element (x) push 4
push x
assn
dspy pops and prints the value at the top of the stack push 10
dspy

Imagine a hypothetical compiler that translates a computer program written in a language called fluffy (funny language used for fun yall) into an equivalent set of stack machine instructions. These instructions are then executed by the stack machine's evaluation model to carry out the meaning of the computer program. Here are example fluffy programs and their equivalent stack instructions.

fluffy stack machine instructions
output 8 + 6 push 8
push 6
add
dspy

fluffy stack machine instructions
x = 4
y = (7+x)*9
output y push 4
push x
assn
push 7
pushc x
add
push 9
mult
push y
assn
dspy


For this assignment you are to write a program that will act as an interpreter for stack machine instructions. The input to your program will be a file containing stack machine instructions. Your program will execute those instructions. Any output generated by the instructions should display to the screen.

The basic algorithm for a stack machine evaluation model is:

1. Create a stack
2. Create a symbol table for the variables x,y, and z. This is an array of size 3. Array position 0 is for x, position 1 is for y, and position 2 is for z.
2. while there are more instructions to evaluate do
get next instruction
if the instruction is 'push' then push the operand onto the stack
if the instruction is 'pushc' then get value of operand from the symbol table and push it onto the stack
if the instruction is 'add', 'mult', 'div', or 'sub', pop two operands from the stack and perform the operation. push the result back onto the stack
if the instruction is 'assn' pop two operands from the stack, put the value of the top element in the symbol table for the second top element
if the instruction is 'dspy' pop an operand from the stack stack and print it to the screen
end do


fluffy
To run your program, the user will be prompted for a file name containing a list of instructions.
Your program will open the file, and read and execute the instructions, sending any output to the screen. You can assume the file contains an error-free and correct set of instructions.
Use either a Linked Stack or an array-based Stack to implement this program.
------------------------------------------------------------------------------------------------------------------------------

First, here's the header:

//Ebony Lewis
//Stack header
//October 08, 2009
/*---FluffyStack.h------------------------------------------------*/

#include <iostream>

#ifndef STACK
#define STACK

const int STACK_CAPACITY = 128;
typedef int StackElement;

/*Make a stack class that will take in and execute instructions from the readfile.*/
class FluffyStack {
      public:
             /*----Function Members----*/
             FluffyStack(); //Constructor for a stack.
             void push(const StackElement  value); //Add a value to the stack.
             void pushc(const StackElement  value); //Add the contents of a variable to the stack.
             int pop(); //Pops top value, assigns it to a variable, decrements array slot.
             void add(); //Pop two values, add them, and push result onto the stack.
             void sub(); //Pop two values, subtract the top from the bottom, and push result onto the stack.
             void mult(); //Pop two values, multiply them, and push the result onto the stack.
             void div(); //Pop two values, divide top by bottom, and push result onto the stack.
             void assn(); //Pop two values, assign the value of the 2nd element to the top element.
             void dspy(); //Pop top value and print it to the screen.
      private:
              /*---Data Members----*/
              StackElement myArray[STACK_CAPACITY];
              int myTop;
};

#endif

And now, here's the source file.

//Ebony Lewis
//Program 2: Stack Instruction Interpreter
//October 09, 2009

#include <iostream>
#include <fstream>
#include "lewis_prog2_header.h"
using namespace std;

FluffyStack::FluffyStack(): myTop(-1)
{}
void FluffyStack::push(const int value)
{/*Will place a value on the top of the stack or output error message if stack is full.*/
  if (myTop < STACK_CAPACITY -1) //Make sure that the stack has room.
            {
            ++myTop; //Increment myTop position to push onto top of the stack.
            myArray[myTop] = value;
            }
  else
  {
      cout << "Stack is full! Can't add a new value." << endl;
      cout << "Please increase value of STACK_CAPACITY in lewis_prog2_header.h!" << endl;
      exit(1);
  }          
}
void FluffyStack::pushc(const int value) 
{/*Same as push function, only this one will push the value of a variable (in the form of an array slot).*/
        
     if (myTop < STACK_CAPACITY -1)
     {
               ++myTop;
               myArray[myTop] = value;
               }
     else
     {
         cout << "Stack is full! Can't add a new value." << endl;
         cout << "Please increase value of STACK_CAPACITY in lewis_prog2_header.h!" <<endl;
         exit(1);
     }
}
int FluffyStack::pop() 
{/*Will take the top value in the stack, assign it to a variable, decrement the slot number, and returns the variable.*/
     int holdplease; //Holding variable declaration.
     holdplease = myArray[myTop]; //Holder is the top value in the array.
     myTop--; //Slide down the array once this function is done.
     return holdplease; //Return the value.
}
void FluffyStack::add()
{ /*Will pop two values from the top, assign those values
  to two variables in the order that they were popped, 
  add them together, and then push the result onto the top of the stack.*/
    int firstvalue, secondvalue, plusval;
    firstvalue = pop();
    secondvalue = pop();
    plusval = firstvalue + secondvalue;
    push(plusval);
}
void FluffyStack::sub()
{/*Will pop two values from the top, assign those values
  to two variables in the order that they were popped, 
  subtract the second from the first, and then push the result onto the top of the stack.*/
     int firstvalue, secondvalue, minusval;
     firstvalue = pop();
     secondvalue = pop();
     minusval = firstvalue - secondvalue;
     push(minusval);
}
void FluffyStack::mult()
{/*Will pop two values from the top, assign those values
  to two variables in the order that they were popped, 
  multiply them together, and then push the result onto the top of the stack.*/
     int firstvalue, secondvalue, multiplyval;
     firstvalue = pop();
     secondvalue = pop();
     multiplyval = firstvalue*secondvalue;
     push(multiplyval);
}
void FluffyStack::div()
{/*Will pop two values from the top, assign those values
  to two variables in the order that they were popped, 
  divide the first by the second, and then push the result onto the top of the stack.*/
     int firstvalue, secondvalue, divideval;
     firstvalue = pop();
     secondvalue = pop();
     divideval = firstvalue/secondvalue;
     push(divideval);
}
void FluffyStack::dspy()
{/*Will display the top value of the stack.*/
     cout << myArray[myTop] << endl;
}
void FluffyStack::assn()
{/*Will pop two values, assign them to variables
 in the order they were popped, and assign the 
 secondvalue to the firstvalue, and push the firstvalue.*/
     int firstvalue, secondvalue;
     firstvalue = pop();
     secondvalue = pop();
     firstvalue = secondvalue;
     push(firstvalue);    
}
int main()
{
    FluffyStack MachineStack; //MachineStack will be the stack that holds all of the data pushed and popped.
    int arraystorage = 3, operationstorage = 15, filelot = 25, value; //Three variables for allotted array slots as well as one for a value.
    int StoArrayXYZ[arraystorage]; //Symbol table of depth 3.
    int x = StoArrayXYZ[0], y = StoArrayXYZ[1], z = StoArrayXYZ[2]; //Assign variables to each slot of the symbol table.
    ifstream readfile; //Variable for file to be read from.
    char operation[operationstorage], filename[filelot];
    
    cout << "Please enter the filename to be read from: "; //Get the name of the file to be read from.
    cin >> filename; //Assign that name to the input stream variable.
    readfile.open(filename); //Open the file.
    if (!readfile) //If file isn't found, display error message.
{
    cout << "File open failure!" << endl;
    cout << "Please run the program again." <<endl;
}
    
    /*Now, there's a while loop that will read from the file.  Until the end of the file is met,
    the loop will read each line for an operation (push, pushc, etc.) and a value.  If that operation
    is equated to "push" or "pushc" or "add" and so on, that member function will be called and
    the procedure will continue on, passing in the value if any is given.*/
    while(!readfile.eof())
    {
                        readfile >> operation >> value;
                        if (operation =="push")
                        MachineStack.push(value);
                        else 
                        if ((operation =="pushc") && (value=='x'))
                        MachineStack.pushc(x);
                        else
                        if (operation =="pushc" && value=='y')
                        MachineStack.pushc(y);
                        else
                        if (operation =="pushc" && value=='z')
                        MachineStack.pushc(z);
                        else
                        if (operation =="add")
                        MachineStack.add();
                        else
                        if (operation =="sub")
                        MachineStack.sub();
                        else
                        if (operation =="mult")
                        MachineStack.mult(); 
                        else
                        if (operation =="div")
                        MachineStack.div();
                        else
                        if (operation =="assn")
                        MachineStack.assn();
                        else
                        if (operation =="dspy")
                        MachineStack.dspy();
    }
    
 return 0;   
}

Any insight into the issue will be welcome; I've got until midnight to figure out what's up. Thanks in advance!

Recommended Answers

All 4 Replies

Member Avatar for iamthwee

Next time don't leave your h/w to the last minute.

Next time don't leave your h/w to the last minute.

Duly noted. Any other pearls of wisdom? Perhaps related to debugging this thing?

First, I'm surprised the code compiles given that you declare StoArrayXYZ using a non-const int.

You should probably have the class methods defined in a file of the same name as the header file they are declared in, but that has the .cpp extension rather than the .h extension. You should then have the driver program (ie the one with main() in it) in a third file, which also has a .cpp extension, but a name different from the class file name.

But, moving on, what type of bugs are you getting? They are probably stack dumps, no output at all, output that doesn't make sense, or wrong output, though guessing isn't usually an effective strategy.

Your options for runtime errors are to use a debugger or to narrow down the program to a manageable size and throw in output statements to monitor variables along the way to see where the program breaks down. Either process is easier when you are working with a small piece of code rather than trying to figure out an entire program at once. For example, in this program I would comment out lines 105 through 157 and add code to the class files and the driver program to see if I am actually creating a FluffyStack object. That is, I might write a accessor function to get the value of myTop and make sure it is -1 by outputting in main after I declare the FluffyStack object. Or, I might write a function, call it empty(), to tell me if the FluffyStack object is empty, and call it after declaring the FluffyStack object in main() (this one might be useful later on, too).

// linked stack
for(int i=0 ; i<10000 ; i++)
{
stack s;
s.push (i) ;

problems :
1-
2-

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.