-3

Cでyesまたはnoの質問をするにはどうすればよいですか?

このコードは、ユーザー名を取得して文字列にすることになっています (これは機能します)。その後も続き、旅を始める準備ができているかどうかをユーザーに尋ねます (単純なテキスト アドベンチャー ゲームになります)。

残念ながら、これは機能しません。方法がわからないので、はい/いいえの質問をすることはできません。Google 検索と StackOverflow の検索も役に立ちませんでした。

これは私がこれまでに持っているものです:

#include <stdio.h>

int main()
{

/*This declares the strings*/

char name[20];
char yesno[3];
char choice[1];

/*This tells the user that they should only use Y/N when responding to Yes or No questions.*/

printf("When responding to Yes or No questions use Y/N.\n");

/*This asks the user what their name is and allows the program to use it as a string for the rest of the program*/  

printf("Hello! What is your name?\n");
scanf("%s",name);

printf("Hello %s, are you ready for your journey?\n",name);
scanf("%c",choice);


char choice, 'Y';
while (choice != 'N' && choice == 'Y')
    {
        printf("Okay!Let's continue %s.\n",name);
    }
char choice,'N';
while (choice != 'Y' && choice == 'N')
    {
        printf("Goodbye.\n");
    }


return 0;
}
4

3 に答える 3

1

考えられる問題がいくつかあります。

printf("こんにちは %s、旅の準備はいいですか?\n",name); scanf("%c",選択);

1) Scanf は、スキャンされた入力を格納するためのアドレスを必要とします。 & 配列を指定しているため、引数として配列名を指定すると、20 文字の余地がある 'name' で機能します (C では NULL ('\0') を使用することに注意してください)。文字列の終わりを示す文字)。配列名を指定すると、必要な配列の開始アドレスが生成されますが、「choice」を 1 文字の配列として宣言しました。終端の '\0' が配列を埋めるため、空でない文字列を保持することはできません。したがって、charを使用して「scanf」にそのアドレスを指定することをお勧めします。

char choice;
...
scanf("%c", &choice);
...
if (choice=='Y' [etc.]

単純に配列を使用する必要がある場合は、'choice' が最初の要素のアドレスを与えることに注意してください。そのため、文字をテストするには、'*choice' または 'choice[0]' が必要です (これらは同じです)。

そう:... if (*choice == 'y' || *choice == 'Y')...

また:... if (choice[0] ...

ところで、func の 'toupper(int c) & tolower(int c) は、多くの文字をテストしているときに入力を節約できますc=(char) tolower((int) c); if (c == 'y') ...else if (c == 'z') [etc.](キャストなしで済む場合があります)。

-- また、誰かが 19 文字を超える名前を入力scanfすると、配列の後にメモリに保存されname、おそらく他のデータが消去されます。実際、これはあなたの問題かもしれません。を使用scanf("%19s", name)すると、それが修正されます ( yesno@ が最大 2 文字を取得することも確認する必要があります)。

[編集:] さらに: 有効な入力が得られるまでループしたい場合は、次のようにする必要があります:

...
printf("Are you truly ready to embark on your journey (y/n)? ");  
scanf("%c", &choice); /* 'choice' defined as char, not vector */  
while (!strchr("nNyY", choice)) { /* bad input, reprompt */  
    printf("Please enter either 'y' or 'n': ");  
    scanf("%c", &choice);  
}  
if (choice=='y' || choice=='Y') {  
    printf("Well met, %s, thou brave soul!\n", name);  
    /* etc. */  
}  
else {  
    printf("Goodbye!\n");  
    /* etc */  
}  

私はこのコードをテストしていないことを告白しますが、ここから始めたいと思います。それが役立つことを願っています...
よろしく、エド

于 2013-02-03T03:49:46.963 に答える
1
#include <stdio.h>
#include <string.h>

int letterinput(const char *validchars) {
    int ch;
    do ch = getchar(); while ((ch != EOF) && !strchr(validchars, ch));
    return ch;
}

int main(void) {
    int x;
    printf("Enter Y/N: ");
    fflush(stdout);
    x = letterinput("nNyY");
    if (x != EOF) {
        if ((x == 'y') || (x == 'Y')) {
            printf("YES!\n");
        } else {
            printf("no :(\n");
        }
    } else {
        printf("You didn't answer\n");
    }
    return 0;
}
于 2013-02-03T10:58:52.317 に答える
1
char[80] choice;
fgets(choice, sizeof(choice), stdin);

if (choice[0] == 'Y' || choice[0] == 'y')
{
    printf("Okay!Let's continue %s.\n",name);
}
else
{
    printf("Goodbye.\n");
    exit(0);
}
于 2013-02-03T03:08:18.300 に答える