Thread: Okay this one i cannot figure out, it seems fine to me

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    178

    Okay this one i cannot figure out, it seems fine to me

    This is basically a cheat code to use for testing my rpg game as I build it. It's about 1/4 the way done and i just decided to do this and it will not work. Basically it is comparing what you enter to see if it is megan, if so it should assign the values to your stats and go to the main menu. Well it doesn't do that, if I type in megan, it will not give me those stats. Does anyone know what i did? Thanks.

    Code:
    char secret[80] = "megan\0";
          cout<<"+--------------------+\n";
          cout<<"|What is your name?  |\n";
          cout<<"+--------------------+\n\n";
          cout<<"\tPlease enter your name:   ";
          gets(stat.name);
          
          if(strcmp(stat.name, secret))
          {
               strcpy(stat.classname, "Humanoid");
               stat.classn = 9;
               stat.maxhp = 100;
               stat.hp = stat.maxhp;
               stat.maxmp = 100;
               stat.mp = stat.maxmp;
               stat.attack = 100;
               stat.defense = 100;
               menu();
          }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    99
    Seems fine to me too... Posibbly an earlier error? Although I do doubt it.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    9
    try changing
    Code:
    if(strcmp(stat.name, secret))
    to
    Code:
    if(strcmp(stat.name, secret) == 0)
    < 0 string1 less than string2
    0 string1 identical to string2
    > 0 string1 greater than string2
    Last edited by kje11; 05-17-2002 at 10:48 PM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >char secret[80] = "megan\0";
    You don't need to place the nul at the end, the compiler will do it for you.

    >gets(stat.name);
    Very unsafe, I highly recommend not using gets.

    >if(strcmp(stat.name, secret))
    strcmp returns 0 if the two strings are equal, this notation tests if the return value is != 0. You can use one of two notations to do this:
    if ( strcmp ( stat.name, secret ) == 0)
    if ( !strcmp ( stat.name, secret ) )

    -Prelude
    My best code is written with the delete key.

  5. #5
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    >char secret[80] = "megan\0";
    You don't need to place the nul at the end, the compiler will do it for you.
    nice one..... BTW 80 for megan isn't needed.

    You can use one of two notations to do this:
    if ( strcmp ( stat.name, secret ) == 0)
    if ( !strcmp ( stat.name, secret ) )
    Prelude, which one do you prefer?
    The world is waiting. I must leave you now.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    178
    All right thanks it worked. As far as the gets(), what is a "safer" way to do that and still let the user enter spaces with cin>>" bla bla bla " would be invalid
    but with gets it would be. is there something else that will let me keep the whitespaces?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Prelude, which one do you prefer?
    I prefer the first since Lint doesn't give me a warning on it.

    >As far as the gets(), what is a "safer" way to do that and still let the user enter spaces
    Use getline:
    Code:
    cin.getline ( stat.name, sizeof stat.name );
    -Prelude
    Last edited by Prelude; 05-17-2002 at 11:26 PM.
    My best code is written with the delete key.

  8. #8
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    LINT!
    I forgot to download that while saving the man pages site url, thanks for jaring my memory.
    The world is waiting. I must leave you now.

  9. #9
    Registered User
    Join Date
    Jan 2002
    Posts
    69

    Answer

    Code:
    char secret[81] = {"megan"};
          cout<<"+--------------------+\n";
          cout<<"|What is your name?  |\n";
          cout<<"+--------------------+\n\n";
          cout<<"\tPlease enter your name:   ";
          cin.getline(stat.name,80);
          
          if(strcmp(stat.name, secret) == 0)
          {
               strcpy(stat.classname, "Humanoid");
               stat.classn = 9;
               stat.maxhp = 100;
               stat.hp = stat.maxhp;
               stat.maxmp = 100;
               stat.mp = stat.maxmp;
               stat.attack = 100;
               stat.defense = 100;
               menu();
          }
    Some people should answer the whole lot not just 1 or 2 things.
    that should fix it cin.getline (stat.name,80,'\n') 80 is how many charactes allowed to be entered and '\n' is when enter is pressed it gets all of the characters

  10. #10
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    kinght543,
    > char secret[81] = {"megan"};
    Why do you need 81 for a 6 character string?
    The world is waiting. I must leave you now.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Some people should answer the whole lot not just 1 or 2 things.
    It seems to me that we have.

    >char secret[81] = {"megan"};
    This is wasteful of space and you really don't need the curly braces.
    Code:
    char *secret = "megan";
    This will work a bit better and protect the secret name from being changed.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3 dimensional figure volume
    By thekautz in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2009, 05:22 PM
  2. Newbie can't figure out linking error
    By sistahduch in forum C Programming
    Replies: 9
    Last Post: 08-10-2008, 09:41 PM
  3. trying to figure out someone's code for a plugin
    By paulpars in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:57 AM
  4. CAN'T FIGURE IT -- structs on a linked list
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 04-01-2002, 05:22 AM
  5. one fine day...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 11-15-2001, 06:45 PM