英文字が5つ現れ、そのうちの先頭から2文字目を記憶し、入力するプログラムを作ろうとしています。
教えていただきたいのは、答え合わせの部分です。
入力した英文字と、解答を比較する方法がよくわかりません。
if 文で
if (strcmp(x, mstr[1]) != 0)
などいろいろ試してみたのですが、できない原因やこれらの使い方もいまいち理解できていないので、
ご教授いただけたら幸いです。
よろしくお願いいたします。
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STAGE 10
int sleep(unsigned long x)
{
clock_t c1 = clock(), c2;
do {
if ((c2 = clock()) == (clock_t)-1)
return 0;
} while (1000.0 * (c2 - c1) / CLOCKS_PER_SEC < x);
return 1;
}
int main()
{
int i, stage;
int level;
int success = 0;
char ltr[] = "abcdefghijklmnopqrstuvwxyz";
srand(time(NULL));
printf("2番目の英字を記憶してください。\n");
for (stage = 0; stage < MAX_STAGE; stage++) {
char mstr[10];
char x[10];
for (i = 0; i < 5; i++)
mstr[i] = ltr[rand() % strlen(ltr)];
mstr[5] = '\0';
printf("%s", mstr);
fflush(stdout);
sleep(1000);
printf("\r入力せよ!:");
scanf("%s", x);
if (strcmp(x, mstr[1]) != 0)
printf("間違いです。\n");
else {
printf("正解です。\n");
success++;
}
}
printf("%d回中%d回成功しました。\n", MAX_STAGE, success);
return 0;
}