C打印问题

时间:2014-10-12 22:58:06

标签: c

我想要做的是用户首先输入的人数会被问到“杯子不是什么颜色?”,然后他们说的任何颜色告诉程序答案不是那种颜色所以最后会留下一种颜色,就是杯子的颜色。当我运行它时,它输出空格。如果我添加代码“printf(”HERE“)”只是为了查看代码到达的位置,它总是在这里打印然后正确的答案颜色,例如HERERed,无论我把printf放在哪里。这只有在我添加代码时才会发生。我知道这不是很有效但帮助将不胜感激。感谢

#include <stdio.h>
#include <string.h>
int main() {
    int quest= 0;
    scanf("%d", &quest);
    int color = 0;
    char strcol[6];
    int ret = 0;
    while(quest > 0){
        scanf("%s", strcol);
        ret = strcmp(strcol, "White");
        if(ret == 0){ //White = 14
            color = color + 1;
        }
        ret = strcmp(strcol, "Black");
        if(ret == 0){ //Black = 13
            color = color + 2;
        }
        ret = strcmp(strcol, "Blue");
        if(ret == 0){ //Blue = 12
            color = color + 3;
        }
        ret = strcmp(strcol, "Red");
        if(ret == 0){ //Red = 11
            color = color + 4;
        }
        ret = strcmp(strcol, "Yellow");
        if(ret == 0){ //Yellow = 10
            color = color + 5;
        }
        else{
            //ERROR
        }
        quest = quest - 1;
    }
    switch ( color ){
        case 14: //Output White
        printf("White");
        break;
        case 13: //Output Black
        printf("Black");
        break;
        case 12: //Output Blue
        printf("Blue");
        break;
        case 11: //Output Red
        printf("Red");
        break;
        case 10: //Output Yellow
        printf("Yellow");
        break;
    }

    return 0;
}

2 个答案:

答案 0 :(得分:1)

尝试

printf("Here\n");

每次要打印某些内容时,请在最后添加'\n'

'\n'表示新行,并且在您编写文本时执行与输入键相同的操作。

答案 1 :(得分:1)

#include <stdio.h>
#include <string.h>

enum COLOR {
    WHITE=1, BLACK=2, BLUE=4, RED=8, YELLOW=16
};

int main() {
    int quest= 0;
    scanf("%d", &quest);
    int color = WHITE + BLACK + BLUE + RED + YELLOW;
    char strcol[7];

    while(quest > 0){
        puts("What color was the mug NOT?");
        scanf("%6s", strcol);
        if(strcmp(strcol, "White") == 0 && color & WHITE)
            color ^= WHITE;//or color -= WHITE;
        else if(strcmp(strcol, "Black") == 0 && color & BLACK)
            color ^= BLACK;
        else if(strcmp(strcol, "Blue") == 0 && color & BLUE)
            color ^= BLUE;
        else if(strcmp(strcol, "Red") == 0 && color & RED)
            color ^= RED;
        else if(strcmp(strcol, "Yellow") == 0 && color & YELLOW)
            color ^= YELLOW;
        else{
            //ERROR
        }
        quest -= 1;
    }
    puts("\nThe color of the mug...");
    switch ( color ){
    case WHITE:
        printf("White");
        break;
    case BLACK:
        printf("Black");
        break;
    case BLUE:
        printf("Blue");
        break;
    case RED:
        printf("Red");
        break;
    case YELLOW:
        printf("Yellow");
        break;
    default:
        if(color & WHITE)
            printf("White ");
        if(color & BLACK)
            printf("Black ");
        if(color & BLUE)
            printf("Blue ");
        if(color & RED)
            printf("Red ");
        if(color & YELLOW)
            printf("Yellow ");
    }
    puts("HERE");

    return 0;
}