![]() |
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. Code:
#include<stdio.h> |
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. |
Re: compiler missing my FGETS function. HELP PLEASEEEE
//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; } |
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 Code:
if (op[0]=='i')Code:
if (op[0]=='i')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;Code:
char op[32]; |
| All times are GMT +5.5. The time now is 03:32. |