Stack

Hey guys, I have been looking into queue's and stacks this week guys and I decided that I would like to convert my current array into a stack. Does anyone know how to or can give me some assistance with the current code I have?? I really do not even know how to start, and when I do it I want to keep the cars being entered automatically and not have the user input them. And I am not asking anyone to do it for me...just give me some assistance like maybe add one or 2 parts then tell me what I need to do! Or something like that, I will continue to post my code here for everyone to see my progress.

Header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;

//Car Class
class Car
{
protected:
    string make;  //make
    string model; // model
    string color;  // color
    int year;  // year
    int mileage;  // miles on car

public:
                //Constructor that will set information for a new car
        void New_vehicle (string a, string b, string c, int d, int e) 
        {make = a; model = b; color = c; year = d; mileage = e;}
        
        Car(); //Default constructor
        Car(string, string, string, int, int);
        //mutator and accessor functions
        void setMake(string);
    void setModel(string);
    void setColor(string);
    void setYear(int);
    void setMileage(int);

    string getMake();
    string getModel();
    string getColor();
    int getYear();
    int getMileage();

        //Check mileage to see if valid
    void valid_mileage(int);
    void car_details();
    string string_car_details();
};

//Sets to default values
Car::Car() {
        make = " ";
    model = " ";
    color = " ";
    year = 0;
    mileage = 0;
}
        // My Vehicle set up(Make, model, color, year, mileage)
Car::Car(string make, string model, string color, int year, int mileage) {
    this->make =  make;
    this->model = model;
    this->color = color;
    this->year = year;
    valid_mileage(mileage);
}


void Car::setMake(string make) {
    Car::make = make;
}

void Car::setModel(string model) {
    Car::model = model;
}

void Car::setColor(string color) {
    Car::color = color;
}

void Car::setYear(int year) {
    Car::year = year;
}

void Car::setMileage(int mileage) {
    valid_mileage(mileage);
}


string Car::getMake() {
    return make;
}
string Car::getModel() {
    return model;
}
string Car::getColor() {
    return color;
}
int Car::getYear() {
    return year;
}
int Car::getMileage() {
    return mileage;
}


void Car::valid_mileage(int mileage) {
    if (mileage>=0)
        Car::mileage=mileage;
    else {
        Car::mileage=0;
        cout << "WARNING! You have entered invalid mileage!\n";
        }
    }

        void Car::car_details() {
            cout << "The current car is a " << year << ' ' << color << ' '
                        << make << ' ' << model << " with " << mileage << " miles.\n\n";
        }



        string Car::string_car_details() {
            stringstream buf;
            buf << "The current car is a " << year << ' ' << color << ' '
            << make << ' ' << model << " with " << mileage << " miles.\n\n";
            return buf.str();
        }


CPP
1
2
3
4
5
6
7
8
9
10
11
#include "CarClass.h"
using namespace std;

