Thread: Problems with a custom-made class

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    21

    Problems with a custom-made class

    I made a class with its constructors, initializers, methods, everything... but a friend function doesn't seem to work fine.

    Something in the "exception-detection" part went wrong, and the compiler isn't throwing any errors.

    I would really appreciate if someone could take a look at it. Especially in the friend void function Create() on player.cpp

    http://www2.netexplora.com/shaomongo/O2Class.zip


    Here's the implementation of Create(player nuevo)


    Code:
    //------------------------------------------------------------------------------
    //Custom initializer
    //------------------------------------------------------------------------------
    void Create(Player nuevo)
         {
         inicio:
         bool nickRight = false;
         bool levelRight = false;
         bool sexRight = false;
         bool experienceRight = false;
         bool errorNick1 = false;
         bool errorLevel1 = false;
         bool errorLevel2 = false;
         bool errorSex1 = false;
         bool errorSex2 = false;
         bool errorSex3 = false;
         bool errorExperience1 = false;
         bool errorExperience2 = false;
         char *nickTemp;
         char *experienceTemp;
         char *levelTemp;
         char *sexTemp;
         cout<<"----------New player creation----------"<<endl;
         cout<<"Fill the following fields as required"<<endl;
         
    //------------------------------------------------------------------------------
    //Nickname
    //------------------------------------------------------------------------------
    nickname:
    cout<<"Nickname: ";
    cin>>nickTemp;
    
    if((strlen(nickTemp)) > 10)
         {
         errorNick1 = true;
         }
    
    if(errorNick1 = true)
         {
         cout<<"Error: Invalid nickname (10 characters max length)"<<endl;
         nickTemp = NULL;
         nickRight = false;
         }
    
    if(nickRight = false)
         {
         goto nickname;
         } 
    else
         {
         nuevo.setNickname(nickTemp);
         }
         
    //------------------------------------------------------------------------------
    //Level
    //------------------------------------------------------------------------------
    level:
    cout<<"Level: ";
    cin>>levelTemp;
    
    if(strlen(levelTemp) > 3)
         {
         errorLevel1 = true;
         }
    
    for(int verificadorlevel = 0; verificadorlevel < 2; verificadorlevel++)
         {
         if(!(isdigit(levelTemp[verificadorlevel])))
              {
              errorLevel2 = true;
              }
         }
    
    if(errorLevel1 = true)
         {
         cout<<"Error: Invalid level (3 digits max length)"<<endl;
         levelTemp = NULL;
         levelRight = false;
         }
    if(errorLevel2 = true)
         {
         cout<<"Error: Invalid level (only numbers allowed)"<<endl;
         levelTemp = NULL;
         levelRight = false;
         }
         
    if(levelRight = false)
         {
         goto level;
         }
    else
         {
         int levelFinal = atof(levelTemp);
         nuevo.setLevel(levelFinal);
         }
    
    
    //------------------------------------------------------------------------------
    //Sex
    //------------------------------------------------------------------------------
    sex:
    cout<<"Sex(m/f): ";
    cin>>sexTemp;
    
    if(strlen(sexTemp) > 1)
         {
         errorSex1 = true;
         }
    
    if(isdigit(sexTemp[0]))
         {
         errorSex2 = true;
         }
         
    if( sexTemp[0] != 'm' || sexTemp[0] != 'M' || sexTemp[0] != 'f' || sexTemp[0] != 'F' )
         {
         errorSex3 = true;
         }
    
    if(errorSex1 = true)
         {
         cout<<"Error: Invalid sex (1 character max length)"<<endl;
         sexTemp = NULL;
         sexRight = 0;
         }
    
    if(errorSex2 = true)
         {
         cout<<"Error: Invalid sex (only letters allowed)"<<endl;
         sexTemp = NULL;
         sexRight = false;
         }
         
    if(errorSex3 = true)
         {
         cout<<"Error: Invalid sex (unknown sex: "<<sexTemp[0]<<" )"<<endl;
         sexTemp = NULL;
         sexRight = false;
         }
    
    if(sexRight = false)
         {
         goto sex;
         }
    else
         {
         nuevo.setSex(sexTemp[0]);
         }
    
    //------------------------------------------------------------------------------
    //Experience
    //------------------------------------------------------------------------------
    experience:
    cout<<"Experience: ";
    cin>>experienceTemp;
    
    if(strlen(experienceTemp) > 10)
         {
         errorExperience1 = true;
         }
    
    for( int verificadorexp = 0 ; verificadorexp < 9 ; verificadorexp++)
         {
         if( !(isdigit(experienceTemp[verificadorexp])) )
              {
              errorExperience2 = true;
              }
         }
    
    if(errorExperience1 = true)
         {
         cout<<"Error: Invalid experience (10 digits max length)"<<endl;
         experienceTemp = NULL;
         experienceRight = false;
         }
    
    if(errorExperience2 = true)
         {
         cout<<"Error: Invalid experience (only numbers allowed)"<<endl;
         experienceTemp = NULL;
         experienceRight = false;
         }
    
    if(experienceRight = false)
         {
         goto experience;
         }
    else
         {
         int experienceFinal = atof(experienceTemp);
         nuevo.setExperience(experienceFinal);
         }
    }
         
    //------------------------------------------------------------------------------



    And here's the output


    Code:
    ----------New player creation----------
    Fill the following fields as required
    Nickname: test
    Error: Invalid nickname (10 characters max length)
    Level:
    As you see, it says "Invalid nickname", and the nick just has 4 characters.
    And if i write something in Level and hit enter, windows throws an exception .

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    if(nickRight = false)
    you do that all over your code.
    this is not a comparison it's an assignement.
    use
    Code:
    if(nickRight == false)
    or
    Code:
    if(!nickRight)
    Kurt

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    21
    Oh my god that's the dumbest mistake ever. I totally forgot.
    Thank you


    EDIT:

    Sigh, there still are some errors.

    If you get the nickname wrong, after the "error" message, windows throws an exception.

    Same happens if you enter anything in "Level"
    Last edited by Kylecito; 02-06-2006 at 11:45 AM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
         char *nickTemp;
         char *experienceTemp;
         char *levelTemp;
         char *sexTemp;
    They are just uninitialized pointers.
    either allocate some space for them using new
    or declare them as char arrays
    Code:
         char *experienceTemp = new char[20];
       //    or 
         char levelTemp[20];
    Kurt

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Or even better use the C++ string class.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    21
    Okay, no luck yet.

    http://img527.imageshack.us/img527/6...ssexcep8qh.jpg

    I allocated space for the temps...
    Code:
         char *nickTemp = new char[20];
         char *experienceTemp = new char[20];
         char *levelTemp = new char[20];
         char *sexTemp = new char[20];
    and for the Nickname member of the Player class...

    Code:
    if(nickRight == false)
         {
        nickTemp = "test";
        goto nickname;
         } 
    else
         {
         nuevo.Nickname = new char[20];
         nuevo.setNickname(nickTemp);
         }
    Not working yet... if i assign something to nickTemp, the program crashes... and if i don't, if keeps saying "Error: Invalid nickname (10 chars max length)"

    Oh... and using string doesn't work, it has some problem with strlen();

    Thanks to everyone so far

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Using the C++ string class takes a little more work than plopping it into your code, but it is worth it. The time you spend trying to figure out these issues with C style strings could be spent looking up the interface of the string class, where you'll find the length() or size() functions that return the length (instead of using strlen).

    If you stick with C style strings, you'll have to use strcpy to assign a string. You'll also have to remember to call delete [] on your char pointers whenever you are done with the memory.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
        nickTemp = "test";
    This assigns a pointer you need to use strcpy().
    That other error is because you entered more then 19 characters.
    Think you should take Daved' advice and use std::string instead.
    Kurt

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    21
    I made some changes... now the nickname part at least works

    Code:
    if(nickRight == false)
         {
        strcpy(nickTemp,blanco);    //added
        goto nickname;
         } 
    else
         {
         nuevo.Nickname = new char[20];  //added
         nuevo.setNickname(nickTemp);   //added
         }
    And changed the setNickname method to use strcpy.


    But i'm interested on using the string class, where can i learn more of it? its methods and uses? Thanks to everyone who has posted here, you're really nice with this c++ newbie x)

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class declaration problems
    By scwizzo in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2008, 07:47 PM
  2. Including a custom class file
    By Beowolf in forum C++ Programming
    Replies: 29
    Last Post: 09-29-2007, 05:29 PM
  3. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  4. Problems creating a new class
    By nickname_changed in forum C++ Programming
    Replies: 3
    Last Post: 05-22-2003, 01:47 AM
  5. Problems w.r.t Virtual Base Class (inheritance)
    By pankajdynamic in forum C++ Programming
    Replies: 1
    Last Post: 04-15-2002, 10:28 AM