Thread: class and case : Errors

  1. #1
    Registered User Zero_X's Avatar
    Join Date
    Mar 2006
    Posts
    19

    class and case : Errors

    hey guys im a beginner in C++ and its ganna show but here is my code....and when you run it you'll see what im getting..im not really sure whats going on
    Code:
    #include<iostream>
    #include<string>
    
    #define cls system("cls");
    #define pause system("pause");
    
    using namespace std;
    
    int clSel();
    
    class player1{
          public:
                 char name[0];
                 int classSel;
                 string type;
                 };
                 
    int main()
    {
        player1 play1;
        cout<<"Please enter player 1's name: ";
        cin>>play1.name;
        clSel();
    }
    
    int clSel()
    {
        cls
        player1 play1;
        cout<<play1.name<<"Please Select a Class...\n";
        cout<<"1)Knight\n";
        cout<<"2)Mage\n";
        cin>>play1.classSel;
        
        switch(play1.classSel)
        {
           case 1:{
                play1.type = "Knight";
                cout<<play1.name<<" Has Selected to Be A "<<play1.type<<endl;
                pause
                }
           case 2:{
                play1.type = "Mage";
                cout<<play1.name<<" Has Selected to Be A "<<play1.type<<endl;
                pause;}
           default:{
                cout<<"ERROR\n";
                play1.classSel = 0;
                clSel();}
                }
                
    }
    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

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    class player1{
          public:
                 char name[0];
    How big is the array name? How many characters can it store?

  3. #3
    Registered User Zero_X's Avatar
    Join Date
    Mar 2006
    Posts
    19
    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

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Zero_X
    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

  5. #5
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    Watch out for your declarations of play1. Notice that the program is doing:
    • create a player1 object named play1 in main()
    • enter clSel()
    • create OTHER player1 object named play1 in clSel() (any use of play1 will not refer to the same play1 created in main())


    Also you need a break; in each of the case statements, in switch-case once a match is found you must use break or it will also execute the following case statements.

  6. #6
    Registered User Zero_X's Avatar
    Join Date
    Mar 2006
    Posts
    19
    hey i tried it with a string but i got the same output

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Zero_X
    hey i tried it with a string but i got the same output
    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

  8. #8
    Registered User Zero_X's Avatar
    Join Date
    Mar 2006
    Posts
    19
    ok the break; works and i used it before but i thought it was breaking it out of the program all together but it was just returning to main() and theres nothing left for it to do

    and in my mind i felt like i needed to make the player1 play1 object in the second function so that it could see the class?? im not sure

  9. #9
    Registered User Zero_X's Avatar
    Join Date
    Mar 2006
    Posts
    19
    ooo ok that makes sence to me, i'll try and see if i can make it work. thanks alot guys

  10. #10
    Registered User Zero_X's Avatar
    Join Date
    Mar 2006
    Posts
    19
    ok sorry..but im stuck again...how would i pass it to clSel()? i know i would go
    Code:
     clSel(play1);
    but then what do i put in the clSel() prototype? i know how to do it with datatype int and char is it any different?

  11. #11
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    class player1
    {
            public:
                    string name;
                    int classSel;
                   string type; 
    };
    
    int clSel(player1& play1)          //Notice we set the function to receive a reference meaning we can see and change the variable inside the function
    {
        cout<<play1.name<<" Please Select a Class...\n";
        cout<<"1)Knight\n";
        cout<<"2)Mage\n";
        cin>>play1.classSel;
        switch(play1.classSel)
        {
            case 1:{
                play1.type = "Knight";
                cout<<play1.name<<" Has Selected to Be A "<<play1.type<<endl;
                break;
                }
           case 2:{
                play1.type = "Mage";
                cout<<play1.name<<" Has Selected to Be A "<<play1.type<<endl;
                break;
                }
           default:{
                cout<<"ERROR\n";
                play1.classSel = 0;
                clSel(play1);        // again passing the play1 object as reference so that we can change it inside the function
                }
         }
    }
    
    int main()
    {
        player1 play1;
        cout<<"Please enter player 1's name: ";
        cin>>play1.name;          //Get Player name from input
        clSel(play1);             //Call function passing play1 as reference to be able to change it inside the function
    }
    Changes are in red and comments in blue. Also i have removed the #define commands since those are windows-specific commands and i'm on linux.

    Hope this helps a bit. Cheers

  12. #12
    Registered User Zero_X's Avatar
    Join Date
    Mar 2006
    Posts
    19
    ah ok this is my mental block im at
    Code:
     int clSel(player1& play1)
    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

  13. #13
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Zero_X
    ah ok this is my mental block im at
    Code:
     int clSel(player1& play1)
    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
    Pass by pointer (sometime called reference):
    Code:
    int clSel(player1* play1)
    {
        // Changes made here to play1 will be kept once function exits
    }
    
    ...
    
    player1 play1;
    clSel(&play1);  // Changes made here stay put
    Pass by reference:
    Code:
    int clSel(player1& play1)
    {
        // Changes made here to play1 will be kept once function exits
    }
    
    ...
    
    player1 play1;
    clSel(play1);  // Changes made here stay put
    Note: Typically a function such as clSel would be made a member function of the player1 class. You then wouldn't need to pass anything to the function.
    "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

  14. #14
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    Pass by pointer (sometime called reference):
    Code:
    int clSel(player1* play1)
    {
        // Changes made here to play1 will be kept once function exits
    }
    
    ...
    
    player1 play1;
    clSel(&play1);  // Changes made here stay put
    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
    Code:
    int clSel(player1* play1)
    {
            (*play1).name = "John";
    }
    Or:
    Code:
    int clSel(player1* play1)
    {
            play1->name = "John";
    }
    Cheers

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Replies: 5
    Last Post: 03-05-2009, 11:32 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM