-3

したがって、このコードを実行しようとすると、最初の printf が実行されますが、値を入力せずに 2 番目の printf が表示されます。私が間違っていることはありますか?

    #include <stdio.h>

int main(int argc, const char * argv[])
{
    int suit1;
    int suit2;
    char H, S, C, D;
    float value1;
    float value2;
    printf("Please enter the card's suit");
    scanf("%d", &suit1);
    printf("Please enter the card's value");
    scanf("%f", &value1);
    printf("%d %f", suit1, value1);
}
4

2 に答える 2

1

スートには文字 (「CDHS」) を使用し、カードには整数を使用することを検討してください (浮動小数点数ではなく、精度を損なうことなく小さな整数を表すことができます)。可能な場合は、変数の内部表現を「現実世界に近い」ものにすることをお勧めします...

少し変更するだけで、あなたのプログラムは私にとってはうまく機能します(上記の最近のコメントに照らして更新されました)

#include <stdio.h>

int main(int argc, const char * argv[])
{
    int suit1;
    int suit2;
    char suitInput;
    char Hearts, Spades, Clubs, Diamonds;
    int value1;
    int value2;
    printf("Please enter the card's suit (C, D, H or S): ");
    scanf("%c", &suitInput);

    printf("\nPlease enter the card's value: (1 = Ace, up to 13 = King): ");
    scanf("%d", &value1);
    printf("\nYou entered %c, %d\n", suitInput, value1);
}

私が得る出力:

Please enter the card's suit (C, D, H or S): D

Please enter the card's value: (1 = Ace, up to 13 = King): 5

You entered D, 5
于 2013-10-16T03:28:12.473 に答える
0

最初の入力に文字または文字列を入力していると思われます。これにより、2 番目の入力が「スキップ」されます (最初のスキャンでは文字が消費されず、2 番目のスキャンは失敗するため)。

スーツをスキャンして文字または文字列にします。

char *suit;
suit = malloc(100 * sizeof(char));
scanf("%s\n", suit);

または2つの数字を入力してください

于 2013-10-16T03:41:27.017 に答える