Thread: my program skips scanf and getchar()

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    32

    my program skips scanf and getchar()

    this function suppose to get input from user and go to correct case and calculate number of items. but It skips user input and keeps says invalid input.

    This is my output looks like:
    1. Big Mac, 2. Fries , 3. ChickenNuggets, 4. FiletOFish, 5.Soda
    Invalid command!

    Code:
    void add()
    {
    	char c;
    	int i;
    
    	printf("\nPlease Enter an Item:\n");
    	printf("\n1. Big Mac, 2. Fries , 3. ChickenNuggets, 4. 	FiletOFish, 5.	Soda"); 
    
                   c=getchar();
    
    switch(c)
    {
    case '1'
    i=1+1;
    break;
    
    default:
    printf("\nInvalid Command!\n\n");
    break;
    }

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    5
    Unfortunately, I don't have my compiler here - i would insert

    fflush(stdin);

    before c=getchar();
    because getchar() reads the input stream and you may have a value other than 1 in the buffer.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >c = getchar();
    Add another call to getchar after this to eat the newline. This is the simplest solution:

    c = getchar();
    getchar(); /* Eat newline */

    >Unfortunately, I don't have my compiler here - i would insert fflush(stdin);
    I would refrain from answering until you've been here long enough to know right from blatantly undefined.

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

  4. #4
    Registered User planet_abhi's Avatar
    Join Date
    Oct 2002
    Posts
    92
    remove the QUOTES from case

    i.e. type case 1 insted of case '1'
    AbHHinaay

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i.e. type case 1 insted of case '1'
    Then it would be sure to fail because you would be testing for an unprintable character. How about we avoid answering questions without checking to see if it works first?

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

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    5
    Thanks very much - fflush is only defined for stdout, NOT stdin per ANSI. I didn't know that. Fortunately I am brand new and haven't posted before. And I won't again
    Originally posted by Prelude
    >c = getchar();
    Add another call to getchar after this to eat the newline. This is the simplest solution:

    c = getchar();
    getchar(); /* Eat newline */

    >Unfortunately, I don't have my compiler here - i would insert fflush(stdin);
    I would refrain from answering until you've been here long enough to know right from blatantly undefined.

    -Prelude

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >And I won't again
    You can post, but it saves face for you if you make sure you're correct before doing so.

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

  8. #8
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    for an easy code, try this ...
    scanf (" %c", &<variable>);
    OR
    scanf ("\n%c", &<variable>);

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    32

    still..

    scanf("\n%c", &variable> worked for other function..
    but this function skips scanf again~!!!
    when I used fflush(stdin), it worked on visual C++, but didn't work with other compilers.
    I also tried c = getchar();
    getchar();
    but same result.
    How can I fix this?


    output1:
    Enter Firstname:
    JOHN
    selectmenu:
    Invalid Command!

    Output2:
    Enter Firstname:
    JOHN1
    selectmenu:
    Invalid Command!
    Please enter numberof items:

    Enter Firstname:
    selectmenu:
    Please enter number of items:2

    Code:
    void neworder()
    {
    	char name[25];
    	char ch;
    	int i;
    	
    
    	
    	printf("Enter Firstname: \n");
    	scanf ("\n%c", &name);
    	printf("Select Menu: \n");
    	scanf("\n%c", &ch);
    
    	switch(ch)
                     {   
    
                case '1': 
    	printf("\nPlease Enter Number of items: \n");
    	scanf("%d", &i);
    	break;
    
                     default:
    	printf("\nInvalid Command!\n\n");
    	neworder();
    	break;
                    }
    }
    Last edited by jk81; 11-27-2002 at 10:22 PM.

  10. #10
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    erm.... seem that scanf ("\n%c", ...) can't work with the program. how about this ..? i didn't check for this code anyway

    ...
    while (getchar() != \n);
    ...
    ..
    ..

    good luck.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >when I used fflush(stdin), it worked on visual C++, but didn't work with other compilers.
    Don't use fflush(stdin), ever. VC++ allows it, but that doesn't make it any more right than void main.

    >but this function skips scanf again~!!!
    Yes, scanf does this often. It's one of the reasons that everyone with much C experience avoids that function. Until you know what calls to scanf will have this problem you should clean up after it every time. Here is a useful function in many cases:
    Code:
    void tidy ( FILE *in )
    {
      int ch;
    
      while ( ( ch = getc ( in ) ) != EOF && ch != '\n' )
        ;
    }
    You can call it like this:

    tidy ( stdin );

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

  12. #12
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    i finally realise that stdin can be written inside of the function prototype anyway. LOL thanks prelude.

  13. #13
    Registered User
    Join Date
    Sep 2002
    Posts
    32

    now..

    now it takes ch but... skips scanf("%c", &name) before ch=getchar()
    I tried to use gets, fgets instead of scanf, but same result.

    Output:
    Enter Firstname: <- skips Scanf and goes to getchar()
    SelectMenu: 1
    Plese Enter Number of Items:

    Code:
    void neworder()
    {
    	char name[25];
    	char ch;
    	int i;
    	int j=0;
    	
    	printf("Enter Firstname: \n");
    	scanf("%c", &name);
    	printf("Select Menu: \n");
    	ch=getchar();
    
    
    	
    	switch(ch)
    	{
    	case '1': 
    	printf("\nPlease Enter Number of items: \n");
    	scanf("%d", &i);
    	break;
    	
    	default:
    	printf("\nInvalid Command!\n\n");
    	neworder();
    	break;
    	}
    enqueuedata(name, i);
    
    }

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >now it takes ch but... skips scanf("%c", &name) before ch=getchar()
    Code:
    void tidy ( FILE *in )
    {
      int ch;
    
      while ( ( ch = getc ( in ) ) != EOF && ch != '\n' )
        ;
    }
    
    void neworder()
    {
      char name[25];
      char ch;
      int i;
    
      printf("Enter Firstname: \n");
      scanf ("%s", name);
      tidy ( stdin );
      printf("Select Menu: \n");
      scanf("%c", &ch);
      tidy ( stdin );
      
      switch(ch)
      {
      case '1':
        printf("\nPlease Enter Number of items: \n");
        scanf("%d", &i);
        tidy ( stdin );
        break;
      default:
        printf("\nInvalid Command!\n\n");
        neworder();
        break;
      }
    }
    -Prelude
    My best code is written with the delete key.

  15. #15
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    prelude, can you please give me a link to or a brief description of why scanf causes this mess. does the function leave data within the buffer and then try to write over the existing data hence requiring it to be cleaned after every use to avoid the problem?

    Also, does the gets and other input functions have similar voulnerabilities?

    any links/suggestions much appreciated
    twans

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Look at my crappy program. Look at it!
    By Hirumaru in forum C Programming
    Replies: 18
    Last Post: 03-12-2009, 01:17 AM
  2. scanf skips lines of code in DOS using Dev-C++ and Windows XP
    By jenovanomusuko in forum C Programming
    Replies: 9
    Last Post: 12-21-2008, 03:10 AM
  3. problem w/ doubles in friend's program
    By mkylman in forum C Programming
    Replies: 16
    Last Post: 11-22-2008, 10:45 AM
  4. First scanf() skips next scanf() !
    By grahampatten in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:47 AM
  5. Replies: 2
    Last Post: 11-10-2003, 09:12 PM