2016-07-06 18 views
0

"int main()"のかっこ内に "random"を宣言する方法がわかりません。 (私はC++の初心者です)C++推測ゲームエラー

私のコードを見て、試してみてください。この問題を解決する方法を知っていると思うと答えてください。それは私にはたくさんのことを意味します。ありがとう!一方、私は自分自身でも問題を解決しようとしています。

注:具体的にするには、Code :: Blocksを使用しています。

エラーは、私のコードのLine 7/9にあります。

#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(); 
} 

ここで更新され、コンパイラエラーです:

|| ===ビルド:推測でデバッグ番号(コンパイラ:GNU GCCコンパイラ)===ここ

は、以下の私の更新されたコードです|

C:\ Users \ Minecraftship \ Documents \ CPPプログラムブック\推測番号\ main.cpp || 関数 'int main()'で:| |

C:\ Users \ Minecraftship \ Documents \ CPPプログラムブック\推測番号\ main.cpp | 12 |

エラー: 'randomize'はこのスコープで宣言されていませんでした。

|| ===ビルドに失敗しました:1つのエラー(s)は、0警告(秒)(0分(秒)、0秒(秒))=== |問題は

+3

は、あなたのコンパイラがエラーに並ぶ何を教えていますありますか?もしそうなら、その情報は参考になります。 –

+7

6行目にはセミコロンがありません。int main(); – yussuf

+7

'while(u = rn)'が疑わしいと思われる –

答えて

10

メインの近くにセミコロンを削除し、コンパイラがあなたを語っているまさに:

​​

があるべき

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

私はusing namespace stdを追加しましたが、他の多くの修正を試みましたが、何らかの理由でエラーが残っています... –

+0

更新されたコードを投稿してください。上のセミコロンを削除しましたか?コンパイラの出力で投稿を編集して、何が起こっているかを確認してください。コードを実行する前にコンパイルしていますか? – 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

これはスムーズに動作します! ;) –