using switch to create an object

This is a discussion on using switch to create an object within the C++ Programming forums, part of the General Programming Boards category; Here's the situation... I'm trying to make a 'gradebook' sorta program. I know the code isn't even close to being ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    1

    using switch to create an object

    Here's the situation... I'm trying to make a 'gradebook' sorta program. I know the code isn't even close to being optimized nor programmed in the best way. The problem.. I can't seem to create an instance of CStudent in a switch statement. I want to use the switch statement to create a menu to give an option of adding a student, printing a student, etc... When I try to compile I get an error: "initialization of Student1 is skipped by case label"

    Code:
    #include <iostream>
    using namespace std;
    
    class CStudent //sizeof(CStudent) = 50 bytes
    {
    	public:
    		CStudent()		{setAll();} //my constructor
    		~CStudent(){;}
    		char firstName[20];	//1x20 =20 bytes
    		char lastName[20];	//1x20 =20 bytes
    		unsigned short grades[5]; //2x5 = 10 bytes
    
    		//public member methods
    		void printAll(int x);
    
    
    	private:
    		void setAll();
    
    };
    
    void CStudent::setAll()
    {
    
    
    	cout << "------- Creating a new Student ---------" << endl;
    	cout << "Enter First Name: ";
    	cin >> this->firstName;
    	cout << "Enter Last Name: ";
    	cin >> this->lastName;
    
    	//use loop to enter grades
    	//starts with 0 and goes to 4 //total = 5 grades
    	for(unsigned short gradeNum = 0; gradeNum < 5; gradeNum++)
    	{
    	cout << "Enter Grade #" << (gradeNum + 1) << ": ";
    	cin >> this->grades[gradeNum];
    	}
    }
    
    void CStudent::printAll(int x)
    {
    	cout << "\n---------- Printing Student Info ----------" << endl;
    	cout << "Student Number " << (x+1) << endl;
    	cout << "First Name:\t" << this->firstName << endl;
    	cout << "Last Name:\t" << this->lastName << endl;
    
    	//prints grades 0 to gradeNum
    	for(unsigned short gradeNum = 0; gradeNum < 5; gradeNum++)
    	{
    	cout << "Grade #" << (gradeNum+1) << " grade:\t" << grades[gradeNum] << endl;
    	}
    
    }
    
    
    
    int main()
    {
    	//Menu
    	unsigned short option;
    	cout << "Enter #: " << endl;
    	cin >> option;
    
    	//switch statement based on option
    	switch(option)
    	{
    	case 1: //create an instance of a Student
    		CStudent Student1;
    		break;
    
    	case 2: //print an instance of a Student
    		Student1.printAll(0);
    		break;
    
    	case 3: //print sizeof
    		cout << "Size of Student: " << sizeof(Student1) << endl;
    
    	case 4: //exit
    		return 0;
    		break;
    
    	default:
    		cout << "\nError!" << endl;
    		break;
    	}
    
    
    	return 0; //the switch should take care of exit
    }
    Thanks for the help

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You need a way to store new students when you create them. A simple way would be to use a vector (or array). When the user chooses option one, you push a student back on to the vector (or set the data in a certain part of the array).

    If you just want to test your code with a single student first, move that line of code outside the switch and call setAll when the user selects option 1.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    switch(option)
    {
    case 1: //create an instance of a Student
        CStudent Student1;
        break;
    
    case 2: //print an instance of a Student
        Student1.printAll(0);
        break;
    What happens if option is 2? You end up skipping the code that constructs your Student1 object and go right to using the object.. the very one that doesn't exist because you haven't constructed it yet. That's what the error is telling you.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Code:
    int main()
    {
    	//Menu
    	unsigned short option;
            CStudent* Student1 = NULL;
    
            while(true)
            {
    	cout << "Enter #: " << endl;
    	cin >> option;
    
    	//switch statement based on option
    	switch(option)
    	{
    	case 1: //create an instance of a Student
                    if(Student1 != NULL) delete Student1;
                    Student1 = new CStudent;
    		break;
    
    	case 2: //print an instance of a Student
                    if(Student1 != NULL) Student1->printAll(0);
                    else cout << "Need to initialize with '1'" << endl;
    		break;
    
    	case 3: //print sizeof
    		if(Student1 != NULL) cout << "Size of Student: " << sizeof(*Student1) << endl;
                   else cout << "Need to initialize with '1'" << endl;
                   break;
    
    	case 4: //exit
                    if(Student1 != NULL) delete Student1;
                    return 0;
    		break;
    
    	default:
    		cout << "\nError!" << endl;
    		break;
    	}
            }//while loop
    
            if(Student1 != NULL) delete Student1;
    	return 0; 
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  2. synchronization object choosing
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 04:33 AM
  3. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  4. Dynamic Object Creation (and Management)...
    By Comrade_Yeti in forum C++ Programming
    Replies: 3
    Last Post: 07-31-2005, 01:44 PM
  5. Function doesn't return an object
    By Aiwendil in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2004, 03:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21