2016-07-06 3 views
0

"int main()"에 대한 괄호 안에 "임의"를 선언하는 방법을 모르며 도움이 필요합니다. (저는 C++ 초보자입니다.)C++ 추측 게임 오류

제 코드를 살펴보고 사용해보십시오.이 문제를 해결하는 방법을 알고 있다고 생각되면 답변을 보내주십시오. 그것은 나에게 많은 것을 의미 할 것입니다. 감사! 한편, 나는 문제를 스스로 해결하려고 계속 노력할 것이다.

참고 : 구체적으로 사용하려면 Code :: Blocks를 사용하고 있습니다.

오류는 내 코드 7/9에 있습니다.

|| === 빌드 : 디버그 추측의 수 (컴파일러 : GNU GCC 컴파일러) ===

다음
#include <iostream> 
#include <stdlib.h> 
#include <conio.h> 

using namespace std; 

int main() 
{ 
int rn = random() % 21; // generates a random int from 0 to 20 

// First output asking the user to guess the number 
cout << "Please guess my number :" << endl; 
int u; 
cin >> u; 

while (u != rn) // Calculates the answer that you give 
{ 

// If the user's number is greater than the random number 
// the program will let you know it's too large 
if (u > rn) 
{ 
    cout << "You guessed too big!" << endl; 
} 
// On the other hand, if the user guesses to small 
// the program will tell them that it's too small 
else if (u < rn) 
{ 
    cout << "You guessed too small!" << endl; 
} 

// If the user does not get the right number, the program 
// will tell the user to guess again 
cout << "Please guess again :" << endl; 
cin >> u; 

} 

// If the user guesses the number correctly, the program 
// will say that they got it right, and end the program 
cout << "You guessed it right!" << endl; 
getch(); 
} 

업데이트 된 컴파일러 오류입니다 : 여기

은 아래에있는 내 업데이트 된 코드입니다 |

C : \ Users \ Minecraftship \ Documents \ CPP 프로그램 책 \ 추측에서 번호 \ main.cpp || 함수에서 'int main()': |

C : \ Users \ Minecraftship \ Documents \ CPP 프로그램 책 \ 추측에서 번호 \ main.cpp | 12 |

오류 : 'randomize'가이 범위에서 선언되지 않았습니다.

|| === 실패 빌드 : 1 오류 (들), 0 경고 (들) (0 minute (s)을 0 초 (들)) === | 문제는

+3

은 컴파일러가 당신을 말해 주는가를 무슨 일이야? 그렇다면 그 정보가 도움이 될 것입니다. –

+7

6 행에 세미콜론이 있는데 여기에는 속하지 않습니다. int main(); – yussuf

+7

이'while (u = rn)'은 용의자로 보입니다 –

답변

10

주 근처의 세미콜론을 제거, 컴파일러는 당신을 말하고있다 정확히 :

int main(); 

될해야

int main() 

귀하의 코드는 컴파일되지 않습니다 심지어 당신이 있기 때문에이 문제를 해결 한 후 std 네임 스페이스를 선언하지 않았습니다. 당장은이 줄을 상단에 넣을 수 있습니다. using namespace std; 그러나 bad practice입니다. 범위 분석 연산자를 사용하여 수동으로 선언해야합니다.

위의 주석에서 이미 언급 한 다른 많은 문제는 어떤 줄에서 문제가 발생했는지 알려주기 때문에 컴파일러 출력을 철저히 읽어야합니다. 다른

#include <iostream> 
#include <stdlib.h> 
#include <conio.h> 

using namespace std; 

int main() 
{ 
int rn = random() % 21; // generates a random int from 0 to 20 

// First output asking the user to guess the number 
cout << "Please guess my number :" << endl; 
int u; 
cin >> u; 

while (u != rn) // Calculates the answer that you give 
{ 

    // If the user's number is greater than the random number 
    // the program will let you know it's too large 
    if (u > rn) 
    { 
     cout << "You guessed too big!" << endl; 
    } 
    // On the other hand, if the user guesses to small 
    // the program will tell them that it's too small 
    else if (u < rn) 
    { 
     cout << "You guessed too small!" << endl; 
    } 

    // If the user does not get the right number, the program 
    // will tell the user to guess again 
    cout << "Please guess again :" << endl; 
    cin >> u; 

} 

// If the user guesses the number correctly, the program 
// will say that they got it right, and end the program 
cout << "You guessed it right!" << endl; 
getch(); 
} 
+0

나는 네임 스페이스 std를 사용하여 추가했고 다른 많은 수정을 시도했지만 오류가 여전히 존재합니다 ... –

+0

업데이트 된 코드를 게시하면 * exact * 동일한 오류가 발생하고 있습니까? 위의 세미콜론을 제거 했습니까? 컴파일러 출력으로 게시물을 편집하여 현재 상황을 볼 수 있습니다. 코드를 컴파일하기 전에 컴파일하고 있습니까? – ifma

+0

좋아요, 바로 올거야 ... –

2

누군가가 그것을 가지고 : 같은

코드는 보일 것입니다. main()과 같은 메소드에 서명 한 후에는 세미콜론이 없습니다.

또 다른 것은 언급하지, 난 당신이 또한

while (u != rn) 

을 원하는 추측하고있어, 차이의 조심해야 "="와 "==".

BTW - C++에 오신 것을 환영합니다!

+0

고마워, 그 일 했어! 표시하지만 오류가 계속 발생합니다. –

+0

아, 어떤 이유로 마지막 부분이 내 PC 화면에 등록되지 않았습니다. 환영 브라이언에 감사드립니다! :) –

0

자신에 대한 컴퓨터 플레이 할 수있는 좀 더 휴대용 버전 (conio.h와를 사용하지 않습니다) :

#include <iostream> 
#include <cstdlib> 
#include <ctime> 

int get_random_in_range(int min, int max) 
{ 
    return std::rand() % (max - min) + min; 
} 

// returns 0 if user guessed right, negative value if user 
// guessed too small, positive if user guessed too big 
int check_user_guess(int guess, int my_secret) 
{ 
    return guess - my_secret; 
} 

int main() 
{ 
    int my_guess = get_random_in_range(1, 10); 

    std::cout << "I think of " << my_guess << std::endl; 

    std::cout << "Please guess my number: "; 
    int user_guess = get_random_in_range(1, 10); 
    std::cout << user_guess << std::endl; 

    while (check_user_guess(user_guess, my_guess) != 0) 
    { 
     std::cout << "You guessed " << user_guess << std::endl; 

     if (check_user_guess(user_guess, my_guess) > 0) 
     { 
      std::cout << "You guessed too big!" << std::endl; 
     } 
     else if (check_user_guess(user_guess, my_guess) < 0) 
     { 
      std::cout << "You guessed too small!" << std::endl; 
     } 

     std::cout << "Please guess again: "; 
     user_guess = get_random_in_range(1, 10); 
     std::cout << user_guess << std::endl; 

    } 

    std::cout << std::endl << "You guessed it right!"; 
} 

여기에 그것을 시도 : 오류를 줄 것을 http://coliru.stacked-crooked.com/a/5bf0b9201ef57529

+0

감사합니다 yussuf! 감사합니다. :) –

+0

원활하게 작동합니다! ;) –