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