This program reads students’ names followed by their test scores then output each students’ name followed by the test scores and relevant grade.It should also find and print the highest test score and the name of the students having the highest score.
I am having trouble with the readData() function. I have created a struct called studentType and an array ( studentType graded[50] ) the following is the declaration of the function readData():
|
void readData (ifstream &infile, studentType graded[50], int number_of_students);
|
this is the call to that function:
|
readData (infile, graded, number_of_students);
|
and this is the function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
void readData(ifstream &infile, studentType graded[50], int number_of_students)
{
int index = 0;
for (index = 0; index < 20; index++)
{
infile >> graded[index].lastName;
cout << graded[index].lastName <<",";
infile >> graded[index].firstName;
cout << graded[index].firstName <<" ";
infile >> graded[index].testScore;
cout << graded[index].testScore <<endl;
}
}
|
here is the program code I have so far. I just need to get this function to read the file into the array of structs.
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
|
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstring>
using namespace std;
ifstream infile;
ofstream outfile;
const int ARRAY_SIZE = 50;
//=============== struct definition =======================
// struct to store each students information in
struct studentType
{
string firstName;
string lastName;
int testScore;
char grade;
};
studentType graded[50];
//============== function declarations ======================
// void function to open the input and output files
void openFiles (ifstream& infile, ofstream& output);
// int function to count what size the array needs to be
int fileCount(ifstream &infile);
// void function to read data into the array of structs
void readData (ifstream &infile, studentType graded[50], int number_of_students);
// void function assign a letter grade to each student based on percentage.
void assignGrade (studentType);
// int function search the array a return the highest score
int highScore( int testScore );
// void function to frint the high score info to tha outfile
void printHigh (int array[],int arrayLength, string,struct studentType);
//============== MAIN starts here ==============================================================================
int main()
{
ifstream infile;
ofstream outfile;
int number_of_students = 0;
openFiles (infile, outfile); // function to open external files
number_of_students = fileCount(infile); // function to count the entries in the file
cout << "This is how many files should be in the file: "<< number_of_students << endl; //#####################################################
readData (infile, graded, number_of_students);
infile.close();
outfile.close();
system ("PAUSE");
return(0);
}
//===================== END OF main() Code ====================================================================
//===================== Functions =============================================================================
void openFiles (ifstream& infile, ofstream& output)
{
outfile.open("OutPut.txt");
infile.open("Ch9_Ex2Data.txt");
if (!infile)
{
cout << "Cannot open the input file. Program terminates!"<< endl;
}
}
int fileCount(ifstream &infile)
{
int counter = 0;
int discard;
string discard1;
while (infile)
{
infile >> discard1; //cin.getline(discardl);
cout << discard1<<endl;
infile >> discard1; //cin.getline(discardl);
infile >> discard; //cin.getline(discardl);
counter++;
}
return counter - 1;
}
void readData(ifstream &infile, studentType graded[50], int number_of_students)
{
int index = 0;
for (index = 0; index < 20; index++)
{
infile >> graded[index].lastName;
cout << graded[index].lastName <<",";
infile >> graded[index].firstName;
cout << graded[index].firstName <<" ";
infile >> graded[index].testScore;
cout << graded[index].testScore <<endl;
}
}
|
If i take the code out of the function and place it in the maim it works fine so I am assuming that it is the way i am sending the information to the function.
right now the output looks like this:
Duckey
Goof
Brave
Snow
Alice
Sleeping
Simba
Dumbo
Brown
Johny
Grump
Hap
Dop
Bash
Sleep
Sneeze
Shelly
Chelsea
Angela
Allison
Nields
This is how many files should be in the file: 20
, 0
, 0
, 0
, 0
, 0
, 0
, 0
, 0
, 0
, 0
, 0
, 0
, 0
, 0
, 0
, 0
, 0
, 0
, 0
, 0
Press any key to continue . . .
================================================================================================================================================================
the input file data looks like this:
Duckey Donald 85
Goof Goofy 89
Brave Balto 93
Snow White 93
Alice Wonderful 89
Sleeping Beauty 85
Simba Lion 95
Dumbo Elephant 90
Brown Deer 86
Johny Jocky 95
Grump Grumpy 75
Hap Happy 80
Dop Dopey 80
Bash Bashful 85
Sleep Sleepy 70
Sneeze Sneezey 83
Shelly Malik 95
Chelsea Tomek 95
Angela Clodfelter 95
Allison Nields 95