int main() {
	const int SIZE = 6;
		Car Car_array[SIZE] = { Car("Porsche", "911", "Silver", 2005, 45000), 
                                Car("Ford", "Mustang", "Red", 2007, 12600),
                                Car("Voltzwagon", "Jetta", "Black", 2006, 20218),
                                Car("Jeep", "Cherokee", "White", 2000, 98322),
                                Car("Nissan", "Sentra", "Red", 2002, 76046),
                                Car("Voltzwagon", "Beetle", "Black", 2005, 28031)};


Oh and I somehow want to create car class as a list node to hold the information
Last edited on
If you want to convert your array to a stack, then write the whole thing in Forth. >.>

-Albatross


-P.S. This might help. http://cplusplus.com/reference/stl/list/
What is Forth??

This is what I got so far!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;

//Car Class
class Car
{
protected:
    string make;  //make
    string model; // model
    string color;  // color
    int year;  // year
    int mileage;  // miles on car
	int IntStackSize;
	int top;	//top of stack

public:
        //Constructor that will set information for a new car
        void New_vehicle (string a, string b, string c, int d, int e) 
        {make = a; model = b; color = c; year = d; mileage = e;}
        
        Car(); //Default constructor
        Car(string, string, string, int, int);
		//Stack Information
	
		void push(int);
		void pop(int &);
		bool isFull();
		bool isEmpty();

        //mutator and accessor functions
        void setMake(string);
	    void setModel(string);
	    void setColor(string);
	    void setYear(int);
	    void setMileage(int);

	    string getMake();
	    string getModel();
	    string getColor();
	    int getYear();
	    int getMileage();

	        //Check mileage to see if valid
	    void valid_mileage(int);
	    void car_details();
	    string string_car_details();
	


};

//Sets to default values
Car::Car() {
        make = " ";
    model = " ";
    color = " ";
    year = 0;
    mileage = 0;
}
        // My Vehicle set up(Make, model, color, year, mileage)
Car::Car(string make, string model, string color, int year, int mileage) {
    this->make =  make;
    this->model = model;
    this->color = color;
    this->year = year;
    valid_mileage(mileage);
}


void Car::setMake(string make) {
    Car::make = make;
}

void Car::setModel(string model) {
    Car::model = model;
}

void Car::setColor(string color) {
    Car::color = color;
}

void Car::setYear(int year) {
    Car::year = year;
}

void Car::setMileage(int mileage) {
    valid_mileage(mileage);
}


string Car::getMake() {
    return make;
}
string Car::getModel() {
    return model;
}
string Car::getColor() {
    return color;
}
int Car::getYear() {
    return year;
}
int Car::getMileage() {
    return mileage;
}


void Car::valid_mileage(int mileage) {
    if (mileage>=0)
        Car::mileage=mileage;
    else {
        Car::mileage=0;
        cout << "WARNING! You have entered invalid mileage!\n";
        }
    }

        void Car::car_details() {
            cout << "The current car is a " << year << ' ' << color << ' '
                        << make << ' ' << model << " with " << mileage << " miles.\n\n";
        }



        string Car::string_car_details() {
            stringstream buf;
            buf << "The current car is a " << year << ' ' << color << ' '
            << make << ' ' << model << " with " << mileage << " miles.\n\n";
            return buf.str();
        }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "CarClass.h"
using namespace std;

Car::Car(string make, string model, string color, int year, int mileage)
{
	const int SIZE = 6;
	Car Car_array[SIZE] = { Car("Porsche", "911", "Silver", 2005, 45000), 
                                Car("Ford", "Mustang", "Red", 2007, 12600),
                                Car("Voltzwagon", "Jetta", "Black", 2006, 20218),
                                Car("Jeep", "Cherokee", "White", 2000, 98322),
                                Car("Nissan", "Sentra", "Red", 2002, 76046),
                                Car("Voltzwagon", "Beetle", "Black", 2005, 28031)};
	top =-1;

}

//Set up Stack Push
void Car::push(int num)
{
	if (isFull())
	{
		cout << "The Stack is Full!\n";
		exit(1);
	}
	else
	{
		top++;
		Car_array[top] = num;
	}
}

//Set up Stack Pop
void Car::pop(int &num)
{
	if (isEmpty())
	{
		cout << "The Stack is Empty!\n";
		exit(1);
	}
	else
	{
		num = Car_array[top];
		top--;
	}
}

bool Car::isFull()
{
	if (top == IntStackSize -1)
		return true;
	else
		return false;
}
bool Car::isEmpty()
{
	if (top == -1)
		return true;
	else
		return false;
}

int main() {
	
tarheelfan08 wrote:
What is Forth??


http://en.wikipedia.org/wiki/Forth_%28programming_language%29

Are you having any particular troubles with the code you've just posted? Not to be rude but I don't really want to compile it myself and look for errors.
Well the code is not finished enough to run...I just want to know right now if I am going down the right path or completely wrong?
It looks well enough though I don't see why Car seems to be trying to be a stack. I would make the stack a separate class completely and just have in contain cars.
We would seriously recommend using an STL list or an STL deque if you want to create a "stack". Well... at least I would recommend it.

-Albatross
Heh, if you're allowed to use the STL:
std::stack<Var> MyStack;
I imagine that's not the case though.
Weeeel, yes. There is a stack in the STL, but don't you find you can do so much more with the list?

-Albatross
I can not use the STL I must create my own stuff...so am i completely wrong??
Aw... that sucks. That really sucks.

In that case, I recommend creating a class that has a bool and two arrays of cars under protected:, and a few manipulators to mess around with the stack in the exact way one would mess around with a stack.

(Why two arrays? You'll find out when you try to implement push.)

The modifiers should be capable of doing these things to your arrays:
http://en.wikipedia.org/wiki/Stack_(data_structure)#Basic_architecture_of_a_stack

I'll leave the implementation to you. If you are completely stuck, feel free to PM me or post a question in this thread about one or more of the operations.

-Albatross
Ok guys I got my code done using a dynamic stack..all thats left is to fix my errors! Can someone please assist me??

Header File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;

//Car Class
class Car
{
private:
	class StackNode
	{
		friend class Car;
		string word;
		string wordTwo;
		string wordThree;
		int value;
		int valueTwo;
		StackNode *next;

		//Constructor
		StackNode(string word1, string word2, string word3, int value1, int value2, StackNode *next1 = NULL)
		{
			word = word1;
			wordTwo = word2;
			wordThree = word3;
			value = value1;
			value = value2;
			next = next1;
		}
	};
	StackNode *top;

	//Car Class information
    string make;  //make
    string model; // model
    string color;  // color
    int year;  // year
    int mileage;  // miles on car

public:
	Car() { top = NULL; }
		void push(string, string, string, int, int);
		void pop(string &, string &, string &, int &, int &);
		bool isEmpty();

        //Constructor that will set information for a new car
        void New_vehicle (string a, string b, string c, int d, int e) 
        {make = a; model = b; color = c; year = d; mileage = e;}
        
        Car(string, string, string, int, int);
		//Stack Information
	
        //mutator and accessor functions
        void setMake(string);
	    void setModel(string);
	    void setColor(string);
	    void setYear(int);
	    void setMileage(int);

	    string getMake();
	    string getModel();
	    string getColor();
	    int getYear();
	    int getMileage();

	        //Check mileage to see if valid
	    void valid_mileage(int);
	    void car_details();
	    string string_car_details();
	


};

//Sets to default values
Car::Car() {
    make = " ";
    model = " ";
    color = " ";
    year = 0;
    mileage = 0;
}
        // My Vehicle set up(Make, model, color, year, mileage)
Car::Car(string make, string model, string color, int year, int mileage) {
    this->make =  make;
    this->model = model;
    this->color = color;
    this->year = year;
    valid_mileage(mileage);
}


void Car::setMake(string make) {
    Car::make = make;
}

void Car::setModel(string model) {
    Car::model = model;
}

void Car::setColor(string color) {
    Car::color = color;
}

void Car::setYear(int year) {
    Car::year = year;
}

void Car::setMileage(int mileage) {
    valid_mileage(mileage);
}


string Car::getMake() {
    return make;
}
string Car::getModel() {
    return model;
}
string Car::getColor() {
    return color;
}
int Car::getYear() {
    return year;
}
int Car::getMileage() {
    return mileage;
}


void Car::valid_mileage(int mileage) {
    if (mileage>=0)
        Car::mileage=mileage;
    else {
        Car::mileage=0;
        cout << "WARNING! You have entered invalid mileage!\n";
        }
    }

        void Car::car_details() {
            cout << "The current car is a " << year << ' ' << color << ' '
                        << make << ' ' << model << " with " << mileage << " miles.\n\n";
        }



        string Car::string_car_details() {
            stringstream buf;
            buf << "The current car is a " << year << ' ' << color << ' '
            << make << ' ' << model << " with " << mileage << " miles.\n\n";
            return buf.str();
        }


CPP File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "CarClass.h"
using namespace std;

//Push arguments onto stack
void Car::push(string make, string model, string color, int year, int mileage)
{
	top = new StackNode(make, model, color, year, mileage, top);
}

//Pop removed value at top of stack and copies it to variable
void Car::pop(string &make, string &model, string &color, int &year, int &mileage)
{
	StackNode *temp;
	if (isEmpty())
	{
		cout << "The stack is empty.\n";
		exit(1);
	}
	else //Pop value off top of stack
	{
		make = top->word;
		model = top->wordTwo;
		color = top->wordThree;
		year = top->value;
		mileage = top->valueTwo;
		temp = top;
		top = top->next;
		delete temp;
	}
}

//Returns true if stack is empty or false otherwise
bool Car::isEmpty()
{
	if(!top)
		return true;
	else
		return false;
}


int main() {
	
	Car stack;
	string catchWord;
	string catchWord2;
	string catchWord3;
	int catchVal;
	int catchVal2;
	//Push information
	cout << "Pushing first car \n";
	stack.push("Porsche", "911", "Silver", 2005, 45000);
	cout << "Pushing second car \n";
	stack.push("Ford", "Mustang", "Red", 2007, 12600);
	cout << "Pushing third car \n";
	stack.push("Voltzwagon", "Jetta", "Black", 2006, 20218);
	cout << "Pushing fourth car \n";
	stack.push("Jeep", "Cherokee", "White", 2000, 98322);
	cout << "Pushing fifth car \n";
	stack.push("Nissan", "Sentra", "Red", 2002, 76046);
	cout << "Pushing sixth car \n";
	stack.push("Voltzwagon", "Beetle", "Black", 2005, 28031);

	cout << "Popping...\n";
	stack.pop(catchWord, catchWord2, catchWord3, catchVal, catchVal2);
	cout << "The current car is a " << catchVal << " " << catchWord3<< " " << catchWord << " " << catchWord2 << " with " << catchVal2 <<" miles. \n";
	stack.pop(catchWord, catchWord2, catchWord3, catchVal, catchVal2);
	cout << "The current car is a " << catchVal << " " << catchWord3<< " " << catchWord << " " << catchWord2 << " with " << catchVal2 <<" miles. \n";
	stack.pop(catchWord, catchWord2, catchWord3, catchVal, catchVal2);
	cout << "The current car is a " << catchVal << " " << catchWord3<< " " << catchWord << " " << catchWord2 << " with " << catchVal2 <<" miles. \n";
	stack.pop(catchWord, catchWord2, catchWord3, catchVal, catchVal2);
	cout << "The current car is a " << catchVal << " " << catchWord3<< " " << catchWord << " " << catchWord2 << " with " << catchVal2 <<" miles. \n";
	stack.pop(catchWord, catchWord2, catchWord3, catchVal, catchVal2);
	cout << "The current car is a " << catchVal << " " << catchWord3<< " " << catchWord << " " << catchWord2 << " with " << catchVal2 <<" miles. \n";
	stack.pop(catchWord, catchWord2, catchWord3, catchVal, catchVal2);
	cout << "The current car is a " << catchVal << " " << catchWord3<< " " << catchWord << " " << catchWord2 << " with " << catchVal2 <<" miles. \n";

	cout << "\n Attempting to pop again... ";
	stack.pop(catchWord, catchWord2, catchWord3, catchVal, catchVal2);


	return 0;
}


ERRORS:

1>------ Build started: Project: Week13, Configuration: Debug Win32 ------
1> Car.cpp
1>\carclass.h(81): error C2084: function 'Car::Car(void)' already has a body
1> \carclass.h(46) : see previous definition of '{ctor}'
1>\car.cpp(44): error C2264: 'Car::Car' : error in function definition or declaration; function not called
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


You have two definitions of your constructor, one in the class and one outside of it. You can only have one.
So which one should i get rid of the Null one or the variable one??
I updated my code and got it going guys but I have one final problem...my year is showing up wrong in the final out put! It is showing up as random numbers..but everything else looks great even the mileage!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;

//Car Class
class Car
{
private:
	class StackNode
	{
		friend class Car;
		string word;
		string wordTwo;
		string wordThree;
		int value;
		int valueTwo;
		StackNode *next;

		//Constructor
		StackNode(string word1, string word2, string word3, int value1, int value2, StackNode *next1 = NULL)
		{
			word = word1;
			wordTwo = word2;
			wordThree = word3;
			value = value1;
			value = value2;
			next = next1;
		}
	};
	StackNode *top;

	//Car Class information
    string make;  //make
    string model; // model
    string color;  // color
    int year;  // year
    int mileage;  // miles on car

public:
	Car() { top = NULL; }
		void push(string, string, string, int, int);
		void pop(string &, string &, string &, int &, int &);
		bool isEmpty();

        //Constructor that will set information for a new car
        void New_vehicle (string a, string b, string c, int d, int e) 
        {make = a; model = b; color = c; year = d; mileage = e;}
        
        Car(string, string, string, int, int);
		//Stack Information
	
        //mutator and accessor functions
        void setMake(string);
	    void setModel(string);
	    void setColor(string);
	    void setYear(int);
	    void setMileage(int);

	    string getMake();
	    string getModel();
	    string getColor();
	    int getYear();
	    int getMileage();

	        //Check mileage to see if valid
	    void valid_mileage(int);
	    void car_details();
	    string string_car_details();
	


};

//Sets to default values

        // My Vehicle set up(Make, model, color, year, mileage)
Car::Car(string make, string model, string color, int year, int mileage) {
    this->make =  make;
    this->model = model;
    this->color = color;
    this->year = year;
    valid_mileage(mileage);
}


void Car::setMake(string make) {
    Car::make = make;
}

void Car::setModel(string model) {
    Car::model = model;
}

void Car::setColor(string color) {
    Car::color = color;
}

void Car::setYear(int year) {
    Car::year = year;
}

void Car::setMileage(int mileage) {
    valid_mileage(mileage);
}


string Car::getMake() {
    return make;
}
string Car::getModel() {
    return model;
}
string Car::getColor() {
    return color;
}
int Car::getYear() {
    return year;
}
int Car::getMileage() {
    return mileage;
}


void Car::valid_mileage(int mileage) {
    if (mileage>=0)
        Car::mileage=mileage;
    else {
        Car::mileage=0;
        cout << "WARNING! You have entered invalid mileage!\n";
        }
    }

        void Car::car_details() {
            cout << "The current car is a " << year << ' ' << color << ' '
                        << make << ' ' << model << " with " << mileage << " miles.\n\n";
        }



        string Car::string_car_details() {
            stringstream buf;
            buf << "The current car is a " << year << ' ' << color << ' '
            << make << ' ' << model << " with " << mileage << " miles.\n\n";
            return buf.str();
        }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "CarClass.h"
using namespace std;

//Push arguments onto stack
void Car::push(string make, string model, string color, int year, int mileage)
{
	top = new StackNode(make, model, color, year, mileage, top);
}

//Pop removed value at top of stack and copies it to variable
void Car::pop(string &make, string &model, string &color, int &year, int &mileage)
{
	StackNode *temp;
	if (isEmpty())
	{
		cout << "The stack is empty.\n";
		exit(1);
	}
	else //Pop value off top of stack
	{
		make = top->word;
		model = top->wordTwo;
		color = top->wordThree;
		year = top->value;
		mileage = top->valueTwo;
		temp = top;
		top = top->next;
		delete temp;
	}
}

//Returns true if stack is empty or false otherwise
bool Car::isEmpty()
{
	if(!top)
		return true;
	else
		return false;
}


int main() {
	
	Car stack;
	string catchWord;
	string catchWord2;
	string catchWord3;
	int catchVal;
	int catchVal2;
	//Push information
	cout << "Pushing first car \n";
	stack.push("Porsche", "911", "Silver", 2005, 45000);
	cout << "Pushing second car \n";
	stack.push("Ford", "Mustang", "Red", 2007, 12600);
	cout << "Pushing third car \n";
	stack.push("Voltzwagon", "Jetta", "Black", 2006, 20218);
	cout << "Pushing fourth car \n";
	stack.push("Jeep", "Cherokee", "White", 2000, 98322);
	cout << "Pushing fifth car \n";
	stack.push("Nissan", "Sentra", "Red", 2002, 76046);
	cout << "Pushing sixth car \n";
	stack.push("Voltzwagon", "Beetle", "Black", 2005, 28031);

	cout << "Popping...\n";
	stack.pop(catchWord, catchWord2, catchWord3, catchVal2, catchVal);
	cout << "The current car is a " << catchVal << " " << catchWord3<< " " << catchWord << " " << catchWord2 << " with " << catchVal2 <<" miles. \n";
	
	stack.pop(catchWord, catchWord2, catchWord3, catchVal2, catchVal);
	cout << "The current car is a " << catchVal << " " << catchWord3<< " " << catchWord << " " << catchWord2 << " with " << catchVal2 <<" miles. \n";
	
	stack.pop(catchWord, catchWord2, catchWord3, catchVal2, catchVal);
	cout << "The current car is a " << catchVal << " " << catchWord3<< " " << catchWord << " " << catchWord2 << " with " << catchVal2 <<" miles. \n";
	
	stack.pop(catchWord, catchWord2, catchWord3, catchVal2, catchVal);
	cout << "The current car is a " << catchVal << " " << catchWord3<< " " << catchWord << " " << catchWord2 << " with " << catchVal2 <<" miles. \n";
	
	stack.pop(catchWord, catchWord2, catchWord3, catchVal2, catchVal);
	cout << "The current car is a " << catchVal << " " << catchWord3<< " " << catchWord << " " << catchWord2 << " with " << catchVal2 <<" miles. \n";
	
	stack.pop(catchWord, catchWord2, catchWord3, catchVal2, catchVal);
	cout << "The current car is a " << catchVal << " " << catchWord3<< " " << catchWord << " " << catchWord2 << " with " << catchVal2 <<" miles. \n";
	
	cout << "\n Attempting to pop again... ";
	stack.pop(catchWord, catchWord2, catchWord3, catchVal2, catchVal);


	return 0;
}

Sweet, got it fixed! Now I got 2 questions! If I wanted to edit a car in the stack how would I do this! And if you wanted to add more cars to the stack would you just make a larger pop??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;

//Car Class
class Car
{
private:
	class StackNode
	{
		friend class Car;
		string word;
		string wordTwo;
		string wordThree;
		int value;
		int valueTwo;
		StackNode *next;

		//Constructor
		StackNode(string word1, string word2, string word3, int value1, int value2, StackNode *next1 = NULL)
		{
			word = word1;
			wordTwo = word2;
			wordThree = word3;
			value = value1;
			valueTwo = value2;
			next = next1;
		}
	};
	StackNode *top;

	//Car Class information
    string make;  //make
    string model; // model
    string color;  // color
    int year;  // year
    int mileage;  // miles on car

public:
	Car() { top = NULL; }
		void push(string, string, string, int, int);
		void pop(string &, string &, string &, int &, int &);
		bool isEmpty();

        //Constructor that will set information for a new car
        void New_vehicle (string a, string b, string c, int d, int e) 
        {make = a; model = b; color = c; year = d; mileage = e;}
        
        Car(string, string, string, int, int);
		//Stack Information
	
        //mutator and accessor functions
        void setMake(string);
	    void setModel(string);
	    void setColor(string);
	    void setYear(int);
	    void setMileage(int);

	    string getMake();
	    string getModel();
	    string getColor();
	    int getYear();
	    int getMileage();

	        //Check mileage to see if valid
	    void valid_mileage(int);
	    void car_details();
	    string string_car_details();
	


};

//Sets to default values

        // My Vehicle set up(Make, model, color, year, mileage)
Car::Car(string make, string model, string color, int year, int mileage) {
    this->make =  make;
    this->model = model;
    this->color = color;
    this->year = year;
    valid_mileage(mileage);
}


void Car::setMake(string make) {
    Car::make = make;
}

void Car::setModel(string model) {
    Car::model = model;
}

void Car::setColor(string color) {
    Car::color = color;
}

void Car::setYear(int year) {
    Car::year = year;
}

void Car::setMileage(int mileage) {
    valid_mileage(mileage);
}


string Car::getMake() {
    return make;
}
string Car::getModel() {
    return model;
}
string Car::getColor() {
    return color;
}
int Car::getYear() {
    return year;
}
int Car::getMileage() {
    return mileage;
}


void Car::valid_mileage(int mileage) {
    if (mileage>=0)
        Car::mileage=mileage;
    else {
        Car::mileage=0;
        cout << "WARNING! You have entered invalid mileage!\n";
        }
    }

        void Car::car_details() {
            cout << "The current car is a " << year << ' ' << color << ' '
                        << make << ' ' << model << " with " << mileage << " miles.\n\n";
        }



        string Car::string_car_details() {
            stringstream buf;
            buf << "The current car is a " << year << ' ' << color << ' '
            << make << ' ' << model << " with " << mileage << " miles.\n\n";
            return buf.str();
        }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "CarClass.h"
using namespace std;

//Push arguments onto stack
void Car::push(string make, string model, string color, int year, int mileage)
{
	top = new StackNode(make, model, color, year, mileage, top);
}

//Pop removed value at top of stack and copies it to variable
void Car::pop(string &make, string &model, string &color, int &year, int &mileage)
{
	StackNode *temp;
	if (isEmpty())
	{
		cout << "The stack is empty.\n";
		exit(1);
	}
	else //Pop value off top of stack
	{
		make = top->word;
		model = top->wordTwo;
		color = top->wordThree;
		year = top->value;
		mileage = top->valueTwo;
		temp = top;
		top = top->next;
		delete temp;
	}
}

//Returns true if stack is empty or false otherwise
bool Car::isEmpty()
{
	if(!top)
		return true;
	else
		return false;
}


int main() {
	
	Car stack;
	string catchWord;
	string catchWord2;
	string catchWord3;
	int catchVal;
	int catchVal2;
	//Push information
	cout << "Pushing first car \n";
	stack.push("Porsche", "911", "Silver", 2005, 45000);
	cout << "Pushing second car \n";
	stack.push("Ford", "Mustang", "Red", 2007, 12600);
	cout << "Pushing third car \n";
	stack.push("Voltzwagon", "Jetta", "Black", 2006, 20218);
	cout << "Pushing fourth car \n";
	stack.push("Jeep", "Cherokee", "White", 2000, 98322);
	cout << "Pushing fifth car \n";
	stack.push("Nissan", "Sentra", "Red", 2002, 76046);
	cout << "Pushing sixth car \n\n";
	stack.push("Voltzwagon", "Beetle", "Black", 2005, 28031);

	cout << "Popping...\n\n";
	stack.pop(catchWord, catchWord2, catchWord3, catchVal, catchVal2);
	cout << "The current car is a " << catchVal << " " << catchWord3<< " " << catchWord << " " << catchWord2 << " with " << catchVal2 <<" miles. \n";
	
	stack.pop(catchWord, catchWord2, catchWord3, catchVal, catchVal2);
	cout << "The current car is a " << catchVal << " " << catchWord3<< " " << catchWord << " " << catchWord2 << " with " << catchVal2 <<" miles. \n";
	
	stack.pop(catchWord, catchWord2, catchWord3, catchVal, catchVal2);
	cout << "The current car is a " << catchVal << " " << catchWord3<< " " << catchWord << " " << catchWord2 << " with " << catchVal2 <<" miles. \n";
	
	stack.pop(catchWord, catchWord2, catchWord3, catchVal, catchVal2);
	cout << "The current car is a " << catchVal << " " << catchWord3<< " " << catchWord << " " << catchWord2 << " with " << catchVal2 <<" miles. \n";
	
	stack.pop(catchWord, catchWord2, catchWord3, catchVal, catchVal2);
	cout << "The current car is a " << catchVal << " " << catchWord3<< " " << catchWord << " " << catchWord2 << " with " << catchVal2 <<" miles. \n";
	
	stack.pop(catchWord, catchWord2, catchWord3, catchVal, catchVal2);
	cout << "The current car is a " << catchVal << " " << catchWord3<< " " << catchWord << " " << catchWord2 << " with " << catchVal2 <<" miles. \n\n";
	
	cout << "\nAttempting to pop again... ";
	stack.pop(catchWord, catchWord2, catchWord3, catchVal, catchVal2);


	return 0;
}


The point of a stack is that you can only push and pop the top elements. They generally aren't used to for editing the elements inside them.

I'm wondering though, why don't you just make a stack of Cars, and push/pop those instead of passing a large amount of variables around?
I followed my book example and it was set up similar to this! I knew that is how my teacher would grade so I went by it!
Topic archived. No new replies allowed.