i dont know why im getting a compling error for this...

error is:
line 112 - name lookup of `i' changed for new ISO `for' scoping
line 95 - using obsolete binding at `i'

what does that even mean?!

my current code is below, to get a better understanding.
is this a common error? i had this error before, but a simple semi-colon fixed my issues before. but not this one and im all out of ideas.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;


const int nScores = 6;

// function to sort scores
int compare(const void*pa, const void* pb)
{
  const int& a = *static_cast<const int*>(pa);
  const int& b = *static_cast<const int*>(pb);
  if (a < b) return -1; 
  if (a > b) return 1;
  return 0;
}

// function to average scores
double getAverage(int* score, int n)
{
  int sum = 0;
  int i = 0;
  for (i = 0; i < n; i++)
    sum += score[i];
  double average = double(sum) / n;
  return average;
}

// function to find A-scores
int AScoreGreater(int* score, int x)
{
  int nGreater = 0;
  int i;
  for (i = 0; i < nScores; i++)
    if (score[i] >= 90) nGreater++;
  return nGreater;
}

// function to find B-scores
int BScoreGreater(int* score, int x)
{
  int nGreater = 0;
  int i;
  for (i = 0; i < nScores; i++)
    if (score[i] >= 80) nGreater++;
  return nGreater;
}

// function to find C-scores
int CScoreGreater(int* score, int x)
{
  int nGreater = 0;
  int i;
  for (i = 0; i < nScores; i++)
    if (score[i] >= 70) nGreater++;
  return nGreater;
}

// function to find PASSING-scores
int ABCScoreGreater(int* score, int x)
{
  int nGreater = 0;
  int i;
  for (i = 0; i < nScores; i++)
    if (score[i] >= 70) nGreater++;
  return nGreater;
}


// main hub for all
int main()
{
  
// create an empty list
  const int MAX_SCORES = 6;   
  int nScores = 0;               
  int score[MAX_SCORES];
  
    // prompt for how many students
  cout << "How many records would you like to view? ";
  cin >> nScores;
  cin.ignore(1000, 10);
  cout << " " << endl;
  
  // read and save the records
  for (int i = 1; i < nScores; i++)
  {
    if (nScores < MAX_SCORES)
    {
      // create a record and read it from file
      cout << "Enter score: ";
      cin >> nScores;
      cin.ignore(1000, 10);
    
      cout << " " << endl;
    }
  }
  
    qsort (score, nScores, sizeof(int), compare);
  
  cout << "\n Sorted: ";
  int size;
  for (i = 0; i < nScores; i++)
      cout << score[i] << ' ';
      cout << endl;
    
  int max = score[0];
  int min = score[0];
    for (i = 0; i < nScores; i++)
    {
      if (max < score[i]) max = score[i];
      if (min > score[i]) min = score[i];
    } 
    
    cout << " " << endl;
    cout << "highest score: " << max << endl;
    cout << "lowest score: " << min << endl;

    cout << "average score: " << getAverage(score, nScores) << endl;
    
    cout << "\n number of A scores: " << AScoreGreater(score, nScores);
    cout << "\n number of B scores: " << BScoreGreater(score, nScores);
    cout << "\n number of C scores: " << CScoreGreater(score, nScores);
    cout << "\n number of passing scores: " << ABCScoreGreater(score, nScores);
    
    cin >> size;
    cin.ignore(1000, 10);
 
  cin.get();
  return 0;
}

Post with line numbers so we know what lines 95 and 112 are:

[code=cplusplus] // code here

[/code]

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;


const int nScores = 6;

// function to sort scores
int compare(const void*pa, const void* pb)
{
  const int& a = *static_cast<const int*>(pa);
  const int& b = *static_cast<const int*>(pb);
  if (a < b) return -1; 
  if (a > b) return 1;
  return 0;
}

// function to average scores
double getAverage(int* score, int n)
{
  int sum = 0;
  int i = 0;
  for (i = 0; i < n; i++)
    sum += score[i];
  double average = double(sum) / n;
  return average;
}

// function to find A-scores
int AScoreGreater(int* score, int x)
{
  int nGreater = 0;
  int i;
  for (i = 0; i < nScores; i++)
    if (score[i] >= 90) nGreater++;
  return nGreater;
}

// function to find B-scores
int BScoreGreater(int* score, int x)
{
  int nGreater = 0;
  int i;
  for (i = 0; i < nScores; i++)
    if (score[i] >= 80) nGreater++;
  return nGreater;
}

// function to find C-scores
int CScoreGreater(int* score, int x)
{
  int nGreater = 0;
  int i;
  for (i = 0; i < nScores; i++)
    if (score[i] >= 70) nGreater++;
  return nGreater;
}

// function to find PASSING-scores
int ABCScoreGreater(int* score, int x)
{
  int nGreater = 0;
  int i;
  for (i = 0; i < nScores; i++)
    if (score[i] >= 70) nGreater++;
  return nGreater;
}


// main hub for all
int main()
{
  
// create an empty list
  const int MAX_SCORES = 6;   
  int nScores = 0;               
  int score[MAX_SCORES];
  
    // prompt for how many students
  cout << "How many records would you like to view? ";
  cin >> nScores;
  cin.ignore(1000, 10);
  cout << " " << endl;
  
  // read and save the records
  for (int i = 1; i < nScores; i++)
  {
    if (nScores < MAX_SCORES)
    {
      // create a record and read it from file
      cout << "Enter score: ";
      cin >> nScores;
      cin.ignore(1000, 10);
    
      cout << " " << endl;
    }
  }
  
    qsort (score, nScores, sizeof(int), compare);
  
  cout << "\n Sorted: ";
  int size;
  for (i = 0; i < nScores; i++)
      cout << score[i] << ' ';
      cout << endl;
    
  int max = score[0];
  int min = score[0];
    for (i = 0; i < nScores; i++)
    {
      if (max < score[i]) max = score[i];
      if (min > score[i]) min = score[i];
    } 
    
    cout << " " << endl;
    cout << "highest score: " << max << endl;
    cout << "lowest score: " << min << endl;

    cout << "average score: " << getAverage(score, nScores) << endl;
    
    cout << "\n number of A scores: " << AScoreGreater(score, nScores);
    cout << "\n number of B scores: " << BScoreGreater(score, nScores);
    cout << "\n number of C scores: " << CScoreGreater(score, nScores);
    cout << "\n number of passing scores: " << ABCScoreGreater(score, nScores);
    
    cin >> size;
    cin.ignore(1000, 10);
 
  cin.get();
  return 0;
}

I'm getting slightly different line numbers, but the problem is clear. i exists from lines 87 through 98, where it goes out of scope (is destroyed). You use i again in loops starting at lines 104 and 110, but it is out of scope. You have two options. One, pick a wider scope for i. Declare i BEFORE the first for-loop on line 87, say at the top of main. That way i will still be in scope when the next two for-loops need it. Or option two, declare a NEW integer called i in those other two for-loops. To do that, put the word "int" in front of i in lines 104 and 110 like you did in line 87.

> for (int i = 1; i < nScores; i++)
In old C++, this meant

int i;
for ( i = 1; i < nScores; i++) {
}

Which meant you could re-use i later on in the same scope without having to redeclare it.


In new C++ however, i is now restricted to the for loop itself.
That is, it looks more like this

{
    int i;
   for (int i = 1; i < nScores; i++) {
   }
} // now i is OUT of scope

Meaning you can no longer legally reference i as a variable.

Your smart C++ compiler recognises that this would break a lot of old code, and helpfully tells you about the problem.

Either
- fix your single for ( int i to move the declaration of int i; just before the loop begins.
- add int to all your other loops.

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.