2

so i have this code, which is supposed to get coordinates from user:

#include <stdio.h>
#include <time.h>

int main() {
int number;
char letter;
int points = 3;
    while(points < 8){
        printf("give me first coordinate (letter)");
        scanf("%c",&letter);
        printf("give me second coordinate (number)");
        scanf("%d",&number);
    }
}

as far as i know, this should keep taking coordinates from a user, but instead it takes it only once, and then crush in a really weird way, like it's skipping scanf without any reason. here's my output:

give me first coordinate (letter)a
give me second coordinate (number)1
give me first coordinate (letter)give me second coordinate (number)12
give me first coordinate (letter)give me second coordinate (number)df
give me first coordinate (letter)give me second coordinate (number)give me first coordinate (letter)give me second coordinate (number)sss

I feel really confused, since this is simple code, and i don't have the slightes idea whats causing this. anybody?(if it makes any difference, my system is mountain lion)

Leo
  • 2,061
  • 4
  • 30
  • 58
  • 4
    Think about how you're handling the newlines present on the input. Hint: you're not handling them at all right now. – Carl Norum May 08 '13 at 18:36
  • You're ignoring the result of the input operation. That's a fatal programming error. – Kerrek SB May 08 '13 at 18:39
  • @KubaPolaczek Mistakes are made by all... but how will you avoid making this mistake in the future? In my mind, this question is mostly about `scanf`. I'd start by reading the `scanf` manual carefully, over and over until I understand all of it... For future reference, opengroup gives a good description of C standard library functions. You can find the opengroup scanf manual by googling "opengroup scanf". – autistic May 08 '13 at 19:01

3 Answers3

7

One possible solution is to add a space to skip whitespace:

scanf(" %c",&letter);
       ^

As user "undefined behavior" properly pointed out, you should also check the return value. In this case you expect the return value to be equal to the number of items you are reading, if the return value <0 then you can't read from stdin anymore and a return value less than the number of items you are reading in indicates you have a conversion error.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • 1
    Make sure you check the return value. Negative values indicate that `stdin` can't be read from any more, and any return value less than the number of items expected to be read (in this case, 1) indicates that a conversion error occured (eg. the scenario described by @V-X below: "*if you enter "a" to scanf("%d",&i), you'll never get any results...*") – autistic May 08 '13 at 18:57
4
scanf(" %c",&ch);

The reason this works is that the first space flushes stdin and then lets the user input the character.

this also works if you put /n or /t before %c

Andrew Schuster
  • 3,229
  • 2
  • 21
  • 32
Lakshman Diwaakar
  • 7,207
  • 6
  • 47
  • 81
-1

I personally don't like usage of scanf. This function leaves the input stream in not so well defined state as it would look. I prefer fgets combined with sscanf...

V-X
  • 2,979
  • 18
  • 28