help me understand why i get this error
Feb 7, 2013 at 2:23am UTC
The hang up im having comes from trying to use a for loop and a function to read in values for the enrollment data of the struct ENROLLMENT
this is the error i keep getting:
1 2 3
academicsB.cpp: In function `int main()':
academicsB.cpp:97: could not convert `Courses[k]' to `ENROLLMENT&'
academicsB.cpp:27: in passing argument 1 of `void ReadEnrollment(ENROLLMENT&)'
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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
// ------------------------------------------------------------------
// File name: academicsb.cpp
// Assign ID: PROG3b
// Due Date: 02/06/13 at 11pm
//
// Purpose: Give student experience working with ARRAYS OF user-defined struct data types.
//
// Author: bfields Byron Fields
//
// ------------------------------------------------------------------
#include <iostream>
#include <iomanip>
using namespace std;
struct ENROLLMENT
{
long StudID;
string Semester;
string CrsPrefix;
int CrsNumber;
int CreditHrs;
char Grade;
};
void ReadEnrollment(ENROLLMENT & inRec)
{
cin >> inRec.Semester >> inRec.CrsPrefix >> inRec.CrsNumber >> inRec.CreditHrs >> inRec.Grade;
}
int main()
{
// ----------------------------------------------------------------------
// Declare variables
// ----------------------------------------------------------------------
int Acount = 0;
int Bcount = 0;
int Ccount = 0;
int Dcount = 0;
int Fcount = 0;
int GPAa = 0;
int coursenum;
int k;
struct STUDENT
{
long StudID; // 9-digit number.
string Last;
string First;
int CumHours;
float GPA;
};
STUDENT STU;
struct ENROLLMENT
{
long StudID;
string Semester;
string CrsPrefix;
int CrsNumber;
int CreditHrs;
char Grade;
};
ENROLLMENT Courses[30];
//-| ----------------------------------------------------------------------
//-| Print the copyright notice declaring authorship.
//-| ----------------------------------------------------------------------
cout << endl << "(c) 2013, bfields Byron Fields" << endl << endl;
//-| ----------------------------------------------------------------------
//-| 1. Read student data into a STUDENT record.
//-| ----------------------------------------------------------------------
cout << "Enter student data. (Input data sequence: StudID, Lastname, Firstname)" << endl;
cin >> STU.StudID >> STU.Last >> STU.First; // Input data sequence: StudID, Lastname, Firstname
//-| ----------------------------------------------------------------------
//-|2. Read ENROLLMENT records.
//-|
//-| 2a. Read the number of courses.
//-| 2b. For each course k, read the enrollment data into k'th record.
//-| ----------------------------------------------------------------------
cout << "Enter number of course:" << endl;
cin >> coursenum;
cout << "Enter enrollment data. (Input data sequence: Semester CrsPrefix CrsNumber CreditHrs Grade)" << endl;
for (k=0;k<coursenum;k++)
{
ReadEnrollment(Courses[k]);
}
//-| ----------------------------------------------------------------------
//-| 3. Compute the #hours student has taken.
//-| ----------------------------------------------------------------------
for (k=0;k<coursenum;k++)
{
STU.CumHours += Courses[k].CreditHrs;
}
//-| ----------------------------------------------------------------------
//-| 4. Compute the GPA based on course grade and credit hours.
//-| ----------------------------------------------------------------------
for (k=0;k<coursenum;k++)
{
switch (Courses[k].Grade)
{
case 'A' :
Acount += 4 * Courses[k].CreditHrs;
break ;
case 'B' :
Bcount += 3 * Courses[k].CreditHrs;
break ;
case 'C' :
Ccount += 2 * Courses[k].CreditHrs;
break ;
case 'D' :
Dcount += 1 * Courses[k].CreditHrs;
break ;
default :
Fcount += 1;
}
GPAa += (Acount + Bcount + Ccount + Dcount);
Acount = 0;
Bcount = 0;
Ccount = 0;
Dcount = 0;
}
STU.GPA = GPAa/STU.CumHours;
//-| ----------------------------------------------------------------------
//-| 5. Print the STUDENT record in the format below:
//-| ----------------------------------------------------------------------
cout << left << setw(10) << "STUDENT:" << left << setw(9) << STU.StudID << right << setw(12) << STU.First << ' ' << left << setw(12) << STU.Last << right << setw(3) << STU.CumHours << right << setw(4) << fixed << setprecision(3) << STU.GPA << endl;
//-| ----------------------------------------------------------------------
//-| Print the copyright notice declaring authorship again.
//-| ----------------------------------------------------------------------
cout << endl << "(c) 2013, bfields Byron Fields" << endl << endl;
return 0;
}//main
Feb 7, 2013 at 2:39am UTC
should line 26 be like this instead?
void ReadEnrollment(ENROLLMENT * inRec)
Feb 7, 2013 at 2:51am UTC
You have several problems:
1) You need:
2) You declare ENROLLMENT multiple times. At line 16 it has global scope. At line 60, it has local scope (to main). At line 70, when you define Courses, you're using the local declaration of ENROLLMENT. At line 97, you're trying to call ReadEnrollment using an array defined with the local definition, while the ReadEnrollment function at line 26 used the global declaration. Get rid of the duplicate declaration at lines 60-68. The declarations may look identical, but because they have different scopes, the compiler treats them differently and gives you an error because they don't match.
3) At line 147, you're doing integer division and assigning the result to a float.
Feb 7, 2013 at 3:01am UTC
thanks for the responses you guys. Program is on track now. That whole local declaration global declaration thing makes sense when i step back and look at it.
Topic archived. No new replies allowed.