-
segmentation fault
Does anyone know why i get a segmentation fault when i call scanf() in guessSquare
function im not sure whether im calling it correctly?
Code:
int main(void)
{
/* Stores all hidden data about the minefield. */
char minefield[MAX_GRID][MAX_GRID];
/* A version of the minefield that only stores known information. */
char displayGrid[MAX_GRID][MAX_GRID];
int display;
char *rSquare;
unsigned *cSquare;
/* Number of cells in each row and column used. */
unsigned size = 0;
char inputType;
/* call functions according to the flow chart diagram*/
init(minefield, displayGrid);
size = getSize();
placeMines(minefield,size);
displayMinefield(displayGrid,size);
inputType = guessType();
guessSquare(rSquare, cSquare, size);
return EXIT_SUCCESS;
/*************************************************/
void guessSquare(char* row, unsigned* col, unsigned size)
{
int valid =0;
char *r = row;
unsigned *c = col;
do
{
valid = 0;
fscanf(stdin, "%c, %u", r, c);
if(*row == 'a')
{
valid = 1;
}
else if(*row == 'b')
{
valid = 1;
}
else if(*row == 'c')
{
valid = 1;
}
else if (*row == 'd')
{
valid = 1;
}
else if(*row == 'e')
{
valid = 1;
}
else if(*row =='f')
{
valid = 1;
}
else
{
printf("Incorrect row A - F !\n");
readRestOfLine();
}
}while(!valid);
}
}
-
You forgot to make your pointers point to anywhere.
-
where abouts in my main where im passing in the values to the parameters for the function?
-
Code:
char *r = row;
unsigned *c = col;
fscanf(stdin, "%c, %u", r, c);
It assigns address of row to r & col to c,if u still want to make scanf work then ommit those two assignments.Try using
Code:
fscanf(stdin, "%c, %u", row, col);
instead.
Thanks & regards
Vinit :)
-
i still get a seg fault in my code
-
> It assigns address of row to r & col to c,if u still want to make scanf work then ommit those two assignments
Simply copying a pointer to an undefined location doesn't make it a defined pointer.
In main, you need
char rSquare;
unsigned cSquare;
guessSquare(&rSquare, &cSquare, size);
-
i got rid of the seg fault now for some reason fscanf isnt taking the value properly because it wont evaluate the if statements properly it keeps printing error even though i type in the correct input
Code:
void guessSquare(char* row, unsigned* col, unsigned size)
{
int valid =0;
char r;
unsigned c;
r = *row;
c = *col;
printf("Enter a square from row A -F and Col 1-6\n") ;
do
{
valid = 0;
fscanf(stdin, "%c, %u", &r, &c);
if(r == 'a')
{
valid = 1;
}
else if(r == 'b')
{
valid = 1;
}
else if(r == 'c')
{
valid = 1;
}
else if (r == 'd')
{
valid = 1;
}
else if(r == 'e')
{
valid = 1;
}
else if(r =='f')
{
valid = 1;
}
else
{
printf("error\n");
break;
}
}while(!valid);
}
-
Don't use scanf / fscanf perhaps?
I dunno - why don't you check the return result of functions?
Using anything other than fgets() to read input is basically a waste of time IMO. It simply leads to more problems, and there's the whole "get rid of surplus newlines problem" - ick ick ick....
Here,
Code:
#include <stdio.h>
#include <stdlib.h>
void guessSquare(char *row, unsigned int *col, unsigned size);
int main(void)
{
char rSquare;
unsigned int cSquare;
unsigned int size = 0;
guessSquare( &rSquare, &cSquare, size);
printf( "%c %u\n", rSquare, cSquare );
return EXIT_SUCCESS;
}
/*************************************************/
void guessSquare(char *row, unsigned *col, unsigned size)
{
char buff[BUFSIZ];
int valid = 0;
while ( !valid &&
fgets( buff, sizeof buff, stdin ) != NULL ) {
if ( sscanf( buff, "%c, %u", row, col ) == 2 ) {
if ( *row >= 'a' && *row <= 'f' ) {
valid = 1;
}
}
}
}
-
ok iv tried that but why does it keep asking for input after i press enter
-
I've no idea - perhaps you didn't enter the required input - say check the caps lock key.
Do you have a log of the input and output ?
Try putting some additional code in - say some printf statements in the else part of each of those if statements.