Thread: little problem

  1. #16
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    >> /* here this -> scanf( "%d%*c", &num ); ?! */
    No, that scanf is a replacement for yours so your code would look like
    Code:
    do {
            [...]
            int i;
    
            printf( "Please choose > " );
            scanf( "%i%*c", &i );  
    
            switch(i)
            [...]
    
    } while ( i != 0 );
    [edit]
    It is not as safe as the first option, because if you enter 1d it will have the same problem as before. As it only reads 1 character the d is read and the newline is left in the buffer
    Last edited by C_Coder; 07-08-2002 at 12:36 PM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  2. #17
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    ok, i just replaced
    while( getchar() != '\n' );
    with
    scanf( "%i%*c", &i );
    and it works fine, too...

    but my next problem is inside the new_char(); function again :
    Code:
        [...]
        char ch_name[21];
    
        printf("Please enter the characters name:\n");
        fgets(ch_name, sizeof(ch_name), stdin);
    
        printf("Ok, %s, choose a profession!\n",ch_name);
        [...]
    after i entered a name, the program displays :
    Ok, Name
    , choose a profession!
    so i think it reads the "ENTER", too? right?
    or is this problem caused by something different?

  3. #18
    Registered User bubux's Avatar
    Join Date
    Jul 2002
    Posts
    14

    I think...

    Maybe this would work if you. But be carefull... If the name is 0 in lenght (''), it may generate an error... Ill explain it in 1 minute.. brb i gtg. But here it is:

    [quote]
    void new_char()
    {
    char ch_name[21];

    printf("Create character\n\n");

    printf("Please enter the characters name\n>");
    gets(ch_name);

    len = strlen(ch_name);
    if (ch_name[len-1] == '\n') ch_name[len-1] = '\0';

    printf("Ok, %s, what is your profession?",ch_name);
    }
    Last edited by bubux; 07-08-2002 at 05:59 PM.
    <VBCODE>
    While Bubux.State = "alive"
    Bubux.LoveGod = True
    Wend
    </VBCODE>

  4. #19
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    thanks bubux, i will try it...
    i think i include a check if ch_name is > ""
    and after this check i include your code...

  5. #20
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >void new_char()
    An empty parameter list means that any number of arguments may be passed, no checking is done. If you don't want to pass anything, specify void:

    void new_char ( void )

    >char ch_name[21];
    I'm sure The Magical Mister Mistoffelees will be sorry to hear that you limited your buffer to 21 characters.

    >gets(ch_name);
    Unless your buffer is infinitely long (which it isn't, for those who haven't been paying attention), this will break eventually. Use fgets instead.

    >if (ch_name[len-1] == '\n') ch_name[len-1] = '\0';
    There's no need to do this since gets will replace the newline with a nul anyway. With fgets this code is useful, but I prefer the shorthand way of doing it:

    ch_name[strcspn ( ch_name, "\n" )] = '\0';

    This way the newline will be replaced with a nul, and if there is no newline strcspn will return the index of the trailing nul, so no harm will be done.

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

  6. #21
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    thank you, prelude! the following code :
    ch_name[strcspn ( ch_name, "\n" )] = '\0';
    just did it!

    the buffer size :
    what do you think is an acceptable buffer size?
    in my c book they tell you to hold your buffers small
    what do you think about an buffer of size 100 (as long
    it isn't an array with [100][100][100]... that's big, huh?

    thanks @ everyone again for helping me with this "little problem"

  7. #22
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >what do you think is an acceptable buffer size?
    Just big enough for the data. If I have to define a static buffer then I'll use the BUFSIZ macro in stdio.h. Otherwise it's best to maintain a dynamic buffer which can increase its size if the data gets too big. For student code a buffer of 100 is fine, input will rarely go beyond that. For any real applications you need to be far more careful.

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

  8. #23
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    A long known problem. It is a residual key left in the keyboard buffer. To deal with this I have always used "gets" followed by a
    "sscanf". One of the nice side benefits of a "sscanf" is it clears the residual key in the keyboard buffer. Here is some code that should work.
    Note that an integer is taken in as a float and then converted to a
    float. This is done incase the user types something like 5.5g. The
    "sscanf" will truncate at the last legal number entered. If the user
    types g5.5 the "sscanf" will ignore it.



    #include <stdio.h>

    void main(void)
    {
    int x,n;
    float f;
    char input[25], string[25];

    // clear any residual key in the keyboard buffer
    sscanf(input, "%s", input);

    // get an integer
    printf("\n enter an integer <> ");
    gets(input);
    sscanf(input, "%f", &f);
    x=f;
    printf(" %d\n",x);

    // get a float
    printf("\n enter a float <> ");
    gets(input);
    sscanf(input, "%f", &f);
    printf(" %2.2f\n",f);

    // get a string
    printf("\n enter a character string <> ");
    gets(input);
    sscanf(input, "%s", string);
    printf(" %s\n",string);

    // you can take input all at once if each field is separated by a space
    string[0]='\0'; // clear the string array for the new entry
    x=n=f=0; // clear the variables
    printf("\n enter an integer <space> a float <space> and a string <>");
    gets(input);
    sscanf(input, "%d%f%s", &x,&f,string);
    printf(" %d %2.2f %s\n",x,f,string);

    // you can even count the number of inputs
    string[0]='\0'; // clear the string array for the new entry
    x=n=f=0; // clear the variables
    printf("\n enter an integer <space> a float <space> and a string <>");
    gets(input);
    n = sscanf(input, "%d%f%s", &x,&f,string);
    printf(" number of inputs = %d\n",n);

    // this "gets" is just to keep the user screen visable
    gets(input);
    }

  9. #24
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    @jerryvtts
    thanks i'll have a look at this...

  10. #25
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Jerry.. a few comments on your code....

    >Please use code tags when posting code (see my sig).

    >void main
    main should return an int.

    >sscanf(input, "%s", input);
    at the time you use input, it's uninitialised, therefore the results of this operation will be undefined. It also doesn't clear the keyboard buffer (I presume you mean stdin?) as your comments suggest.

    >gets(input);
    It's been discussed many time, gets() is bad... it allows buffer overflows.

    Welcome to the board, btw.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM