0

I'm trying to write a program that simulates darts being thrown at a standard curve. Whenever I get close to debugging the entire thing something else pops up. So far I am getting a lot of errors like:

Error: variable is not declared in this scope

Also there's an error I have no idea how to fix which has to do with C++ comparing pointers and integers

I'm pretty new to C++ so any pointers would be greatly appreciated.

Here's what I got so far.

note: errors are on lines 67, 70, 72, and 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;
 }
Chuck
  • 998
  • 8
  • 17
  • 30
  • Also, my instructor asked to adjust the max value of x and y by x_max and y_max but I don't know what to do with y_max. – Tobin Penhollow May 01 '13 at 01:56
  • 1
    Can you please annotate your source code with the lines that are giving errors? It saves us having to count them, or paste them into another editor. – MatthewD May 01 '13 at 01:56
  • You seem to be returning out of you loop. Are you sure you want that? – emsr May 01 '13 at 02:02
  • I added notes that explain the errors I am having. @emsr are you referring to the return function after "again"? – Tobin Penhollow May 01 '13 at 02:05
  • Please format your code so it is more readable. (This might also help you understand where some of the declaration problems are.) – Code-Apprentice May 01 '13 at 02:32
  • Also, please copy and paste the error messages rather than type them. "invalid Ivalue" (uppercase eye) should be "invalid lvalue" (lowercase ell). – Code-Apprentice May 01 '13 at 02:33
  • @TobinPenhollow: He's referring to the return statement after `double n (double t);` – MatthewD May 01 '13 at 03:47
  • Even after fixing the compiler errors, there are many issues you need to fix before this program will work. Most of them can be solved by becoming more familiar with the C++ language itself. If you have a good beginner C++ textbook, I'd suggest you go through it. – MatthewD May 01 '13 at 03:49
  • I was referring to the return statement in the for loop in seed function. `x=rand();` etc. will not be executed as written. – emsr May 01 '13 at 13:38

2 Answers2

0
  1. This line:

    double n (double t);
    

    is prototyping a function n that takes one parameter double t. This is causing two of the errors:

    • error: 't' was not declared in this scope (because function prototypes don't declare variables)
    • error: ISO C++ forbids comparison between pointer and integer (because n is a pointer to a function)

    Did you mean this to be a function prototype? If not, what did you mean?

  2. The error error: y_max was not declared in this scope is straight-forward. y_max isn't declared anywhere.

  3. This line:

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

    The error error: invalid Ivalue in assignment is because you can't assign a value to an expression. What did you intend to do here?


Additionally, there are some other problems:

  1. This line:

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

    will cause your program to go into an infinite loop, since you set again = 'y' just before it, and the semicolon tells the compiler this:

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

    To fix this, remove the semicolon and put braces around the code that needs to be inside the while loop. The same issue exists later too (while (x_max < 0);).

  2. Someone else pointed out this one:

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

    which occurs in the middle of the function. This will cause that function to finish immediately and return the calculated value. Is this what you intended? The code after this line will never run.


More problems:

  1. This code doesn't handle the N/n case. The program will not stop when you type 'n', and will probably crash.

    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. Use braces to control loops, not whitespace. Instead of this:

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

    You want this:

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

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

    If you're trying to set area, this needs to be in the form:

    area = static_cast<double>(num_darts)/darts; // n(0)+0.5 goes where??
    
MatthewD
  • 2,509
  • 2
  • 23
  • 27
  • I don't know what you mean by 'prototype' function but what I was trying to do was set the varible N as a function (e.g n(t)) so that for any input t, the program will input that value to the **equation** N(t). ... I understand the y_max error, but my assignment ask I adjust the max values found for x and y and multiply them. To be more specific: "adjusting your random x and y by x_max and n(0) respectively, as mentioned by the lab, then adjusting the final area estimate by " x_max*n(0) + 0.5 " , which is also mentioned in the lab. .... I was trying to multiply and add to the varible area. – Tobin Penhollow May 01 '13 at 02:30
  • 'prototype function' means you're telling the compiler that there is a function somewhere called `n`, this is what it looks like, and not to worry because the definition of `n` will be available when the linker creates the executable file. – MatthewD May 01 '13 at 02:33
  • Ahh, I think I get what you're trying to do. The return statement is the definition of your function 'n'. – MatthewD May 01 '13 at 02:34
  • @Tobin I suggest you research the difference between function *definitions* and function *declarations* (also called function prototypes). Your textbook should have some information about these two concepts. – Code-Apprentice May 01 '13 at 02:38
  • @Tobin p.s. The line `double seed(int darts, int x);` is an example of a function prototype. – Code-Apprentice May 01 '13 at 02:49
  • @TobinPenhollow: Regarding the line `area*n(0)+0.5 = static_cast(num_darts)/darts;`. What variables are you trying to change? Are you trying to treat `n()` like a list of values? The line `n(0) = (x*x_max + y*y_max);` seems to be attempting the same thing. `n` can only be either a function or a type of storage, but not both. – MatthewD May 02 '13 at 00:35
0

When you are first learning to program C++, I suggest that you declare and define all of your functions at the global level. This means that lines like double n (double t); should never appear inside any braces. So to fix part of the problem with your code, move these two lines of code:

double n (double t);

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

outside of the seed() function and make a few minor modifications so it looks like this:

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

This should help you in the right direction. (Just be sure that pi is declared either as a global constant.)

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268