Like Tree1Likes
  • 1 Post By Salem

Want User Input but Program Ends

This is a discussion on Want User Input but Program Ends within the C Programming forums, part of the General Programming Boards category; Hi, I'm working through O'Reilly's Practical C Programming , just for fun, and one of the exercises in section 5 ...

  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,185
    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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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.
    anduril462 likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21