1

This is my program for a dualstack. When I try to call the push method, it doesnt push the struct onto the stack, it pushes 1 even though im passing in the struct for the parameter and the function parameter is receiving a struct. Any help is greatly appreciated. I just want to push the struct I create to the stack, then when I pop it, I want to pop out that struct and print it to the console. Im struggling with it because if I change vmr then it complains. My Code is below:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MAX = 20;
int vmr = 1;
int seed;
int upper;
int lower;
float randNum;
float randNumArr;
int dagabacount = 0;
int numberArrivals;
clock_t start;
double duration;
int fixedvehicles = 0;

//struct for vmr
struct vmr{
    char type;
    char name;
    int repairTime;
    int startTime;
    int finishTime;
};

//declaring dualstack
class repairStack
{
    private:
        int topT;
        int topS;
        int ele[MAX];

    public:
        repairStack();
        void pushTie   (struct vmr);
        void pushStar  (struct  vmr);
        int  popTie    (int *vmr); 
        int  popStar   (int *vmr);
}; 

//initializing dualstack
repairStack::repairStack()
{
    topT = -1;
    topS = MAX;
}

//pushing to tiestack
void repairStack::pushTie( struct vmr )
{
    if( topS == topT + 1 )
    {
        cout<<"\nStack Overflow tStack";
        dagabacount++;
        return;
    }

    topT++;
    ele[topT] = vmr;

    //cout<<"\nInserted item in tStack : "<< vmr;    
}

//pushing to starstack
void repairStack::pushStar( struct vmr )
{
    if( topS == topT + 1 )
    {
        cout<<"\nStack Overflow sStack";
        dagabacount++;
        return;
    }

    topS--;
    ele[topS] = vmr;

    //cout<<"\nInserted item in sStack : "<< vmr <<endl;    
}

//popping to tiestack
int repairStack::popTie( int *vmr )
{
    if( topT == -1 )
    {
        cout<<"\nStack Underflow tStack";
        return -1;
    }

    *vmr = ele[topT--];
    return 0;
}

//popping to starstack
int repairStack::popStar( int *vmr )
{
    if( topS == MAX )
    {
        cout<<"\nStack Underflow sStack";
        return -1;
    }

    *vmr = ele[topS++];
    return 0;
}

int getNumArrivals(){
        randNumArr = float(rand()) / (float(RAND_MAX) + 1.0);
          if(randNumArr < 0.25){
             return 1;
          }
          else if(.25 <= randNumArr < .50){
              return 2;
          }
          else if(.50 <= randNumArr < .75){
              return 3;
          }
          else{
             return 4;
          }
}

int main()
{ 
    //asking user for bounds
    cout << "Please give seed value: ";
    cin >> seed;
    cout << "Please give lower bound of stack: ";
    cin >> lower;
    cout << "Please give upper bound of stack: ";
    cin >> upper;
    srand(seed);
    //initalizing stack 's'
    repairStack s = repairStack();

    //initializing clock to 0 for first arrivals
    start = clock();
    duration = ( clock() - start ) / (double) CLOCKS_PER_SEC;
    cout<<"printf: "<< duration <<'\n';

    while(dagabacount < 5){
        //getting numberArrivals either 1,2,3,4
        numberArrivals = getNumArrivals();
        cout << "Number of Arrivals: " << numberArrivals;
        for(int i = 0; i < numberArrivals; i++){
        //getting random number between 0 and 1 to see if its T or S

          randNum = float(rand()) / (float(RAND_MAX) + 1.0);
          //if tie then push tie
          if(randNum < 0.75){
            struct vmr Tie_A;
            Tie_A.type='T';
            Tie_A.repairTime = 3;
            Tie_A.startTime = clock();
            Tie_A.finishTime = 0;
            Tie_A.name = 'A';
            s.pushTie(Tie_A);
          }
          //if star then push star
          else{
            struct vmr StarA;
            StarA.type='S';
            StarA.repairTime = 7;
            StarA.startTime = clock();
            StarA.finishTime = 0;
            StarA.name = 'A';
            s.pushStar(StarA);
          }
        }

        //fixing star if star stack is not empty, if so, fix a tiefighter if theres one in stack
        if(s.popStar(&vmr) == 0){
            cout << "\nFixed Star" << endl << endl;
            fixedvehicles++;
        }
        else{
            if(s.popTie(&vmr) == 0){
                cout << "\nFixed Tie" << endl << endl;
                fixedvehicles++;
            }
            else{
                cout << "Underflow" << endl;
            }
        }

    }
        cout << "\n5 Vehicles Rejected" << endl;
        cout << "# of Vehicles Serviced: " << fixedvehicles << endl;
        cout << "Average Time of Repair: " << endl;
    return 0;
}
Makyen
  • 31,849
  • 12
  • 86
  • 121
  • `void pushTie (struct vmr)` why do you write `struct` here? I dont know what it means, do you? If you want to pass a parameter of type `vmr` it should be `void pushTie(vmr x)`. Naming variables the same as a type as you do in various places is rather confusing – 463035818_is_not_an_ai Sep 26 '19 at 08:28
  • maybe thats one source of confusion: why did you tag `typedef` ? there is no typedef in your code... – 463035818_is_not_an_ai Sep 26 '19 at 08:29
  • dont use the same name for different things. `vmr` is a struct, a global `int` and different local variables. It is very hard to tell them apart – 463035818_is_not_an_ai Sep 26 '19 at 08:30
  • About [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Sep 26 '19 at 08:48
  • Please don't make more work for others by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](//creativecommons.org/licenses/by-sa/4.0), for SE to distribute the content (i.e. regardless of your future choices). By SE policy, the non-vandalized version is distributed. Thus, any vandalism will be reverted. Please see: [How does deleting work? …](//meta.stackexchange.com/q/5221). If permitted to delete, there's a "delete" button below the post, on the left, but it's only in browsers, not the mobile app. – Makyen Sep 27 '19 at 20:44

1 Answers1

3

Try to avoid global variables. Or at least give it special names like glob_myVarName. In your code, you have a global variable vmr and struct vmr. Your push method has one argument without a name. You have specified the type only. And method refers to the global vmr variable.

Your method should have signature like this:

void repairStack::pushTie(struct vmr v)

But struct keyword is redundant in C++ when you declare variable (not struct itself). So you can use

void repairStack::pushTie(vmr v)
Aconcagua
  • 24,880
  • 4
  • 34
  • 59
P. Dmitry
  • 1,123
  • 8
  • 26