0

デバッグはまだ得意ではありませんが、いくつかのエラーが発生しています。'(' ')' と ';' がいくつか期待されていました 前の 'if' のない 'else' も、cout の 'operator>>' に一致しません

これは簡単なことだとわかっていますが、それでもドアに足を踏み入れようとしています。ありがとう :)

#include <iostream>
#include <cstdlib>
using namespace std;

int main()   // guess number game
{
    int x;
    cout >> "Please enter a number\n";
    getline(cin x);
    int y = rand();

    while x != y
    {

        if x < y;
           cout >> "Go higher";
        else;
           cout >> "Go lower";
    }


}
4

5 に答える 5

5
cout >> "Please enter a number\n";

これは間違っています。フォーマットされたデータを挿入std::ostreamsするための のみを提供してください。代わりにoperator<<使用してください。cout << "Please enter a number\n";

getline(cin x);

,まず、getlineには 2 つまたは 3 つの引数が必要なため、 がありません。しかし、xis でintegerあり、 a ではないので、std::stringそれでも間違っています。考えてみてください。整数内にテキスト行を格納できますか? cin >> x代わりに使用してください。

int y = rand();

これは間違っているようには見えませんが、論理的なエラーがあります。rand()疑似乱数発生器です。シードを開始値として使用し、ある種のアルゴリズム ( a*m + b) を使用します。したがって、シードとも呼ばれる開始値を指定する必要があります。を使用して指定できますsrand()。同じシードは同じ順序の数字になるため、 のようなものを使用しますsrand(time(0))

while x != y
if x < y;

括弧を使用します。追加の をドロップし;ます。プログラム内の不要なセミコロン;は、空の式に似ています。

編集:作業コード:

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

int main(){
    int x;
    int y;
    srand(time(0));
    y = rand();
    std::cout << "Please enter a number: ";
    do{
        if(std::cin >> x){
            if(x < y)
                std::cout << "Go higher: ";
            if(x > y)
                std::cout << "Go lower: ";    
        }
        else{
            // If the extraction fails, `std::cin` will evaluate to false
            std::cout << "That wasn't a number, try again: ";
            std::cin.clear(); // Clear the fail bits
        }
    }while(x != y);
    std::cout << "Congratulations, you guessed my number :)";

    return 0;
}
于 2012-04-18T21:06:26.807 に答える
1

C++ にはあまり詳しくありませんが、while/if は次のようになるはずです。

 while (x != y)
{

 if (x < y)
    cout << "Go higher";
 else
    cout << "Go lower";
}

if ループと while ループの両方の条件は、括弧でネストする必要があります。

于 2012-04-18T21:06:35.127 に答える
1

これを試して:

void main()
{
  int x;
  cout << "Please enter a number\n";
  getline(cin, x);
  int y = rand();

  while(x != y)
  {
   if(x < y)
    cout << "Go higher";
   else
     cout << "Go lower";
  }
}
于 2012-04-18T21:06:15.467 に答える
0

上記にリストしたものはすべて構文エラーです。これは、C++の構文を読むことで簡単に修正できます。

http://www.cs.duke.edu/csed/tapestry/howtoa.pdf

次のようになります。

 while (x != y)
 {

     if (x < y)
        cout >> "Go higher";
     else
        cout >> "Go lower";
  }
于 2012-04-18T21:03:22.173 に答える
0

すべてのエラーを見てみましょう。

#include <iostream>
#include <cstdlib>
using namespace std;

int main()   // guess number game
{
    int x;
    cout >> "Please enter a number\n"; // should be `cout <<` cin uses >>
    getline(cin x); // incorrect number of arguments should be 2 or 3 plus x is an int not string could use cin instead
    int y = rand();

    while x != y // error condition checks should be parentheses like (x != y)
    {

        if x < y; // error condition should be parentheses also by adding a semicolon here you terminate the statement
           cout >> "Go higher";
        else; // technically not an error but unintended code is executed cout >> "Go higher" is always executed because of the semi colon ;
           cout >> "Go lower";
    }

// no return of value, you declared main to return an int so you should
}

これを試して:

#include <iostream>
#include <cstdlib>
using namespace std;

int main()   // guess number game
{
    int x;
    cout << "Please enter a number\n";
    cin >> x;
    int y = rand();

    while (x != y)
    {

        if (x < y)
           cout >> "Go higher";
        else
           cout >> "Go lower";
    }

    return 0;
}
于 2012-04-18T21:13:27.663 に答える