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

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"

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.

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