class and case : Errors

This is a discussion on class and case : Errors within the C++ Programming forums, part of the General Programming Boards category; hey guys im a beginner in C++ and its ganna show but here is my code....and when you run it ...

please any help on this mess would be great, first i dont know why im getting a smiley face when im trying to get the players name to show...and the next big problem is why it is showing both "case :" statements when you select one of the choices

if you guys could please help me out and let me know whats going on that would be awesome! thanks

i want it to be flexible, i was going to just leave it blank so that they could enter how ever many characters they needed for their name...again im sure there is a better way but from the very very little i know i thought that it would work that way

i want it to be flexible, i was going to just leave it blank so that they could enter how ever many characters they needed for their name...again im sure there is a better way but from the very very little i know i thought that it would work that way

Use a string, you're already using one for the type, why not for name as well?

"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

LinuxCoder pointed out the reason for this. The play1 instance in main is local to main and you aren't passing it into the clSel function so the version of play1 that is local to the clSel function where you are doing your output is a new instance and does not have any name data associated with it. You need to pass play1 into the function from main and remove the play1 object in clSel.

"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

using the '&' how is that working? its a pointer correct? i havnt messed with pointers much im going to go over the tutorial on this site for them later today but do you guys have any short explenation on whats going on with that part of code? and thanks alot for the help again i was a little bit off

using the '&' how is that working? its a pointer correct? i havnt messed with pointers much im going to go over the tutorial on this site for them later today but do you guys have any short explenation on whats going on with that part of code? and thanks alot for the help again i was a little bit off

In this context, it means that the play1 argument is passed by reference instead of by value. Passing the argument in this manner means that any changes made within the called function (clSel) will still be reflected in the object in the calling function (main). If the object was not passed by reference, then when you pass the play1 object from main to clSel a copy would be made. Changes to the classSel and type variables would only affect the values of the copy of play1 in the clSel function. Once the function exits and control returns to the main function, those changes would be in effect wiped out and play1 would only have the name variable set. Passing by reference (or pointer) enables the changes made in the called function to keep throughout the lifetime of the object wherever it gets passed to/from.

Pass by value/copy:

Code:

int clSel(player1 play1)
{
// Changes made here to play1 will not be kept once function exits
}
...
player1 play1;
clSel(play1); // Changes made once function exits are wiped out

"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

Although this is totally true i believe we must inform/warn our fellow coder that inside the function you won't be able to access the variable in a regular way since it is now a pointer, the following is totally wrong and it won't even compile:

Code:

int clSel(player1* play1)
{
play1.name = "John";
}

So if you use this approach to access the name member of the player1 object that was passed in you'd have to do

hk_mp5kpdw's method is best. Do remember you can only pass L-variables (those you declare with a name) to a function which takes arguments by reference.

Another benefit of passing by reference: only a pointer (4 bytes on a 32-bit machine) is passed, and the program doesn't need to copy the hundreds of bytes the class takes up for each time the function is used.