Like Tree2Likes
  • 2 Post By laserlight

code not accepting input for first string

This is a discussion on code not accepting input for first string within the C Programming forums, part of the General Programming Boards category; In this code, its not accepting input for first string. I mean its directly asking input from second string.. why ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    113

    code not accepting input for first string

    In this code, its not accepting input for first string. I mean its directly asking input from second string.. why ?
    Code:
    # include <stdio.h>
    # include <string.h>
    
    struct reverse
    {
      char str[100];
      char temp_str[100];
    };
    
    int main(void)
    {
      int i,cases;
      struct reverse s[100];
     
      printf("enter strings cases");
      scanf("%d",&cases);
      for (i=0; i<cases; i++)
      { 
        printf("\ncase: %d", i+1);
        fgets(s[i].str, sizeof (s[i].str), stdin);
        printf("\n--");
      }
      
      printf("your output\n");
      for(i=0; i<cases; i++)
      { 
        printf("case:%d",i+1);
        fputs (s[i].str, stdout);
      }
    }
    output :
    PHP Code:
    enter strings cases3

    case: 1
    --
    case: 
    2e

    --
    case: 
    3e

    --your output
    case:1
    case:2e
    case:3e 

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,258
    This is a similiar problem as in Should be an easy C fix!, hence the solution is similiar.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    @laserlight: I got what you were saying in the reply that you quoted..
    but the thing is I never left any space or new line in the input buffer so that its taking for the next one..
    > should I use fflush.. to flush the buffer ?
    > can you tell me the simplest possible way. please.
    Last edited by suryak; 07-05-2011 at 02:45 AM.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    fgets() is still going to stop at the newline character which you still left in your buffer from your initial scanf(). Take another look at the thread Laser posted.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    134
    The solution of your problem is explained already in other thread. Also it is perfectly OK in case of yours to use fflush(stdin) after scanf.
    S_ccess is waiting for u. Go Ahead, put u there.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,258
    Quote Originally Posted by maven
    Also it is perfectly OK in case of yours to use fflush(stdin) after scanf.
    No, it is not OK at all.
    AndrewHunter and suryak like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    134
    Quote Originally Posted by laserlight View Post
    You are right. I missed it!
    S_ccess is waiting for u. Go Ahead, put u there.

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    Though I couldn't get what you are saying but could manage to fix the problem..
    I used getchar() .. its working. Thanks.

    Can you tell me in detail in this code what exactly is happening..
    @hunter: why would \n be present in input stream after scanf(), I mean its not a string to get placed by default. ?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by suryak View Post
    I mean its not a string to get placed by default. ?
    You put it there when you hit enter, and you didn't tell scanf to remove it.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    printf("enter strings cases");
    scanf("%d",&cases);
    You enter a number and then press the "enter" key. The scanf function reads and converts the number value and stores it in the variable cases. It stops converting once it reaches the newline/enter key and leaves that part of your input in the input buffer to be processed at a later time. The very first fgets in your loop sees this newline character in the input buffer and consumes it as if you had simply pressed the "enter" key at that point as part of a blank/empty line. Subsequent fgets calls in that loop however work as expected because there is no newline key in the buffer at the point the code is reached.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    @hk : I got what you are saying but when I used fflush(stdin) .. why it didn't go ?

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Read this FAQ article for explanation and solution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C compiler accepting c++ code!!
    By suryap.kv1bbsr in forum C Programming
    Replies: 4
    Last Post: 03-22-2011, 09:35 AM
  2. Accepting formatted input
    By fxtdr79 in forum C Programming
    Replies: 15
    Last Post: 06-28-2010, 02:25 PM
  3. Problems with fgets not accepting input from stdin
    By k2712 in forum C Programming
    Replies: 7
    Last Post: 08-26-2007, 12:33 AM
  4. Accepting Multiple Types of Input
    By Junior89 in forum C++ Programming
    Replies: 2
    Last Post: 03-07-2006, 11:25 PM
  5. accepting input while writing to screen
    By variable in forum C Programming
    Replies: 17
    Last Post: 02-06-2005, 10:14 PM

Tags for this Thread


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