13 Replies - 1017 Views - Last Post: 22 April 2010 - 09:42 PM Rate Topic: -----

#1 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Custom Stack

Posted 20 April 2010 - 06:25 PM

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
#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
#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)};


This post has been edited by tarheelfan_08: 20 April 2010 - 06:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Custom Stack

#2 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Custom Stack

Posted 20 April 2010 - 06:34 PM

Oh and I somehow want to create car class as a list node to hold the information
Was This Post Helpful? 0
  • +
  • -

#3 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Custom Stack

Posted 20 April 2010 - 07:23 PM

This is what I got so far!

#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();
        }



#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() {
	


Was This Post Helpful? 0
  • +
  • -

#4 noclaf  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 94
  • Joined: 18-April 10

Re: Custom Stack

Posted 20 April 2010 - 08:34 PM

I disagree with your decision to place the stack inside the Car class. You want a stack full of your Car objects, not a bunch of Car objects each with their own stack. You could make the array static (meaning it would be the same for all instances of the class Car), but this still doesn't make much logical sense. Here's a simplified example of how you could make a bounded stack of objects:

#include <iostream>
using namespace std;

Foo foo[10];
Foo *fooptr;

class Foo {
  string name;

  public:
    Foo();
    Foo(string);

    string getName();

};

Foo::Foo() {
  this->name = "[no name]";
}

Foo::Foo(string name) {
  this->name = name;
}

string Foo::getName() {
  return name;
}

int main(void) {
  /* Initialize stack pointer */
  fooptr = &foo[0];

  /* add some items to the stack */
  *fooptr++ = Foo("bar");
  *fooptr++ = Foo("baz");
  *fooptr++ = Foo("bam");
  *fooptr++ = Foo("bap");

  /* use pointer arithmetic to determine the number of items of the stack */
  cout << "stack has " << fooptr - &foo[0] << " items on it" << endl;

  /* pop and print while there's still items on the stack */
  while (fooptr != &foo[0])
    cout << (*--fooptr).getName() << endl;

  return 0;

}

Was This Post Helpful? 1
  • +
  • -

#5 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Custom Stack

Posted 20 April 2010 - 08:41 PM

I do not want to use the library stuff..I want to create my own stack

And what you put confused the crap out of me lol sorry
Was This Post Helpful? 0
  • +
  • -

#6 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Custom Stack

Posted 21 April 2010 - 08:13 PM

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
#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
#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 ==========
Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Custom Stack

Posted 21 April 2010 - 08:18 PM

@ noClaf? how is that supposed to work. You declare a array of FOO objects before you even declare the class. Make sure the code you post to help someone compiles before you post it for someone to try and learn from

This post has been edited by ImaSexy: 21 April 2010 - 08:21 PM

Was This Post Helpful? 1
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Custom Stack

Posted 21 April 2010 - 08:34 PM

noclaf gave you good advice. Your Car class is a type of object. A Car (an instance of the Car class) is a single object. It makes no sense for a car to have a stack of cars, or an array of cars, or any other kind of collection of cars. When you did this with an array, the array wasn't part of the Car class, was it? The array was a structure that you created in your main function, and you put a bunch of Cars into the array. If you want to use a stack you should do that the same way -- leave your Car class as it was. There's no reason to change it at all. Now create a Stack class that can hold Car objects -- in a list, array, or whatever data structure you use to implement the stack. Then in your main function you declare a Stack object, and a bunch of Car objects, and put the Cars into the Stack.

This post has been edited by r.stiltskin: 21 April 2010 - 08:34 PM

Was This Post Helpful? 0
  • +
  • -

#9 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Custom Stack

Posted 21 April 2010 - 08:43 PM

This confuses me big time is their anyway you can do an example with what I have you...I understand to keep car stuff the same..but idk what you mean otherwise!
Was This Post Helpful? 0
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Custom Stack

Posted 21 April 2010 - 08:58 PM

You already have an example in your own code that you posted in the first post. There's no array IN the Car class. You created an array of Car objects in main.

In the same manner, you will create a Stack of Car objects in main. So if you want to do that, first you have to code either:
a "CarStack" -- e.g. a Stack class specifically designed to contain Car objects
or
a template Stack class which can contain any type of object, similar to the STL container classes.

I think you're probably not ready to tackle a template so you should just do a CarStack.

There are plenty of examples of stack implementations right here in DIC. You just have to modify one so it handles Cars instead of ints or strings or whatever.
Was This Post Helpful? 1
  • +
  • -

#11 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Custom Stack

Posted 21 April 2010 - 09:02 PM

I updated my coe and got it going guys but I have one final problem...my year is showing up wrong in the final out put!

#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();
        }



#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;
}


Was This Post Helpful? 0
  • +
  • -

#12 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Custom Stack

Posted 22 April 2010 - 01:04 AM

Typo in StackNode 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;
}


As you can see, you first set the value of "value" to value1, then to value2. Maybe use something like this:
StackNode(string word1, string word2, string word3, int value1, int value2, StackNode *next1 = NULL)
{
    word = word1;
    wordTwo = word2;
    wordThree = word3;
    value = value1;
    valueTwo = value2; // valueTwo gets value2
    next = next1;
}


And in main() function, you have to swap the last two paramters of your stack.pop function. So:
stack.pop(catchWord, catchWord2, catchWord3, catchVal2, catchVal);


becomes:
stack.pop(catchWord, catchWord2, catchWord3, catchVal, catchVal2);


That's because catchVal stands for year, and catchVal2 for mileage.
Was This Post Helpful? 2
  • +
  • -

#13 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Custom Stack

Posted 22 April 2010 - 06:48 PM

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

#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();
        }



#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;
}


Was This Post Helpful? 0
  • +
  • -

#14 noclaf  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 94
  • Joined: 18-April 10

Re: Custom Stack

Posted 22 April 2010 - 09:42 PM

No offense, but you've missed the point of object oriented program. Your push and pop methods should accept or return (respectively) objects of type Car, not the individual data elements. It's unclear what you're doing with the 'friend Car' inside the class definition, as well.

Yo dawg, heard you like cars...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1