I am new to C++ and i don't understant this error message i am recieving. Could you show me the solution? ([Error] name lookup of 'n' changed for ISO 'for' scoping [-fpermissive]) line 25, col 17 (the 'n' is the problem)

#include <cstdlib>
#include <iostream>
#include <math.h>
#include "dp_lib.h"

using namespace std;

int main()
{
    double pi=3.141592653589793238462643383279502884197169399375;
    float value;
    unsigned long length = 44100; // sample rate
    int freq = 1000;
    int t = 1;

    int amp = 10;  // size of the wave

    FILE* fp = fopen("data.csv", "wb"); // open file 

// loop to generate audio samples
    for(int n=0; n < length; n++) //where n is the step 
    int t = n/freq;           //t/freq
        {
      value = amp*sin(2*pi*freq*t);
      write_CSV(n, value, fp);
      cout << n << "\t" << value <<"\n"; 
    }

    fclose(fp);         // close file
    free(fp);         // release memory

  cout << "\n\n";
  system ("pause");
}

Thank you very much

Dani AI

Generated

The diagnostic is from GCC telling you that older, non‑standard C++ once allowed a for loop’s control variable to be visible after the loop. ISO C++ changed that: the variable declared in for (int n = ... ) is scoped to the loop only. Without braces, only the next statement is in the loop; any subsequent block that tries to use n is outside that scope, hence the warning downgraded by -fpermissive. See GCC’s notes on C++ dialect and non‑conforming extensions in C++ Dialect Options.

Two practical tips here:

  • If you need the loop variable only inside the loop, always add braces to make the loop body explicit. If you need it afterward, declare it before the loop instead.
  • You are also hitting integer division: n/freq with both int yields 0 for n < freq. For audio synthesis, compute time in seconds as a floating point quantity.

For example, within the loop compute the phase using floating point and std::sin:

double t_sec = static_cast<double>(n) / sample_rate;
double theta = 2.0 * M_PI * freq * t_sec;  // radians
double sample = amplitude * std::sin(theta);

Prefer <cmath> and std::sin (cppreference). Also, do not call free(fp) on a FILE*; just fclose(fp) is correct (cppreference fopen, fclose). Enabling -Wall -Wextra -Wpedantic will help catch single‑statement loops and shadowed variables early.

This is one of those sneaky bugs. Basically it is telling you that if you declare a variable in the for loop, it only lasts as long as the for loop. At first glance it looks like you only ever use 'n' inside your loop. You are wrong. Let me re-write your code with some different whitespace-ing and you will see why:

// loop to generate audio samples
for(int n=0; n < length; n++) //where n is the step |Start of For loop
    int t = n/freq; //t/freq                        |End of For loop


{//random unnecessary block
    value = amp*sin(2*pi*freq*t);
    write_CSV(n, value, fp);
    cout << n << "\t" << value <<"\n";
}

The reason for this is that loops, and if statements, only last for 1 statement (IE: until the first semi-colon). This would make them really hard to use, except that C++ provides Blocks. Basically any time you say {/*code here*/} it takes all of your code and turns it into one statement. This is true of every time you use the curly braces (as far as I know). However functions often still require them (my compiler doesn't, but ideone does).

Thus the fix is simple: move line 22 into the block:

// loop to generate audio samples
for(int n=0; n < length; n++) //where n is the step |Start of For loop
{
int t = n/freq; //t/freq
value = amp*sin(2*pi*freq*t);
write_CSV(n, value, fp);
cout << n << "\t" << value <<"\n";
} //|End of For loop
commented: Fine eye! +15
commented: nice explanation +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.