Thread: Want User Input but Program Ends

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    Want User Input but Program Ends

    Hi,
    I'm working through O'Reilly's Practical C Programming, just for fun, and one of the exercises in section 5 is to write a program to convert °C to °F and vice versa. Here's what I have so far..

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char choice[2] ; // which conversion do I want ? 
    int userChoice ; // the int to pass to the if .. then 
    char input[11] ; // the number to be converted
    float temp ; // the temperature to start with
    float result ; // the result
    
    int main () 
    
    {
    
      printf ("\n(1) - Celsius - Fahrenheit ") ;
      printf ("\n(2) - Fahrenheit - Celsius\n\n") ;
      fgets (choice, sizeof(choice), stdin) ;
      sscanf (choice, "%d", &userChoice) ;
    
      if (userChoice == 1)
      {
    	printf ("\n\nPlease enter temperature in degrees Celsius : ") ;
    	fgets (input, sizeof(input), stdin) ;
    	sscanf (input, "%f", &temp) ;
      }
    
      return 0 ;
    
    }
    Now, there are undoubtedly far more elegant ways of doing this, but for the moment, I'm right at the basics. What I can't understand is that the program compiles without any errors, it asks the user to choose which conversion he wants.. and then prints out the Please enter temperature in degrees Celsius : and exits.

    Why? I don't get it. The fgets is the same format as the previous time when input was asked for, and yet bang - it exits.

    Help !

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your first fgets read will read at most one character from the input. Since you have two characters ('1' and 'enter key') the last one is still there when your next fgets happens.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Your choice variable's array isn't long enough. fgets will null terminate the string automatically, so the newline character is left behind in the buffer because there's no room for it. In other words, after the first fgets your choice array looks like

    Code:
    -------------
    |  1  |  \0 |
    -------------
    and the stdin buffer still has a \n in it, which the next fgets will happily take as input. If you increase the size of the choice array, then you'll end up with

    Code:
    --------------------
    |  1  |  \n |  \0  |
    --------------------
    And the second fgets will be waiting for the user to input something.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    Quote Originally Posted by tabstop View Post
    Your first fgets read will read at most one character from the input. Since you have two characters ('1' and 'enter key') the last one is still there when your next fgets happens.
    Ha! *slaps forehead*

    Code:
    fpurge (stdin) ;
    .. did the trick.

    Thanks!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    fpurge() isn't a standard function.

    If you have
    char buff[BUFSIZ];
    then you generally don't have to worry about whether Dirty Harry types in 6 characters or only 5, or worry that he can't remember in all the excitement. You as a programmer no longer have to "feel lucky" when it comes to choosing the buffer size.

    Trying to be overly precise on the input buffer size just leads to random bodge code to fix it.
    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.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    Thumbs up

    Quote Originally Posted by Salem View Post
    fpurge() isn't a standard function.

    If you have
    char buff[BUFSIZ];
    then you generally don't have to worry about whether Dirty Harry types in 6 characters or only 5, or worry that he can't remember in all the excitement. You as a programmer no longer have to "feel lucky" when it comes to choosing the buffer size.

    Trying to be overly precise on the input buffer size just leads to random bodge code to fix it.
    Thanks for these tips. I'm supposing that the book will mention char buff later on. I'm still at that stage where even a missed semicolon takes ages for me to notice. I just did another exercise in the book - to calculate the square of the distance between two points, and my first char initialization looked like char[100] distance1. Dang, I had to sit and stare at it for about an hour before I saw it!

    Thanks again for your advice - much appreciated. :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program doesn't wait for user input!
    By gaurav_13191 in forum C Programming
    Replies: 8
    Last Post: 07-13-2011, 08:25 AM
  2. Replies: 3
    Last Post: 10-12-2010, 01:40 PM
  3. Replies: 7
    Last Post: 08-23-2008, 07:44 AM
  4. Replies: 4
    Last Post: 04-03-2008, 09:07 PM
  5. user input and program design
    By Chaplin27 in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2005, 08:53 AM