Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   compiler missing my FGETS function. HELP PLEASEEEE (http://www.go4expert.com/forums/compiler-missing-fgets-function-help-t26224/)

jose_peeterson 6Jul2011 20:46

compiler missing my FGETS function. HELP PLEASEEEE
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.

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];

 printf("Enter Player Name : ");
// scanf("%c",user_name); // both input methods are not compiled


xpi0t0s 7Jul2011 12:24

Re: compiler missing my FGETS function. HELP PLEASEEEE
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.

jose_peeterson 8Jul2011 10:09

Re: compiler missing my FGETS function. HELP PLEASEEEE
//thank you, ok the welcome(); is

void welcome()
char op ; // opereation
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");

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("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");


xpi0t0s 8Jul2011 10:39

Re: compiler missing my FGETS function. HELP PLEASEEEE
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

if (op[0]=='i')
  printf("OH DEAR etc");

By the way you don't need so many returns. You don't need any, in fact. Or the else clause:

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

char op;
char 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

char op[32];
if (op[0]=='i')...

All times are GMT +5.5. The time now is 23:01.