Thread: Basic Scanf question

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    1

    Basic Scanf question

    Hi I hope I'm posting in the right forum but I'm having trouble with a Computing 1 homework assignment. Were supposed to have the user enter an integer: 100, and keep them in a while loop until they post 100.

    Here's my program:

    Code:
    #include <stdio.h>
    int main(int argc, const char * argv[]) {
        int number;
        
        printf("Please enter integer 100: ");
        int numOfConversion = scanf("%d", &number);
        
    
    
        while (numOfConversion == 0 || number != 100)  {
                printf("Please enter integer 100: ");
                numOfConversion = scanf("%d", &number);            
                }
    
    
        printf("I got the number: %d\n", number);
        return 0;
    }
    It works for integers but when I enter anything but an integer it just repeats "Please enter integer 100: " over and over. According to the lectures its supposed to work? Apparently its supposed to work using just a while loop and I cant find anything that fixes this problem using one while loop. Any help is much appreciated and thanks in advance!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Inside your while loop, you need something like

    Code:
    {
      int ch;
      while ( (ch=getchar()) != EOF && ch != '\n' ) { }
    }
    Like so
    Code:
        while (numOfConversion == 0 || number != 100)  {
                {
                  int ch;
                  while ( (ch=getchar()) != EOF && ch != '\n' ) { }
                }
                printf("Please enter integer 100: ");
                numOfConversion = scanf("%d", &number);            
                }
    When scanf fails, it doesn't consume the input at the point of failure.
    So any bad character will still be there unless you do something to remove it. That is what the while getchar loop is doing - cleaning up the input stream to reach a convenient point to try again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. N00b @ C: Code is failing at line 12 which is a basic scanf
    By Mike Garber in forum C Programming
    Replies: 3
    Last Post: 09-01-2013, 10:25 AM
  2. Question about scanf
    By dayanike in forum C Programming
    Replies: 4
    Last Post: 03-24-2012, 07:14 AM
  3. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  4. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  5. Basic Scanf() help
    By DooberXL in forum C Programming
    Replies: 7
    Last Post: 10-11-2004, 11:02 PM

Tags for this Thread