Can't figure out compile error

I am trying to write a program so it will be stored in a set of arrays that will be displayed, sorted, and processed. I need to
declare three arrays, each of which will hold a maximum of 20
elements.One array of integers holds student id numbers. two arrays
of floating point numbers: one for the program averages and one for the test averages. I'm pretty sure it's written correctly, but im having problems with getting it to compile. Specifically im getting errors that "line 35: studentId is not declared in this scope" and "line 60: buffer is not declared in this scope". I have tried to declare each of the variables both within and outside of the functions that they are written in, but its not working. Any idea why this is happening??


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

#include <iostream>
#include <iomanip>
#include <fstream>


using namespace std;

struct student
{
   int studentID;
   double programAvg;
   double testAvg;
};

int buildAr(student[]);
void displayAr(int[], double[], double[], int);
void sortAr(int[], double[], double[], int);
void change(int[], int, int);
void change2(double[], int, int);

#define limit 20

int main ()
{
    student person[limit];
    int count;
    double programAverage[limit], testAverage[limit], overallAverage[limit];
    count = buildAr(person);
    
    if (count > 0)
       {
        cout << "\t\t\tThe UNSORTED student information" << endl << endl;
        displayAr(studentId, programAverage, testAverage, count);
        system("pause");
        
        sortAr(studentId, programAverage, testAverage, count);
        cout << "\t\t\tThe SORTED student information" << endl << endl;
        displayAr(studentId, programAverage, testAverage, count);
       }

system("pause");
return 0;
}

// Build program that builds the arrays from the infomation provided in the input file //
int buildAr(student person[])
    {
     ifstream inFile;
     int i = 0;
	 	 
     inFile.open( "averages.txt" );
     
     if ( inFile.fail() )
        {
         cout << "input file did not open";
         exit(0);
        }
     inFile >> buffer;
     while (inFile)
           { 
            person[i].studentID = atoi(buffer);
            
            inFile >> buffer;
            person[i].programAvg = atof(buffer);
            
            inFile >> buffer;
            person[i].testAvg = atof(buffer);
            i++;
            inFile >> buffer;
           }
     inFile.close();
     return i;
    }

// Display program that displays the information in neat columns //
void displayAr(int studentId[], double programAverage[], double testAverage[], int count)
    { 
      double overallAverage[limit];
      int i;
      cout << "Student ID          Program Average       Test Average          Overall Average" << endl;
      cout << "-------------------------------------------------------------------------------\n";
      for(i = 0; i < count; i++)
         { 
          overallAverage[i] = (programAverage[i] * 0.4) + (testAverage[i] * 0.6);
          cout << setw(8) << right << studentId[i];
          cout <<setiosflags( ios:: fixed | ios:: showpoint ) << setprecision(2); 
		  cout << "\t\t" << programAverage[i] << left << "\t\t     "; 
		  cout << testAverage[i] << "\t\t     " << overallAverage[i] << endl;
         }
      cout << "\n\n";
    }

  
  void sortAr(int studentId[], double programAverage[], double testAverage[], int count)
    {
       double z;
                  int i,j, temp;
                      for ( i=0; i < count; i++)
                         for (j=i+1; j < count; j++)
                           if( studentId[j] < studentId[i])
                           {
                  temp = studentId[i];
               studentId[i] = studentId[j];
                  studentId[j] = temp;
                 
            z = programAverage[i];
        programAverage[i] = programAverage[j];
                  programAverage[j] = z;
                 
                  z = testAverage[i];
                  testAverage[i] = testAverage[j];
                  testAverage[j] = z;
                 }
    }

class="quote">
class="qd">
[output][/output][code]


Last edited on
They're... pretty much what they say on the tin.

You don't have a variable in main() named studentId. Similarly, in buildAr, you don't have a variable named "buffer". You'll need to declare them along with your other ones (in main() and in buildAr(), respectively) before you can use them. :)

