static data members - declaration and inheritance

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yodadbl07
    New Member
    • Nov 2006
    • 15

    #1

    static data members - declaration and inheritance

    I am having a lot of trouble writing this program. It is basically keeping track of sales. But I am not getting any values passed from the main function to the class functions. ex Id is not passing to the func JustSold. I am also having trouble with my static variable TotalSales. Can anyone help?....

    Thanks

    Code:
    #include <iostream>
    #include <iomanip>   
    using namespace std;
    
    #define W setw
    
    class HotDogStand{
    	private:
    		static double TotalSales;
    	public:
    		int StandID;
    		int StandSales[];
    		void Setup ();
    		void CreateStand ();
    		void JustSold (int);
    		void ShowSales (int);
    		int Menu ();
    		static void ShowTotalSales ();
    };
    
    	void HotDogStand::Setup (){
    		StandID = 0;
    	return;
    	}
    	
    	void HotDogStand::CreateStand (){
    		char answer[1];	
    		cout << "Do you want to create a new stand (y/n) ";
    		cin >> answer;
    		
    		if (answer[0] == 'n' || answer[0] == 'N'){
    			return;
    		}
    		
    		++StandID;
    		StandSales[StandID] = 0;
    		
    		cout << "Stand #" << StandID << " created!" << endl << endl;
    		
    	return;
    	}
    	
    	void HotDogStand::JustSold (int StandD){
    		++StandSales[StandID];
    		++TotalSales;
    	return;
    	}
    	
    	void HotDogStand::ShowSales (int StandID){
    		cout << "Stand #" << StandID << " has sold " << StandSales[StandID] << " hotdog(s)! " << endl << endl;
    		
    	return;
    	}
    	
    	void HotDogStand::ShowTotalSales (){
    		cout << TotalSales << " hotdog(s) have been sold!" << endl;
    		
    	return;
    	}
    	
    	int HotDogStand::Menu (){
    		int option;
    		
    		cout << "Welcome, what would you like to do: " << endl;
    		cout << W(7) << "1." << W(4) << "Create New Stand" << endl;
    		cout << W(7) << "2." << W(4) << "Report New Sale" << endl;
    		cout << W(7) << "3." << W(4) << "Show Sales of Specific Stand" << endl;
    		cout << W(7) << "4." << W(4) << "Show Total Sales" << endl;
    		cout << W(7) << "5." << W(4) << "Quit" << endl;
    	
    		cin >> option;
    		
    	return option;
    	}
    	
    	
    int main(void){
    	int option, ID;
    
    	HotDogStand A;
    	A.Setup();
     
    	while (true){
    		
    		option = A.Menu();
    	
    		if (option == 1){
    			A.CreateStand();
    		}
    		else if (option == 2){
    			cout << "Please Enter Stand ID#: ";
    			cin >> ID;
    			A.JustSold (ID);
    		}
    		else if (option == 3){
    			cout << "Please Enter Stand ID#: ";
    			cin >> ID;
    			A.ShowSales (ID);
    		}
    		else if (option == 4){
    			A.ShowTotalSales();
    		}	
    		else if (option == 5){
    			return 0;
    		}		
       }
       return 0;
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    try
    Code:
    class HotDogStand{
    	private:
    		static double TotalSales;  // declare satic data member
    	public:
    		int StandID;
    		int StandSales[10];
    		void Setup ();
    		void CreateStand ();
    		void JustSold (int);
    		void ShowSales (int);
    		int Menu ();
    		static void ShowTotalSales ();
    };
    double HotDogStand::TotalSales=0;  // define TotalSales data member
    
    etc
    you declare satatic data members inside the class declaration but
    you have to define the static data members outside the class declaration.

    also you need to allocate some storage to StandSales[]

    you should now be able to get a bit further with the program

    Comment

    • yodadbl07
      New Member
      • Nov 2006
      • 15

      #3
      thanks a lot, sometimes after a while you skip over little things.

      thanks again

      Comment

      • yodadbl07
        New Member
        • Nov 2006
        • 15

        #4
        hey i have one more question, now that things are working, for some reason when i try to create the second stand I says like stand #22093223 created, why not #2. it also does this in the ShowSales func it says stand #1 has sold #22320492 hotdogs.....

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          Originally posted by yodadbl07
          hey i have one more question, now that things are working, for some reason when i try to create the second stand I says like stand #22093223 created, why not #2. it also does this in the ShowSales func it says stand #1 has sold #22320492 hotdogs.....
          you probably need to initialise your data members to 0 - in particular the elements of StandSales[] which will be undefined

          Comment

          • vinothg
            New Member
            • Oct 2006
            • 21

            #6
            You have a static variable as a data member in your class.Static variables will not be initalized by the constructor.It has to be explicitly define it.

            You need to add this line.
            HotDogStand::To talSales = 0;

            If you still have queries refer
            http://www.parashift.c om/c++-faq-lite/ctors.html#faq-10.10

            Comment

            • horace1
              Recognized Expert Top Contributor
              • Nov 2006
              • 1510

              #7
              the cause of the segmentation faults you were getting was difficult to find. The problem was in
              Code:
              		char answer[1];	
              		cout << "Do you want to create a new stand (y/n) ";
              		cin >> answer;
              answer is an array so you should read a character into it using an index
              Code:
              		cin >> answer[0];
              I guess your original code was corrupting some memory location

              this now works
              Code:
              #include <iostream>
              #include <iomanip>   
              using namespace std;
              
              #define W setw
              
              class HotDogStand{
              	private:
              		static double TotalSales;  // declare satic data member
              	public:
              		int StandID;
              		int StandSales[50];
              		void Setup ();
              		void CreateStand ();
              		void JustSold (int);
              		void ShowSales (int);
              		int Menu ();
              		static void ShowTotalSales ();
              };
              
              	double HotDogStand::TotalSales = 0;  // define TotalSales data member
              
              
              	// Initializes variable StandID to Zero
              	void HotDogStand::Setup (){
              		StandID = 0;
              	return;
              	}
              	
              	//Creates Different Stands and Reserves a Space for them in the array StandSales
              	void HotDogStand::CreateStand (){
              		char answer;	   //** no need for an array
              		cout << "Do you want to create a new stand (y/n) ";
              		cin >> answer;
              		
              		if (answer == 'n' || answer == 'N'){
              			return;
              		}
              		
              		++StandID;
              		
                      StandSales[StandID] = 0;
              		
              		cout << "Stand #" << StandID << " created!" << endl << endl;
              		
              	return;
              	}
              	
              	//Incriments the stands sales and the total sales by one
              	void HotDogStand::JustSold (int StandID){
              		++StandSales[StandID];
              		++TotalSales;
              	return;
              	}
              	
              	//Displays the sales of a specific stand on the screen
              	void HotDogStand::ShowSales (int StandID){
              		cout << "Stand #" << StandID << " has sold " << StandSales[StandID] << " hotdog(s)! " << endl << endl;
              		
              	return;
              	}
              	
              	//Displayes the total sales on the screen
              	void HotDogStand::ShowTotalSales (){
              		cout << TotalSales << " hotdog(s) have been sold!" << endl << endl;
              		
              	return;
              	}
              	
              	//Displays the menu on the screen and returns the users choice
              	int HotDogStand::Menu (){
              		int option;
              		
              		cout << "Welcome, what would you like to do: " << endl;
              		cout << W(7) << "1." << W(4) << "Create New Stand" << endl;
              		cout << W(7) << "2." << W(4) << "Report New Sale" << endl;
              		cout << W(7) << "3." << W(4) << "Show Sales of Specific Stand" << endl;
              		cout << W(7) << "4." << W(4) << "Show Total Sales" << endl;
              		cout << W(7) << "5." << W(4) << "Quit" << endl;
              	
              		cin >> option;
              		
              	return option;
              	}
              	
              	
              int main(void){
              	int option, ID;
              
              	HotDogStand A;
              	A.Setup();
               
              	while (true){
              		
              		option = A.Menu();
              	
              		if (option == 1){
              			A.CreateStand();
              		}
              		else if (option == 2){
              			cout << "Please Enter Stand ID#: ";
              			cin >> ID;
              			A.JustSold (ID);
              		}
              		else if (option == 3){
              			cout << "Please Enter Stand ID#: ";
              			cin >> ID;
              			A.ShowSales (ID);
              		}
              		else if (option == 4){
              			A.ShowTotalSales();
              		}	
              		else if (option == 5){
              			return 0;
              		}		
                 }
                 return 0;
              }

              Comment

              Working...