Thread: Scanf messing up program

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    12

    Scanf messing up program

    Well I had to create a program to sort a bunch of string. And the user gets to choose how many strings we get to sort. Now the program works when I hard code the n, the number of strings but when I let the user do it but using scanf, the problem arises when the gets() function skips the first reading in. Here is a shot of the code and problem:

    http://i67.photobucket.com/albums/h3...nV2/screen.jpg

    Does any know why this is happening?

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    28
    put a getchar(); before starting the for loop to clear terminating newline ('\n' or ENTER ) from the input stream
    Last edited by mrbains; 11-05-2010 at 01:05 PM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    thank you! it worked!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Consider it an opportunity to learn how to use fgets() to read a WHOLE line, then parse the result from memory using whatever functions you like.
    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. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM