0
int main(){
    char students[20][20]={
        "Ehtisham Hassan",
        "Arbab Mushtaq",
        "M Yaseen Ayub",
        "Qamar Farooq",
        "Muhammad Muneeb"
    };
    char isPresent;

    for(int b=0; b<5; b++){

         printf("%s\n",students[b]);
         scanf("%c\n",&isPresent);

    }

    return 0;
}

What I want my code to do is to Print The Name of one Student from the students array and then wait for the user to input a character but what it does is it prints the name of one student then stores a character and then when the loop runs for the second time it prints the name of two students and then it waits for the char input. Can anyone tell why this happens?

Here is the output of my program:

Ehtisham Hassan
a
Arbab Mushtaq
M Yaseen Ayub
a
Qamar Farooq
Muhammad Muneeb
a

Expected Output:

Ehtisham Hassan
a
Arbab Mushtaq
a
M Yaseen Ayub
a
Qamar Farooq
a
Muhammad Muneeb
a
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

1

Use

scanf(" %c",&isPresent);
      ^^
      white space and removed '\n`

Instead of

scanf("%c\n",&isPresent);

Refer this FAQ question.

msc
  • 33,420
  • 29
  • 119
  • 214
  • 1
    Thank You Very Much... It worked. Can you tell me whats the difference b/w these two statements. Do the white spaces matters? Why it doesn't work without whitespace. – Ehtisham Hassan Feb 25 '17 at 12:40