I am writing a program to write my html files rapidly. And when I came to write the content of my page I got a problem.
#include<stdio.h>
int main()
{
int track;
int question_no;
printf("\nHow many questions?\t");
scanf("%d",&question_no);
char question[question_no][100];
for(track=1;track<=question_no;track++)
{
printf("\n<div class=\"question\">%d. ",track);
printf("\nQuestion number %d.\t",track);
fgets(question[track-1],sizeof(question[track-1]),stdin);
printf("\n\n\tQ%d. %s </div>",track,question[track-1]);
}
}
In this program I am writing some questions and their answers (in html file). When I test run this program I input the value of question_no
to 3. But when I enter my first question it doesn't go in question[0]
and consequently the first question doesn't output. The rest of the questions input without issue.
I searched some questions on stackoverflow and found that fgets()
looks for last \0
character and that \0
stops it.
I also found that I should use buffer to input well through fgets()
so I used: setvbuf
and setbuf
but that also didn't work (I may have coded that wrong). I also used fflush(stdin)
after my first and last (as well) scanf
statement to remove any \0
character from stdin but that also didn't work.
Is there any way to accept the first input by fgets()
?
I am using stdin and stdout for now. I am not accessing, reading or writing any file.