I am working on finished up a program using stacks. I am getting some funky errors when I attempt to rebuild&compile, so I am certain something in the code is not right.

Program Objective: Just to brief, I am using main and a .h file with classes. I am supposed to write a simulation of three people making pepperoni pizzas in an assembly line. The first person puts the sauce on the pizza, the second places cheese on the pizza and the third puts on the pepperoni. It will consist of 4 stacks. (pizzas waiting for sauce, pizzas waiting for cheese, pizzas waiting for pepperoni, completed pizzas.) The program should also change the random number generator seed each run using srand and time functions.

Pasted below, I have the main cpp file and then underneath it, the .h file.

I appreciate any input or advice you may have. Thank you

Main:

#include <cstdlib>
#include <iostream>
#include <ctime>
#include "stack.h"

using namespace std;

int main(int argc, char *argv[])
{
    stack dough;      //stack components
          sauce; 
          cheese; 
          pepperoni; 
          finished;
          
      int units;
      int current_pizza = 0;
      
    
    cout << "Enter the numer of units to run the simulation: " << endl;
    cin >> units ;
    cout << "\nEnter the probability of dough being ready: " << endl;
    cin >> dough ;
    cout << "\nEnter the probability of sauce being ready: " << endl;
    cin >> sauce ;
    cout << "\nEnter the probability of cheese being ready: " << endl;
    cin >> cheese ;
    cout << "\nEnter the probability of pepperoni being ready: " << endl;
    cin >> pepperoni ;
    
    srand()time(0)); 
    
    for (int i = 0; i <= units; i++)
    {
      if ( rand () / 32767.0 < dough )    
    {
      current_pizza++;
      sauce.push;
    }
      if ( rand () / 32767.0 < sauce )
    {
      if(!sauce.empty ())
      cheese.push (sauce.pop());
      
    }
      if ( rand () / 32767.0 < cheese )
    {
      if(!cheese.empty ())
      pepperoni.push (cheese.pop())
      
    }
      if ( rand () / 32767.0 < pepperoni )
    {
      if(!pepperoni.empty ())
      finished.push (pepperoni.pop());
      
    }
    
}
    
    
    cout << "\n\nAfter" << "   " << units << "time units we have:" << endl;
    
    cout << "\n\n   " << sauce.elements << "pizzas waiting for sauce:" << endl;
    cout << sauce.print;
    cout << "\n   " << cheese.elements << "pizzas waiting for cheese:" << endl;
    cout << cheese.print;
    cout << "\n   " << pepperoni.elements << "pizzas waiting for pepperoni:" << endl;
    cout << pepperoni.print;
    cout << "\n   " << finished.elements << "pizzas completed:" << endl;
    cout << finished.print;
    
    
 
    system("PAUSE");
    return EXIT_SUCCESS;
}

Then the ".h" file:

#include <iostream>

using namespace std;

const int stack_size = 1000;

class stack
{
      private:
         int data [stack_size]; // array of stack elements
         int top;               // index of the top element of the stack
      public:
         stack ();              // constructor creates an empty stack
         void push (int item);  // push adds an item to the top 
                                // of the stack
         int pop ();            // pop removes and returns an element from
                                // the top of the stack
         bool full ();          // returns true if the stack is full
         bool empty ();         // returns true if the stack is empty
         void print ();         // prints the elements in the stack
         int elements ();       // returns the number of elements in the stack
};

// constructor creates an empty stack
stack::stack ()
{
    top = -1;
}

// push adds the given item to the top of the stack
void stack::push (int item)
{
     if (full ())
     {
         cout << "\n\nStack Error: Pushing on a full stack";
         cout << "\nValue being pushed is: " << item;
     }
     else  // OK to add an element
     {
         top++;
         data [top] = item;
     }
}

// pop removes and returns the top element from the stack
int stack::pop ()
{
     if (empty ())
     {
        cout << "\n\nStack Error: Popping an empty stack";
        cout << "\nReturning a space";
        return ' ';
     }
     else   // OK to remove an element
     {
         top--;
         return data [top + 1];
     }
}

// full returns true if the stack is full, else it returns false
bool stack::full ()
{
     if (top == stack_size - 1)
        return true;
     else
         return false;
}

// empty returns true if the stack is empty, else it returns false
bool stack::empty ()
{
     return top == -1;
}

int stack::elements ()
{
    while (!empty())
    {
    return data[stack_size - 1];
    }
    return 0;
}

void stack::print()
{
     
      while (!empty())
      { 
      for(int i= 0 ;i <= top ;i++)
      cout << data[i] << "\n" << endl;
      }
}

Output should read as:
(example)
After (however many) time units we have:

6 pizzas waiting for sauce:
number number number number number number

4 pizzas waiting for cheese:
number number number number

and so on.....

Your declarations in main() are wrong. Use commas to separate the object names instead of semicolons.
i.e.
stack dough, sauce,cheese,pepperoni,finished;

Try that, then let us know if there are other problems.

Ok, I tried that. Set them up as
stack dough, sauce,cheese,pepperoni,finished;
Same problems. The first error actually goes right to the cin << dough;, says there's no match for operator>> (??) Something else has to be wrong...

Thanks for your response, btw.

Ok, I tried that. Set them up as
stack dough, sauce,cheese,pepperoni,finished;
Same problems. The first error actually goes right to the cin << dough;, says there's no match for operator>> (??) Something else has to be wrong...

Other things do need attention. It looks as though you're trying to input a number to use as a probability, but dough is a stack, not a number, so cin >> dough is wrong. Perhaps you should declare another set of variables like float probDough, float probSauce etc. to use for the input.

Ohh ok. So maybe for instance, use D for dough, to read the input instead of dough itself as dough is a stack so that won't work ...I see what you mean there. Will give that a shot and see how that works. Will let you know how that goes. Thanks...

Ohh ok. So maybe for instance, use D for dough, to read the input instead of dough itself as dough is a stack so that won't work ...I see what you mean there.

Yep, D will do just fine, though probDough might help you or someone else understand the code better.
While you're at it, sauce.push on line 38 will need a parameter, presumably currentPizza?

Ok finally ran it to try it out. (At work at the moment, trying to complete this program and work at the same time! sorry for the delayed response....)

It seemed to have taken nicely to that correction, however now it's telling me there is something wrong with my srand function and the time....it says there are too few arguments for the srand function...hmm. I thought that maybe there was a bracket issue there. but they look fine. Any ideas?

Thanks

Ok finally ran it to try it out. (At work at the moment, trying to complete this program and work at the same time! sorry for the delayed response....)

It seemed to have taken nicely to that correction, however now it's telling me there is something wrong with my srand function and the time....it says there are too few arguments for the srand function...hmm. I thought that maybe there was a bracket issue there. but they look fine. Any ideas?

Thanks

No, those brackets don't look fine at all. time(0) needs to be a parameter to srand(). So you need srand(time(0));

Oh wow. Thank you, big typo on my part. That made me look I also found that the .elements and .print need () as well. That worked and my program ran. Thank you for your help! The only thing now is formatting the stack numbers correctly. They are printing downward rather than across.

It's printing:
number
number
number
...

rather than:
number number number number.

But otherwise, the program runs great!

Member Avatar for iamthwee

cout << data << "\n" << endl;

change to:

cout << data << " ";

cout << data << "\n" << endl;

change to:

cout << data << " ";

That works. Thanks!

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.