Thread: Stack

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    20
    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!

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

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    I just skimmed, but it might have something to do with this:
    Code:
    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;
    		}
    Quote Originally Posted by tarheelfan_08 View Post
    I have one final problem...my year is showing up wrong in the final out put!
    I hate to have to say this, but you have more problems than that. Your Car / stack chimera class, the interface, the variable names... Nothing really makes any sense.

    Somebody else made this suggestion, but you paid no attention. I'll repeat. Why don't you try something like this:
    Code:
    class Car
    {
    	string make;
    	string model;
    	string color;
    	int year;
    	int miles;
    };
    
    class StackNode
    {
    	Car data;
    	StackNode* next;
    };
    Now you'll need to add public members, but part of your code should strongly resemble this.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM