我开始编写这个程序代码,它创建一个包含用户输入的HTML页面。
当然,这是一个非常简单的代码版本,但问题在于程序开始解决问题的根源。我使用了烦恼,因为输入将包含空格。 scanf不能正常做到这一点(我想我必须删除\ n?!) 当我运行它时,它会跳过用户输入,并进入第二个输入:
#include <stdio.h>
int main() {
int i;
char choice1[32];
char choice2[32];
char choice3[32];
char choice4[32];
int correctanswer;
char questionstem[128];
unsigned int questionscount;
FILE *fp;
fp=fopen("/Users/User/Desktop/Questions.html", "w+");
fprintf(fp, "<html> <title>Exam</title> <head>Exam</head> <body> ");
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("\n\n\tWelcome to Exam Builder\n\n\t");
printf("\nMultiple-Choice Exam Builder\n");
printf("\nHow many questions?\n");
scanf("%d", &questionscount);
questionscount++;
for (i=1; i<questionscount; i++)
{
printf("\nEnter question stem:\n");
fgets(questionstem, sizeof(questionstem), stdin);/* the program skips this part of the code */
printf("\nQuestion Accepted. Now enter the first choice:[PRESS ENTER KEY TO FINISH TYPING]\n");
fgets(choice1, 128, stdin);
printf("\nNow, the second choice:\n");
fgets(choice2 ,128, stdin);
printf("\nNow, the third choice:\n");
fgets(choice3, 128, stdin);
printf("\nNow, the fourth choice:\n");
fgets(choice4, 128, stdin);
printf("\nWhat is the right answer?[1,2,3,4]");
scanf("%d", &correctanswer);
fprintf(fp, "<p> %d %s %s %s %s %s</p>",i, questionstem, choice1, choice2, choice3, choice4 );
printf("\nGot it. Now, the next question.\n");
}
fprintf(fp, "</body></html>");
fclose(fp);
return 0;
}