나는 C로 빠른 프로그램을 만들려고 노력하고있다. 사용자가 몇 가지 질문과 가능한 대답을 먼저 정의한 다음 프로그램에서 질문합니다. 최종판에서 답이 맞다면 다음 질문이 나오고 그렇지 않으면 프로그램이 끝납니다.C 프로그래밍의 초보자 - 문자열 지정에 붙이기
이것은 내가 거기까지 갔을 때 모든 것이 정확하다는 것을 확신하지 못합니다. (나는 정의 된 각 변수에 대해 어떤 크기를 할당해야하는지 혼란 스럽습니다. 확실한 이유는!).이 프로그램은 '당신이 할 수있는, 3 질문 (이)가 제대로 표시되지 않습니다. 누군가가 조언을 주실 질문 할 때? 이상하게 동작
#include <stdio.h>
#include <string.h>
struct Quest {
char title[255];
char option1[255];
char option2[255];
char answer[5];
};
int main() {
int i;
char name[255];
char user_answ[5];
struct Quest Question[2];
for(i=0; i<3; i++)
{
printf("Enter q%d title:\n", i+1);
fgets(Question[i].title,sizeof(Question[i].title),stdin);
printf("Enter q%d option1:\n", i+1);
fgets(Question[i].option1,sizeof(Question[i].option1),stdin);
printf("Enter q%d option2:\n", i+1);
fgets(Question[i].option2,sizeof(Question[i].option2),stdin);
printf("Enter q%d answer:\n", i+1);
fgets(Question[i].answer,sizeof(Question[i].answer),stdin);
}
/* Ask Name */
printf("What is your name?\n");
fgets(name,sizeof(name),stdin);
printf("Hi %s! \n",name);
for(i=0; i<3; i++){
printf("%s\n", Question[i].title);
printf("1: %s\n", Question[i].option1);
printf("2: %s\n", Question[i].option2);
printf("Please enter your answer\n");
fgets(user_answ,sizeof(user_answ),stdin);
printf("Your answer is %s\n",user_answ);
printf("The right answer is %s\n",Question[i].answer);
}
return 0;
}
감사합니다. @ blue112, 좋은 시작입니다. :) – user2475110