2016-08-13 2 views
-6

다음 코드가 항상 '그리기'를 인쇄하는 이유를 이해할 수 없습니다. 그걸 알아 내도록 도와 줄 수 있니? 이 포인터 비교 그대로다음 코드는 항상 '그리기'를 제공합니다. 출력으로

#include <iostream.h> 
#include <conio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <stdio.h> 
#include <ctype.h> 

void main() 

{ 
    clrscr(); 

    int choice1; 
    char choice2[50]; 
    int compare(const char*, const char*); //prototype 
    char s[100]; 
    cout << "\n\n WELCOME TO STONE PAPER SCISSORS " << endl 
     << endl; 
    cout << " enter your choice" << endl; 

    cout << "\n 1:SCISSORS \n"; 
    cout << "\n 2:ROCK \n"; 
    cout << "\n 3:PAPER \n"; 
    cout << "enter choice number"; 
    cin >> choice1; 

    if (choice1 == 2) { 
     cout << "you have entered Stone!"; 
     strcpy(s, "ROCK"); 
    } 
    if (choice1 == 1) { 
     cout << "you have entered scissors"; 
     strcpy(s, "SCISSORS"); 
    } 
    if (choice1 == 3) { 
     strcpy(s, "PAPER"); 
     cout << "you have entered paper"; 
    } 
    randomize(); 
    float point = 2; 
    float compchoice; 
    compchoice = random(point); 

    if (compchoice < 0.37) { 
     strcpy(choice2, "ROCK"); 
    } 
    else if (compchoice < 0.64) { 
     strcpy(choice2, "PAPER"); 
    } 
    else { 
     strcpy(choice2, "SCISSORS"); 
    } 

    cout << endl; 
    cout << "User Choice=" << s << endl; 
    cout << "Computer Choice=" << choice2 << endl; 
    cout << s << "\t" 
     << "VS" 
     << "\t" << choice2 << "=" 
     << " "; 

    int p = compare(s, choice2); 
    if (p == 1) { 
     cout << "computer wins"; 
     if (p == 0) 
      cout << "user wins"; 
     if (p == -1) 
      cout << "draw!"; 

     getch(); 
    } 
    int compare(const char* s, const char* choice2) 
    { 
     if (s == "SCISSORS") { 
      if (choice2 == "ROCK") 
       return 1; 
      else 
       return 0; 
     } 
     else if (s == "ROCK") { 
      if (choice2 == "SCISSORS") 

       return 0; 
      else 
       return 1; 
     } 
     else if (s == "PAPER") { 
      if (choice2 == "SCISSORS") 
       return 1; 
      else 
       return 0; 
     } 
     else 
      return -1; 
    } 
+2

링크가 작동하지 않습니다. [How to ask] (http://stackoverflow.com/help/how-to-ask)를 참조하십시오. – JVApen

+2

http://stackoverflow.com/help/how-to- –

+0

에게 문의하십시오. –

답변

3

이 코드의 문제는 if (choice2 == "SCISSORS") 과 같은 코드 내에 자리 잡고 있습니다. 자세한 내용은 this stack overflow post을 참조하십시오.

char [50] 대신이 코드를 현대화하고 std::string 대신이 문자열을 사용하십시오. 문자열이 배열로 저장된다는 사실을 염려하지 않아야합니다.

관련 문제