0

標準曲線で投げられるダーツをシミュレートするプログラムを作成しようとしています。全体のデバッグに近づくと、何か他のものがポップアップします。これまでのところ、次のような多くのエラーが発生しています。

エラー: 変数はこのスコープで宣言されていません

また、ポインターと整数を比較する C++ に関係する修正方法がわからないというエラーもあります。

私はC ++にかなり慣れていないので、ポインタをいただければ幸いです。

これが私がこれまでに得たものです。

注: エラーは 67、70、72、および 75 行目にあります。

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

double seed(int darts, int x);

int main ()
{
    int darts, x_max;
    double area;

    char again = 'y';
    char giveDarts;
    while (again == 'y' || again == 'Y');
        cout << "Run program (y/n)?";
        cin >> giveDarts;
        switch (giveDarts) {
            case 'y':
            case 'Y':
                cout << "Enter the ammount of darts to be thrown: "; //since we are simulating DARTS I will use the varible darts instead of "NumberOfSamples"
                cin >> darts;
                srand(darts);
            default:
                break;
        }
    cout << "Enter maximum value of x: ";
    cin >> x_max;

    while (x_max < 0);
        cout << "Please enter a positive value of x: ";
        cin >> x_max;
        cout << endl;

    srand(time(NULL));

    area = seed(darts, x_max);

    cout << "Estimate of area under curve is: " << area << endl;
    cout << "Go again? ";
    cin >> again;
    return 0;
}

double seed(int darts, int x_max)
{
    int i, num_darts=0; //num_darts instead of SamplesInsideArea.
    double area;

    for(i=1; i<=darts; i++) // for loop
    {    
        double x, y; 
        double pi = 3.14;
        double n (double t);

        return 1/sqrt(2*pi)*exp(-pow(t,2)/2); //error:'t' was not declared in this scope
              x = rand() / static_cast<double>(RAND_MAX);
              y = rand() / static_cast<double>(RAND_MAX);
        n(0) = (x*x_max + y*y_max); //error: y_max was not declared in this scope

        if(num_darts <= n) //error: ISO C++ forbids comparison between pointer and integer
            num_darts++;

            area*n(0)+0.5 = static_cast<double>(num_darts)/darts; //error: invalid Ivalue in assignment.
     }

     return area;
 }
4

2 に答える 2

0

C++ のプログラミングを初めて学ぶときは、すべての関数をグローバル レベルで宣言および定義することをお勧めします。これは、 のような行double n (double t);が中括弧内に現れてはならないことを意味します。したがって、コードの問題の一部を修正するには、次の 2 行のコードを移動します。

double n (double t);

return 1/sqrt(2*pi)*exp(-pow(t,2)/2);

関数の外側にseed()いくつかのマイナーな変更を加えて、次のようにします。

double n (double t) {
    return 1/sqrt(2*pi)*exp(-pow(t,2)/2)
}

これは正しい方向に役立つはずです。pi(がグローバル定数として宣言されていることを確認してください。)

于 2013-05-01T02:43:14.783 に答える
0
  1. この行:

    double n (double t);
    

    nは、 1 つのパラメーターを取る関数のプロトタイプを作成していますdouble t。これにより、次の 2 つのエラーが発生します。

    • error: 't' was not declared in this scope(関数プロトタイプは変数を宣言しないため)
    • error: ISO C++ forbids comparison between pointer and integern関数へのポインタであるため)

    これは関数のプロトタイプという意味ですか? そうでない場合、どういう意味ですか?

  2. エラーerror: y_max was not declared in this scopeは簡単です。y_maxどこにも宣言されていません。

  3. この行:

    area*n(0)+0.5 = static_cast<double>(num_darts)/darts; //error: invalid Ivalue in assignment.
    

    エラーerror: invalid Ivalue in assignmentは、式に値を割り当てることができないためです。ここで何をするつもりだったのですか?


さらに、他にもいくつかの問題があります。

  1. この行:

    while (again == 'y' || again == 'Y');
    

    again = 'y'直前に設定したため、プログラムは無限ループに陥り、セミコロンはコンパイラに次のように伝えます。

    while (again == 'y' || again == 'Y')
    {
        // do nothing
    }
    

    これを修正するには、セミコロンを削除し、while ループ内にある必要があるコードを中かっこで囲みます。後で同じ問題が発生します ( while (x_max < 0);)。

  2. 他の誰かがこれを指摘しました:

    return 1/sqrt(2*pi)*exp(-pow(t,2)/2);
    

    関数の途中で発生します。これにより、その関数がすぐに終了し、計算された値が返されます。これはあなたが意図したものですか?この行の後のコードは実行されません。


その他の問題:

  1. このコードは N/n ケースを処理しません。「n」を入力してもプログラムは停止せず、おそらくクラッシュします。

    switch (giveDarts) {
    case 'y':
    case 'Y':
        cout << "Enter the ammount of darts to be thrown: "; //since we are simulating DARTS I will use the varible darts instead of "NumberOfSamples"
        cin >> darts;
        srand(darts);
    default:
        break;
    }
    cout << "Enter maximum value of x: ";
    
  2. 空白ではなく中括弧を使用してループを制御します。これの代わりに:

    while (x_max < 0);
    cout << "Please enter a positive value of x: ";
    cin >> x_max;
    cout << endl;
    

    あなたはこれを求めている:

    while (x_max < 0)
    {
        cout << "Please enter a positive value of x: ";
        cin >> x_max;
        cout << endl;
    }
    
  3. この行:

    area*n(0)+0.5 = static_cast<double>(num_darts)/darts;
    

    を設定しようとしている場合area、これは次の形式である必要があります。

    area = static_cast<double>(num_darts)/darts; // n(0)+0.5 goes where??
    
于 2013-05-01T02:19:45.560 に答える