Results 1 to 6 of 6

Thread: Problems with scanf?

  1. #1
    Join Date
    Oct 2006
    Posts
    9
    Rep Power
    0

    Default Problems with scanf?

    I'm writing this program and it works, however it asks me to type inputs in the wrong places... here is the code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
        char name[100];
        int height,weight,comparison;
        
        printf("Please enter the name of the criminal.\nEnter end to terminate the program.\n");
        scanf("%99s",&name);
        //printf("test\n"); 
        comparison = strcmp(name,"end");
    
        while(comparison!=0)
        {
              printf("\nPlease enter the height(in cm) of the criminal. ");
              scanf("%i",&height);
              
              printf("\nPlease enter the weight(in kg) of the criminal. ");
              scanf("%i",&weight);
              
              if(height < 165 && weight > 90)
              {
                    printf("%s is a suspect.\n", name);
              }
              else
              {
                    if(height > 190 && weight < 65)
                    {
                          printf("%s is a suspect.\n", name);
                    }
                    else
                    {
                          printf("%s is not a suspect.\n", name);
                    }
              }
              
              printf("\nPlease enter the name of the criminal.\nEnter end to terminate the program.\n");
              scanf("%99s",&name);
              
              comparison = strcmp(name,"end"); 
        }
        
        //printf("the name is %s\n", name);
        system("pause");
        return 0;
    }

    and here is a screenshot of the cmd copiled

    Please Help..

  2. #2
    Join Date
    May 2003
    Posts
    229
    Rep Power
    0

    Default

    I believe that this problem is being caused by the buffering
    Try flushing the standard output stream after each printf.
    Like this fflush(stdout);

    Another thing you might think about is to put a "\n" or other whitespace character at the end of your scanf format strings to make scanf read the whitespace after the user input (e.g. the carriage return that the user entered after the input). For example, you could use

    scanf("%99s\n",&name);

    instead of

    scanf("%99s",&name);

  3. #3
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Also try
    scanf("%99s", name);
    instead of
    scanf("%99s",&name);
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  4. #4
    Join Date
    Oct 2006
    Posts
    9
    Rep Power
    0

    Default

    ye well guys, i got it 2 work although i didn't use any of ur methods. Thanks anyways and check out another post of mine that i'm about 2 put up

  5. #5
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default

    if u r scanning strings or chars, dont use the ampersand &. just scanf("%s", name);

    also if u r scanning strings u may use gets(name);

  6. #6
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    use fgets, so u can specify the max length as you were doing with scanf
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •