-6

I am trying to read 1 character and 2 integers without success.

C code:

char action; int cr,cc;
printf("Enter 'c <row> <column>' to click on a block.\n");
scanf("%c %d %d",&action,&cr,&cc);
printf("You typed: %c %d %d\n",action,cr,cc);

Terminal output: ( EXAMPLE )

Enter 'c <row> <column>' to click on a block.
c 3 3
You typed: 
 0 0

I compiled it with gcc (Ubuntu 5.3.1-14ubuntu2.1) 5.3.1 20160413 in Ubuntu 16.04 LTS.

GeoMint
  • 169
  • 1
  • 13
  • Please include your variables' declaration – Fazlin Jul 15 '16 at 06:36
  • 2
    try `scanf(" %c %d %d",&action,&cr,&cc);` – BLUEPIXY Jul 15 '16 at 06:37
  • http://stackoverflow.com/questions/38343113/why-is-the-below-code-not-scanning-all-inputs-properly#comment64105936_38343113 – Mohit Jain Jul 15 '16 at 06:40
  • 4
    My crystal ball tells me the code executed *before* this performed a read from the input stream for *something*, but failed to consume the whitespace that immediately followed. It was then consumed by the `%c` (note the offset of your output to your first `0`), then the subsequent attempts to read to `int` both failed as the first tried to process an `int` from a `c`. And, of course, you assumed they worked because you never checked the result of your `scanf` call. VTC for lack of a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve). – WhozCraig Jul 15 '16 at 06:45
  • 1
    @BLUEPIXY a space between the %c made it work. thanks – GeoMint Jul 15 '16 at 06:45
  • 1
    Why nobody mentioned about checking the return value of `scanf()`? :( – Sourav Ghosh Jul 15 '16 at 07:18

2 Answers2

1

I guess you have not declared your variables properly

char action;
int cr, cc;

Here is the same code: http://ideone.com/spf5Vu

jayeshsolanki93
  • 2,096
  • 1
  • 20
  • 37
0

Without having seen your entire code, I suggest you try:

#include<stdio.h>

int main(int argc, char *argv[])
{
        int cr, cc;
        char action;
        printf("Enter 'c <row> <column>' to click on a block.\n");
        scanf("%c %d %d",&action, &cr, &cc);
        printf("You typed: %c %d %d\n", action, cr, cc);
}

This should work.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
inzanez
  • 366
  • 1
  • 16