Thread: Help with strings

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    10

    Question Help with strings

    this code is a part of larger assignment

    all it's supposed to do is receive a number from player1...
    check is the number of digits matches up with the number asked in the beginning,
    then ask player2 for a number and check if it has the same number of digits.

    there are no errors in this code, but there are 7 warnings.
    all in rows that use gets() and strlen()

    the program runs, but doesn't do what it's supposed to.

    i'm obviously not using these functions correctly.
    what am i doin wrong ?


    Code:
    #include <stdio.h>
    #include <string.h>
    
    int dig_count, count, len;
    int p1in[256], p2in[256];
    main()
    {
                                    printf("\nHow many digits do you want(3 - 6) ? ->");
    				scanf("%d",&dig_count);
    				while(dig_count>6 || dig_count<3)
    				{
    					printf("Bad input, try again.\n");
    					scanf("%d",&dig_count);
    				}
    				
    				printf("Player #1: Enter your number ( %d digits): ->",dig_count);
    				gets(p1in);                                                                          //-!
    				
    				len=strlen(p1in);                                                                 //-!
    				 
    				while(len!=dig_count)
    				{
    					printf("Bad input, try again.\n");
    					gets(p1in);                                                                   //-!
    					len=strlen(p1in);                                                           //-!
    				}
    				printf("Player #2: Enter your guess ( %d digits, 0- to exit): ->",dig_count);
    				gets(p2in);                                                                             //-!
    				
    				len=strlen(p2in);                                                                     //-!
    				while(len!=dig_count && p2in!=0)
    				{
    					printf("Bad input, try again.\n");
    					gets(p2in);                                                                      //-!
    				}
    }
    the warnings all say "passing arg 1 of (whichever function is in the row) from incompatible pointer type"

    are the arrays being identified ans pointers ?

    what's goin on here ?
    Last edited by SymDePro; 05-27-2009 at 06:07 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1. DO NOT use gets() to read data from the user. It is completely unprotected. Use fgets() or some other "safer" function.

    2. You seem to need a function to read a string from the user with x digits - as you do the same thing twice. Repeating the same code twice or more is a bad idea.

    3. main returns int - so be explicit about that.

    4. This should presumably be char rather than int:
    Code:
    int p1in[256], p2in[256];
    as you are using it as a string.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    10
    this is for an assignment...

    with pretty strict rules.

    we're not supposed to use certain functions

    and we can't build functions.

    so i'm not sure i can follow all your advice.

    why is gets unsafe ?
    how does fgets work ?
    would it add a '\0' automatically?

    char instead of int fixed the warnings...

    but it's still not doing what it's supposed to.

    specifically, in the part after "p1 enter number"

    the program skips into the "bad input try again" loop without waiting for input...
    is it accepting an 'enter' from the last feed, like getchar does ?
    Last edited by SymDePro; 05-27-2009 at 06:22 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is "great" that you are taught to use functions that are unsafe, don't you think. It's like a motorcycle school saying "Well, you are beginners, so you don't need to wear helmets or protective clothing - just ride in your T-shirt, shorts and flip-flops".

    gets is unsafe because there is no way to tell gets how long the area of storage that it is supposed to save the string into actually is. So if you type in a long string, it will simply overwrite whatever memory happens to be AFTER the storage given. This leads to crashes in a "good" case, and exploits (where someone can take over the program and/or the machine altogether) in a bad case.

    fgets is very similar to gets, except it takes two extra parameters: a FILE * for which file you want to read from - when using it as a gets replacement, that'd be the standard input file (stdin), and a length of the buffer (storage). So the typical way to do this would be:
    Code:
    fgets(p1in, sizeof(p1in), stdin);
    There is one other difference, which complicates matters a little bit, and that is that the newline (enter) character that is used to end the line is also stored in the string, which is different from gets. So you need to get rid of it. The way to do that is:
    Code:
    char *p = strchr(str, '\n');
    if (p) *p = 0;
    For the moment, just treat that as a "magical cantation" that you have to do, and don't worry about what it means or how it works - it just works.

    The skipping is caused by the fact that you are mixing scanf() type input with gets() [and no, fgets doesn't help here]. The scanf() leaves a newline in the input buffer. It should be possible to get rid of that "as if by magic" by adding a space at the end of the format string. So use "%d " instead of the "%d" in your scanf line - I think that will work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM