3

Im new to C and cant for the life of me work out what im doing wrong here. The first scanf works fine, variables are printed out as they are read in. The second scanf doesn't seem to be reading the input correctly. Input is of the format "char int int" i.e. b 4 4
when i print opb x and y out, opb = " ", x = 13238272 , y=0. Any Ideas?.....note ive cut out code below the problem

int main(void) { /*initialize variables*/ int width, height; char op; /*grid input*/ scanf("%c %d %d", &op, &width, &height); /*check conditions*/ if (op != 'g' || width>100 || height>100 || width*height<10 || width<1 || height<1) { printf("grid-error\n"); return 0; } /*initialize grid elements*/ int grid[width][height]; char printGrid[width][height]; /*create grid elements*/ int i, j; for (i=0; i<height; i++) { for (j=0; j<width; j++) { grid[j][i] = 0; printGrid[j][i] = '*'; } } /*print successful creation*/ printf("%c %d %d \n", op, width, height); int k; for (k = 0; k<10; k++) { /*initialize variables*/ int x, y; char opb; /*mine input*/ scanf("%c %d %d", &opb, &x, &y); /*check conditions*/ if (opb != 'b' || x<0 || y<0 || x>(width-1) || y>(height-1) || grid[x][y] == 9) { printf("mine-error\n"); return 0; } 
5
  • You should check the return value of scanf(), because it's the only way you'll know if there's a format error. Commented Mar 23, 2011 at 6:04
  • 1
    yer appears theres a format error (as it didnt return 3), whats going on here? Commented Mar 23, 2011 at 6:13
  • 1
    @user445559: One thing that occurs to me right off is that %c will read the next available character — even if it's a space or the newline left over from reading the previous line. In general it's safer to read input as lines and use sscanf() to parse them. Commented Mar 23, 2011 at 6:15
  • the only problem is weve been instructed to use scanf. So im just manually catching bad input Commented Mar 23, 2011 at 22:57
  • Not to sound like a grumpy old geekosaur or anything, but if that's what they're teaching these days.... Commented Mar 23, 2011 at 23:00

2 Answers 2

4

I suspect the problem is that you aren't dealing with newline characters in your input. The result is that the opb is actually a newline character (not a space, though it looks like one) and x and y aren't read at all (i.e. they retain the values they were initialised with).

To solve the problem, try adding the newline to both your scanfs. That is:

scanf("%c %d %d\n", &op, &width, &height); 

and later

scanf("%c %d %d\n", &opb, &x, &y); 
Sign up to request clarification or add additional context in comments.

3 Comments

thanks, I tried this but now it just hangs at the first scanf(), so with input: "g 10 10<return>" it goes to next line until i press ctrl+d, then it carries on as it should
Yes, I believe this is due to the way keyboard input is buffered. If you change the first scanf back to scanf("%c %d %d", &op, &width, &height); and the second scanf to scanf("\n%c %d %d", &opb, &x, &y); it will not hang. The point is to deal with the white space characters appropriately.
yep that was the issue, but i fixed it using a space for the following scans and ridding of the \n. i.e. scanf("%c %d %d", &op, &width, &height); and later scanf(" %c %d %d", &opb, &x, &y); thanks for everyones help.
1

I think the easiest would be to put space before %c in second scanf. If you use it without space, it will take first symbol. That means, newline. The space makes %c take first symbol that isn't space or tab. So:

scanf(" %c %d %d", &op, &x, &y); 

1 Comment

I don't believe that " %c" ignores whitespace.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.