-1

I've got a very simple code here:

char A[9];
for (int i = 0; i < 9; i++){
    scanf("%c\n", &A[i]);
}

for (int i = 0; i < 9; i++){
    printf("%c ", A[i]);
    if (i == 2 || i == 5 || i == 8){
        printf("\n");
    }
}

It is supposed to create a 1x9 array, then read 9 letters from a user (letter, enter, letter, enter and so on) and put them into an array. Then it should print out the array as if it was 3x3 array. It works as it should with one weird exception: the first loop takes 10 letters from the user, not 9, why?

I found some information that it might be caused by the "\n" next to the "%c" in the loop, but when I delete it and leave just "%c" the program reads only 5 letters cause it takes enter as another char.

I also tried using "%s\n" instead, but then the stack around A is corrupted for some reason and it still reads 10 letters instead of 9.

melpomene
  • 84,125
  • 8
  • 85
  • 148
Ania
  • 450
  • 4
  • 14
  • @StoryTeller yes well I would like to leave it letter, enter, letter, enter and so on, is this possible to do? – Ania Feb 25 '18 at 13:33
  • @StoryTeller also, this doesn't work either, the program wants one more letter followed by enter. – Ania Feb 25 '18 at 13:33
  • Have it consume **leading** white-space characters instead. Should do it. – StoryTeller - Unslander Monica Feb 25 '18 at 13:34
  • Then just don't give it more input. – user202729 Feb 25 '18 at 13:35
  • @StoryTeller oh right it works, though I'm not really sure why – Ania Feb 25 '18 at 13:36
  • @user202729 yeah well if I don't, then program will wait for me to do so, I don't want it to work somehow, I want it to work perfectly. – Ania Feb 25 '18 at 13:36
  • Gives it the EOF. – user202729 Feb 25 '18 at 13:37
  • 1
    If you want to answer your own question, post an answer. Don't edit your question. – melpomene Feb 25 '18 at 13:40
  • 2
    And if you want to avoid all this confusion, just [don't use `scanf`](http://c-faq.com/stdio/scanfprobs.html). – Steve Summit Feb 25 '18 at 13:44
  • @SteveSummit in my case, I've got very specific codes to write and it said to use scanf, so I had to figure out a way to do so. – Ania Feb 25 '18 at 13:56
  • @Ania I understand. Is this a class you're taking? Unfortunately most classes (and beginning C books) cruelly mislead you: they teach you to use `scanf`, as if it's useful, but it turns out it's just not. C programmers who know what they're doing don't use `scanf` for anything. (I mean it.) So spend as little time and effort as you possibly can learning `scanf`, and if you can afford to, just skip the assignments involving it, or solve them some other way, taking a 0 for your score if need be. (This sounds like heretical advice, I know, but some day, you'll thank me for it. :-) ) – Steve Summit Feb 25 '18 at 14:08
  • @SteveSummitm Oh I am not interested in programming as a career at all, I'm more into graphic design, but I wanted to add some programming so that I know how it works and maybe have some use of it in web design (not literally, but you know, some schemes and rules). But thank you for all the advice, unluckily I'd not pass the subject if I didn't use scanf... – Ania Feb 25 '18 at 16:20

2 Answers2

2

scanf() taking more argument than it should ? yes, its because of \n inside scanf(). you are supposed to scan one char at a time but scanf() wait for extra input each time because of \n

It should be like

scanf(" %c",&A[i]);/* provide space before %c to clear stdin buffer */

Complete code

for (int i = 0; i < 9; i++){
    scanf(" %c", &A[i]);
}
Achal
  • 11,821
  • 2
  • 15
  • 37
1

Try this instead:

char A[9];
    for (int i = 0; i < 9; i++){
        scanf(" %c", &A[i]);
    }

    for (int i = 0; i < 9; i++){
        printf("%c ", A[i]);
        if (i == 2 || i == 5 || i == 8){
            printf("\n");
        }
    }

The problem occurs when you use scanf("%c\n", &A[i]); as explained in this question: scanf asking twice for input while I expect it to ask only once

Lluís Camino
  • 178
  • 3
  • 10