1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

compiler missing my FGETS function. HELP PLEASEEEE

Discussion in 'C' started by jose_peeterson, Jul 6, 2011.

  1. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    hello experts,
    in the following code after welcome(); is executed the program quits.
    welcome function just has some text and an if statement so if op = instructions it displays some instructions else just enters
    so when i press instruction in the welcome(); the fgets is ignored and so does not read in
    help pleaseeeeeeeeeeeeeeee.
    Code:
    #include<stdio.h>  
    #include<stdlib.h>                   
    struct player
    {
     char name[20];
     int score;
    
    };
    
    void welcome();
    void rand_mines(char msweep[][]);
    void printmatrix(char msweep[][],int r,char user_chart[][]);
    int process(char msweep[][],int r,int c,char user_chart[][]);
    
    void main()
    
    {                   
    
    char msweep[6][6] = {{'0'}};      
    int var,row,column;  
    char user_chart[6][6] = {{'0'}};
    char user_name[20];
    
     welcome();
     
     printf("Enter Player Name : ");
    // scanf("%c",user_name); // both input methods are not compiled
     fgets(user_name,20,stdin);
    return;
    
    }
     
    Last edited by a moderator: Jul 6, 2011
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    It's a classic debugging error to decide the problem can't possibly be in a certain place.

    OK, let's say you're correct. Comment out the welcome() call and put something like "printf("Welcome call was here\n"); immediately after it. Does it still go wrong?

    Unfortunately you forgot to post the welcome() code so I can't check you're correct.
     
  3. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    //thank you, ok the welcome(); is

    void welcome()
    {
    char op ; // opereation
    printf("\n");
    printf(" // COPYRIGHT Jose Peeterson - 2011 \\\\\n\n");

    printf("Welcome to MINESWEEPER in C >>.....\n");

    printf("Enter <<\n");
    printf(" i for instructions\n");
    printf(" any other key to enter game\n");
    scanf("%c",&op);

    if(op == 'i')
    {
    printf("OH DEAR, what a shock you are unfortunatly in the midst of a\n");
    printf("mine field.\n");
    printf("The 8 coordinates surounding a cell can have mines.\n");
    printf("when a coordinate is typed the number of mines surrounding \n");
    printf("that cell is shown. e.g. when 5 is seen in the cell of the \n");
    printf("chosen coordinate it means that 5 mines are likely in the \n");
    printf("first neighbour cell in N,NW,NE,E,W,S,SW,SE,the eight \n");
    printf("directions,NOW\n");
    printf("Enter the coordinates of the x and y plane between 1 to 4\n");
    printf("Are you destined to DIE or Live ?\n");
    printf("HA ha ha hah, GOOD LUCK\n\n");
    return;
    }
    else
    return;

    return;
    }
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Please use code blocks when posting code.

    The problem, as always, is scanf. This is a REALLY SHIT way of getting user input. I honestly cannot fathom why teachers insist on teaching it.

    Change op to a character array and use fgets. Then
    Code:
    if (op[0]=='i')
    {
      printf("OH DEAR etc");
      return;
    }
    
    By the way you don't need so many returns. You don't need any, in fact. Or the else clause:
    Code:
    if (op[0]=='i')
    {
      // display instructions
    }
    } // end of function close bracket.
    
    If you're still wondering why it doesn't work as you originally wrote it, here's the tech explanation. scanf scans user input converting what it finds and placing it in the specified variables. Now unfortunately, you can't just enter a single character; you have to press RETURN before the input is processed. So the input stream if you enter i<RETURN> comprises the letter i and the return character. scanf finds the i, matches that with the single character spec %c and puts that into op. It leaves the rest of the input buffer untouched. So when your fgets function starts, it finds the RETURN character that scanf "helpfully" left on the input stream and takes that as the user input.

    The best solution to this is to drop scanf ALTOGETHER and NEVER use it at all. Use fgets to read a string from the user, then you can always use sscanf if you want - that is fine, because there will be no confusion. sscanf is like scanf but reads from a string instead of from stdin. In this case though no sscanf is needed - you only want to see if the first character entered was an i so that you can display the instructions.

    There is a way to get scanf to process the RETURN - the easiest is just to do something like
    Code:
    char op;
    char dummy;
    scanf("%c%c",op,dummy);
    
    which is fine until the stupid user enters "no, sod off<ENTER>", at which point scanf will set op to 'n', dummy to 'o', and leave ", sod off<ENTER>" for fgets. You could scanf in a loop until you hit ENTER, but at that point it really becomes a lot simpler just to do
    Code:
    char op[32];
    fgets(op,32,stdin);
    if (op[0]=='i')...
    
     
    Last edited: Jul 8, 2011

Share This Page