I'm getting errors at every occurrence of my void definitions.
"expected primary-expression before "void"" and "expected ';' before "void"" at my "void displayMenu ()"
"a function-definition is not allowed here before '{" token" "expected ',' or ';' before '{' token' "
At every future occurrence of void.
Any ideas? I've never had problems with my void functions before this and I simply followed the same template I've always used before.
Thanks
#include <iomanip>
#include <iostream>
#include <fstream>
#include <cstring> // string function library
using namespace std;
struct StudentRecord
{
char lastName [16]; // field definitions of the structure
char firstName[16];
char hometown [16];
char major[5];
int studentNumber;
double balance;
int earnedHours;
double gpa;
};
void displayMenu ();
void printArray (StudentRecord s[], int n);
void ascend (StudentRecord s[], int n);
void descend (StudentRecord s[], int n);
void majorThresh (StudentRecord s[], int n);
void threshold (StudentRecord s[], int n);
int main ()
{
char textFilename[50];
char binaryFilename[50];
StudentRecord nextStudent; // one instance of Studentrecord
StudentRecord student[100]; // arrayof 100 StudentRecords
StudentRecord *ptr; //pointer to a struct of type "StudentRecord"
int n; // counts number of elements in student array
fstream studentFile; // an object in fstream can be read or written
ifstream textFile; // an object in istream can be read only
char lastIn [16]; // Places to read data from the text file
char firstIn[16]; // These correspond to the fields of StudentRecord
char townIn [16];
char majorIn[5];
int numberIn;
double balanceIn;
int hoursIn;
double gpaIn;
bool menuActive;
char menuChoice;
{
/* First phase: read text file data and create binary file records */
cout << "Enter filename of source text file: ";
cin >> textFilename;
cout << endl;
textFile.open (textFilename);
cout << "Enter filename of binary file of student records: ";
cin >> binaryFilename;
cout << endl;
studentFile.open (binaryFilename, ios:: out | ios:: binary);
n = 0;
while (!textFile.eof())
{
textFile >> lastIn >> firstIn >> townIn >> majorIn >> numberIn >> balanceIn >> hoursIn >> gpaIn;
if (!textFile.eof()) //
{
strcpy (nextStudent.lastName, lastIn); // string copy function
strcpy (nextStudent.firstName, firstIn);
strcpy (nextStudent.hometown, townIn);
strcpy (nextStudent.major, majorIn);
nextStudent.studentNumber = numberIn;
nextStudent.balance = balanceIn;
nextStudent.earnedHours = hoursIn;
nextStudent.gpa = gpaIn;
studentFile.write (reinterpret_cast<char *>(&nextStudent), sizeof(nextStudent));
n++;
}
} // end while loop
cout << "Records created in binary student file = " << n << endl;
cout << endl;
textFile.close();
studentFile.close();
studentFile.clear();
/* Second phase: read binary file records into an array */
studentFile.open (binaryFilename, ios:: in | ios:: binary);
ptr = student; // point to beginning of array.
n = 0; // count for number of students
while (!studentFile.eof())
{
studentFile.read (reinterpret_cast<char *>(ptr), sizeof(nextStudent));
if (!studentFile.eof())
{
ptr++; //
n++; //count of number of students read from student file.
}
} // end while loop
studentFile.close();
cout << "Records read from binary student file = " << n << endl;
cout << endl<< endl;
menuActive = true;
while (menuActive)
{
displayMenu ();
cin >> menuChoice; cin.ignore(80,'\n');
cout << endl;
switch (menuChoice)
{
case '1': case 'P': case 'p':
printArray (student, n);
break;
case '2':
ascend (student, n);
break;
case '3':
descend (student, n);
break;
case '4':
threshold (student, n);
break;
case '5':
majorThresh (student, n);
break;
case '6': case 'Q': case 'q':
menuActive = false;
break;
default:
cout << " Invalid menu choice: try again!" << endl << endl;
}
}
}
void displayMenu ()
{
cout << " ***********************************************************" << endl;
cout << " * *" << endl;
cout << " * M A I N M E N U *" << endl;
cout << " * *" << endl;
cout << " * 1) (P)rint Array *" << endl;
cout << " * 2) Resort into ascending by last name. *" << endl;
cout << " * 3) Resort into descending by GPA. *" << endl;
cout << " * 4) Display students within balance threshold. *" << endl;
cout << " * 5) Display students of a major in GPA threshold. *" << endl;
cout << " * 6) (Q)uit: Finish processing this array *" << endl;
cout << " * *" << endl;
cout << " ***********************************************************" << endl;
cout << " Enter choice: ";
}
void printArray (StudentRecord s[], int n)
{
int count;
count = 1;
cout << setw(16) << right << "Student Number" << setw(16) << right << "Last Name" << setw(16) << right << "First name" << setw(16) << right << "Major" << setw(14) << right << "Earned Hours" << setw(5) << right << "GPA" << endl;
for (i=0; i < n; i++)
{
cout << setw(3) << right << count << "." << setw(13) << left << student[i].studentNumber << setw(16) << left << student[i].lastName << setw(16) << left << student[i].firstname << setw(4) << left << student[i].major << setw(14) << right << student[i].earnedHours << setw(5) << setpercision(2) << fixed << right << student[i].gpa << endl;
count++;
if (count % 10 == 0)
{
cout << endl << endl;
cout << "Hit <Enter> to continue.";
cin.get(ch);
}
}
}
void ascend (StudentRecord s[], int n)
{
for (i=0; i < n-1; i++)
{
winner=i;
for (j=i+1; j <= n-1; j++)
{
if (strcmp(student[j].lastName, student[winner].lastName) > 0)
winner = j;
}
if (winner!=i)
{
temp = student[i];
student[i] = student[winner];
student[winner] = temp;
}
}
}
void descend (StudentRecord s[], int n)
{
for (i=0; i < n-1; i++)
{
winner=i;
for (j=i+1; j <= n-1; j++)
{
if (student[j].gpa < student[winner].gpa)
winner = j;
}
if (winner!=i)
{
temp = student[i];
student[i] = student[winner];
student[winner] = temp;
}
}
}
void threshold (StudentRecord s[], int n)
{
double thresh;
int count;
cout << "Enter Threshold value for balance: " << endl;
cin >> thresh;
count = 0;
for (i=0; i <=n; i++)
{
if (student[i].balance >= thresh)
{
cout << setw(16) << left << student[i].lastName << setw(16) << left << student[i].firstName << setw(13) << right << student[i].studentNumber << setw(5) << right << setpercision(2) << fixed << student[i].balance << endl;
count++;
}
}
if (count == 0)
{
cout << "No Student Matches Search Criteria." << endl;
}
}
void majorThresh (StudentRecord s[], int n)
{
char major[4];
double thresh;
int print;
{
cout << "Enter Major: "
cin << major;
major = toupper(ch);
cout << "Enter Threshold: "
cin << thresh;
print = 0;
for (i=0; i <=0; i++)
{
if ((strcmp (student[i].major, major) == 0) && student[i].gpa >= thresh);
{
cout << setw(16) << left << student[i].lastName << setw(16) << left << student[i].firstName << setw(16) << left << student[i].hometown << setw(4) << left << student[i].major << setw(5) << setpercision(2) << fixed << right << student[i].gpa << endl;
print++;
}
}
}
if(print == 0)
{
cout << "No student matches search criteria" << endl << endl;
}
}
}