-Albatross
I then get an error "invalid conversion from int to int**" in all the instances of studentId. I've never seen this error before. What does that mean?
and "invalid conversion from int to constant character" if i declare int buffer
hey man please put your code into [code] block ! its hard to read
Last edited on
That's because atoi and atof expect a char* (aka in some contexts as a string) as input. If you declare buffer to be an int, you can skip those functions.

How did you declare studentId?

-Albatross
Last edited on
I declared studentId as a integer
...right. *sleepy*

The problem is that (assuming the code there roughly matches what you have) your displayAr expects an integer array (an int[]), but you're handing it an in. In C/C++, these are fundamentally different types.

Do you know what to do from here?

-Albatross

I've been working on this for awhile and i have the program compiling, but now i just get "0x23ff00" as my output, when it should be the list of values from the file. Do you have any idea why this is happening?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

#include <iostream>
#include <iomanip>
#include <fstream>


using namespace std;

int buildAr(int[], double[], double[]);
void displayAr(int[], double[], double[], int);
void sortAr(int[], double[], double[], int);
void change(int[], int, int);
void change2(double[], int, int);

#define max 20

int main ()
{
    int studentId[max];
    int count;
    double programAverage[max], testAverage[max], overallAverage[max];
    count = buildAr(studentId, programAverage, testAverage);
    
    if (count > 0)
       {
        cout << "\t\t\tThe UNSORTED student information" << endl << endl;
        displayAr(studentId, programAverage, testAverage, count);
        system("pause");
        
        sortAr(studentId, programAverage, testAverage, count);
        cout << "\t\t\tThe SORTED student information" << endl << endl;
        displayAr(studentId, programAverage, testAverage, count);
       }

system("pause");
return 0;
}

// Build program that builds the arrays from the infomation provided in the input file //
int buildAr(int studentId[], double programAverage[], double testAverage[])
    {
     ifstream inFile;
     int i = 0;
     inFile.open( "averages.txt" );
     
     if ( inFile.fail() )
        {
         cout << "input file did not open";
         exit(0);
        }
     inFile >> studentId[i];
     while (inFile)
           { 
            inFile >> programAverage[i] >> testAverage[i];
            i++;
            inFile >> studentId[i];
           }
     inFile.close();
     return i;
    }

// Display program that displays the information in neat columns //
void displayAr(int studentId[], double programAverage[], double testAverage[], int count)
    { 
      double overallAverage[max];
      int i;
      cout << "Student ID          Program Average       Test Average          Overall Average" << endl;
      cout << "-------------------------------------------------------------------------------\n";
      for(i = 0; i < count; i++)
         { 
          overallAverage[i] = (programAverage[i] * 0.4) + (testAverage[i] * 0.6);
          
		  cout << setw(8) << right << studentId[i];
          
		  cout <<setiosflags(ios:: fixed | ios:: showpoint) << setprecision(2) << "\t\t"; 
		  cout << programAverage[i] << left << "\t\t     ";
		  cout << testAverage[i] << "\t\t     "; 
		  cout << overallAverage[i];
         }
      cout << "\n\n\n";
    }

// sorting program that sorts the data in ascending student ID //    
void sortAr(int studentId[], double programAverage[], double testAverage[], int count)
    {
      double temp;
      int i,j, temp1;
      for ( i=0; i < count; i++)
          for (j=i+1; j < count; j++)
              if( studentId[j] < studentId[i])
                 {
                  temp1 = studentId[i];
                  studentId[i] = studentId[j];
                  studentId[j] = temp1;
                 
                  temp = programAverage[i];
                  programAverage[i] = programAverage[j];
                  programAverage[j] = temp;
                 
                  temp = testAverage[i];
                  testAverage[i] = testAverage[j];
                  testAverage[j] = temp;
                 }
    }


by the way, i appreciate your help so far....
Last edited on
Topic archived. No new replies allowed.