2014-10-12 2 views
-2

내가하려는 것은 사용자가 "어떤 색상의 머그잔을 사용하지 않았습니까?"라는 질문을받는 사람들의 숫자를 입력하는 것입니다. 그런 다음 그들이 말한 색상이 무엇이든 프로그램에 대답하는 것은 아닙니다. 그래서 결국 머그잔의 색깔이 남아있는 하나의 색이 될 것입니다. 이 스크립트를 실행하면 빈 공간이 출력됩니다. "printf ("here ")"코드를 추가하면 코드가 어디에 있는지 항상 확인한 다음 여기에 올바른 대답 색 (예 : HERERed)을 인쇄합니다. 이것은 해당 코드를 추가 할 때만 발생합니다. 매우 효율적이지는 않지만 도움이 될 것입니다. 감사합니다C 인쇄 문제

#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; 
} 
+0

고쳐졌습니다. 어떻게 원래의 문제를 해결했는지 확신 할 수는 없지만 같은 색상을 여러 번 입력하면 숫자를 여러 번 추가 할 수있는 논리 오류가 있음을 깨달았습니다. 카운팅 방법을 전환하고 작동했습니다. –

답변

1

printf("Here\n"); 

당신이 뭔가를 인쇄 끝에 '\n'를 추가 할 때마다 시도합니다.

'\n'은 줄 바꿈을 의미하며 텍스트를 쓸 때 Enter 키와 동일한 의미입니다.

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; 
}