Problem With C/C++ "fgets" on eclipse, Help !

Discussion in 'Programming' started by programmer_best1, Apr 17, 2013.

  1. #1
    Hi,
    i have installed gcc & eclipse on windows.
    when i use fgets, the program doesn't debug until i write the fget value on the console.

    code:-
    #include <stdio.h>
     
    enum { max_string = 127 };
    static char string[max_string + 1]="";
     
    int main(int argc, char ** argv ){
        printf("type string: ");
        fgets(string, max_string, stdin);
        printf("your string is %s\n", string);
        return 0;
     
    }
    
    Code (markup):
    this is a simple c code to print "type string:", then fgets reads input to print it later.
    so the right sequence of this program supposed to be like this:-
    1. print "type string:" on console
    2. fgets reads input and store it on "string" variable
    3. print the "string" value

    now the problem is the program runs on this sequence.
    1. blank console page without printing anything and fgets reads input first
    2. print everything written before fgets and after it

    any help ?
     
    programmer_best1, Apr 17, 2013 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    Try it with scanf instead:

    
    #include <stdio.h>
     
    enum { max_string = 127 };
    static char string[max_string + 1]="";
     
    int main(int argc, char ** argv ){
        printf("type string: ");
        scanf("%127s", max_string);
        printf("your string is %s\n", string);
        return 0;
    }
    
    Code (markup):
     
    ThePHPMaster, Apr 18, 2013 IP