ユーザーにボタンを押すと作るか、彼が望んでいる時はいつでも、それを終わらせるために何かを入力するためにとにかくがある場合、私は思っていました。
はい、あります。しかし、最初にscanf()
は、あなたのサンプルコードのようにコンソールアプリケーションでユーザー入力を得る良い方法ですが、バッファオーバーフローや不要な空白などを避けるために注意する必要があります。
とコンソールのユーザー入力のための良い方法に関する:ここ
提案:
永遠にループで単純な文字値のテストを使用してください。主にsequential flow
とif(.){...}else(.){...}
を使用して、
int main()
{
char c[10];
char go[10];
//Place the rest of your variable declarations and initializations here
for(;;)
{
c[0]=0;
//...
//The bulk of your code here...
//...
//Place this section at the bottom of your questions:
printf("enter q to exit, or <enter> to continue\n");
fgets(c, sizeof(c), stdin);//reads string input from stdin
sscanf(c, " %9s", go);//string 'c' is evaluated according to the
//contents of the format string: " %9s"
//and parsed into the buffer 'go'.
//
//Note the space in the format string just
//prior to %9s. It causes any white space,
//including the newline character, \n, to
//be consumed, effectively removing it from
//being written into the 'go' buffer.
//
//The '9' in " %9s" prevents user input beyond
//9 characters to be read into the buffer,
//thus preventing buffer overflow
//and allows room for NULL termination.
if(strstr(go, "q")) break;//if 'q' is in string 'go' exit loop
//...
}
return 0;
}
元の構造に基づいて、あなたのコードの完全なバージョンを、このスニペットは、ループを残し、またはに滞在するユーザの入力時に使用することができる1つの技術の本質的な要素を示していますステートメント(リンクで説明されているメソッドのデモを含む)は以下のとおりです。実行フローは、リンクで説明されている概念を使用することにより、オリジナルよりも改善されています。テストのための質問、質問への回答、そして最後に終了するオプションをユーザに提示します。
注:これらの編集では、テストの終了時にのみ終了するユーザーオプションが用意されています。一番下には、プログラムの任意のポイントを残す機能を含む、他の構成オプションを示すバージョンがあります。変更が行われた場合にコメントが示す
注
int main()
{
char c[10]; ///added
char go[10]; ///added
int a, ep;
int score;
score = 0;
for(;;) ///added
{ ///added
printf("Choose one\n");
printf(" 1. Athletics\t 2. History\t\n 3. Internet\t 4. Greek Mythologyn\n");
printf("Pick: ");
fgets(c, sizeof(c), stdin); //edited
sscanf(c, " %d",&ep); //edited
if (ep==1)
{
printf("\n");
printf("1. Ποιος διεθνής Έλληνας σκόραρε πάνω από ένα γκολ στο Μουντιάλ 2014;\n");
printf(" 1. Παπασταθόπουλος\n 2. Σάμαρης\n 3. Σαμαράς\n 4. Κανένας\n");
printf("Answer: ");
fgets(c, sizeof(c), stdin); //edited
sscanf(c, " %d",&a); //edited
if (a==4)
{
printf("Correct!!!\n");
score = score +1;
}
else
{
printf("Wrong\n");
score = score -1;
}
printf("Your score is: %d\n\n\n",score);
printf("2. Ποιος κέρδισε τον δεύτερο απο τους πέντε τελικούς της σειράς για την Α1 τη\nσεζόν 2013-14;\n");
printf(" 1. Ολυμπιακός\n 2. Παναθηναϊκός\n");
printf("Answer: ");
fgets(c, sizeof(c), stdin); //edited
sscanf(c, " %d",&a); //edited
if (a==1)
{
printf("Correct!!!\n");
score = score +1;
}
else
{
printf("Wrong\n");
score = score -1;
}
printf("Your score is: %d\n\n\n",score);
} ///added
printf("enter q to exit, or c to continue\n"); //added
fgets(c, sizeof(c), stdin); //added
sscanf(c, " %9s", go); //added - note space
//in format string
//to consume \n
//character if there
if(strstr(go, "q")) break; //added
}
return 0;
}
代替構築:
C switch() {...}; statementとternary operator。コンストラクトは、読みやすさを向上させるためのオプションとして使用されます。
int main()
{
char c[10];
char go[10];
int a, ep;
int score;
score = 0;
for(;;)
{
printf("Categories:\n\n");
printf(" 1. Athletics\t 2. History\t\n 3. Internet\t 4. Greek Mythology\n\n");
printf("Choose a category (or 'q' to exit) : ");
fgets(c, sizeof(c), stdin); //edited
(isalpha(c[0])) ?
(sscanf(c, " %9s",go), ep=0) :
(sscanf(c, " %d",&ep), go[0]=0); //using ternary operator -> ?:
if(strstr(go, "q")) break;
switch(ep) {
case 1:
// questions for First category
printf("Make selection: (or 'q' to exit)\n");
printf("1. Ποιος διεθνής Έλληνας σκόραρε πάνω από ένα γκολ στο Μουντιάλ 2014;\n");
printf(" 1. Παπασταθόπουλος\n 2. Σάμαρης\n 3. Σαμαράς\n 4. Κανένας\n");
printf("Answer: ");
fgets(c, sizeof(c), stdin); //edited
(isalpha(c[0])) ?
(sscanf(c, " %9s",go), a=0) :
(sscanf(c, " %d",&a), go[0]=0); //using ternary operator -> ?:
if(strstr(go, "q")) break;
if (a==4)
{
printf("Correct!!!\n");
score = score +1;
}
else
{
printf("Wrong\n");
score = score -1;
}
printf("Your score is: %d\n\n\n",score);
printf("2. Ποιος κέρδισε τον δεύτερο απο τους πέντε τελικούς της σειράς για την Α1 τη\nσεζόν 2013-14;\n");
printf(" 1. Ολυμπιακός\n 2. Παναθηναϊκός\n");
printf("Answer: ");
fgets(c, sizeof(c), stdin); //edited
(isalpha(c[0])) ?
(sscanf(c, " %9s",go), a=0) :
(sscanf(c, " %d",&a), go[0]=0); //using ternary operator -> ?:
if(strstr(go, "q")) break;
if (a==1)
{
printf("Correct!!!\n");
score = score +1;
}
else
{
printf("Wrong\n");
score = score -1;
}
printf("Your score is: %d\n\n\n",score);
break;
case 2:
// questions for second category
break;
case 3:
// questions for third category
break;
case 4:
// questions for forth category
break;
default:
printf("Wrong selection. Try again. (1 - 4)\n");
break;
}
printf("\n\nenter q to exit, or c to continue\n");
fgets(c, sizeof(c), stdin);
sscanf(c, " %9s", go);
if(strstr(go, "q")) break;
}
return 0;
}
を追加する必要がありますが、ユーザが質問に彼らが質問に答えている間、彼らが望む任意の時間に答える中止しますか? –
イベントジェネレータとハンドラのようなものを別のスレッドに作成することなく、シーケンシャルなユーザのプロンプトとレスポンスを使用して終了し、テストを中止するオプションを提供する必要があります。 – ryyker
フォーマットがわかりにくくなりますが、リストしたサンプルコードは完全ではありません。それはコンパイルされません。しかし、下記の答えには完成版(ワイド文字を除く)があります。 – ryyker