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