function prototypes

I have a few questions about the following program. I can't seem to read all the data from the input file. It only reads the first student name, number, and grades then only reads the next student's name then 0's and it stops.

I only need guidance on how to do the function prototypes for determining grade and computing the average. I'm not sure what function prototypes to use


In this program, you are to write a C++ Program that processes students’ grade points using arrays and structure type data.
Consider the following data structures:
1
2
3
4
5
6
7
const int MAX_STDS = 20;
struct StudentGrade {
   int number;      	// student number
   string name;		// student name;
   float eng101;	
   float hist201;
};



StudentGrade studList[MAX_STDS];
StudentGrade is a struct data type that contains the student’s data. studList is an array of StudentGrade. Assume that a college administrator uses your program to get the grade point for each student and average of each course. The student data should be read from an input file

1. Provide two input files, one for English major freshman and one for Math major sophomore class. Each file should contain up to 20 student data. Make up students’ name, id, and scores. Your input file should look like this: (Note the space between the first and last name.)
1
2
3
4
5
6
        Jon Doe  
	xxxxxxxxx		 
	93 85
  	Jane Smith	 
	yyyyyyyyy		 
	87 91



Grade should be given with the following guideline:
score >= 90: A
80 <= score < 90: B
70 <= score < 80: C
60 <= score < 70: D
score < 60: F

Provide separate functions for
a. reading student data from a file,
b. determining the grade,
c. computing the average, and
d. printing out the result

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
#include "prog5.h"

// function prototypes
void init_students(StudentGrade []);           // init all stud data 
void read_data(StudentGrade [], int&);         // read data from file
void print_result(const StudentGrade [], int); // print report 

int main()
{
    StudentGrade studList[MAX_STDS];
    int stud_num;                       // number of student data read from file

    init_students(studList);
    read_data(studList, stud_num);
    print_result(studList, stud_num);

    return 0;
}

void init_students(StudentGrade st[])
{
    // initialize each data member to zero or equivalent
    for (int i = 0; i < MAX_STDS; i++) {
        st[i].name = "";
        st[i].number = 0;
        st[i].eng101 = 0;
        st[i].hist201 = 0;
    }
}   
 
void read_data(StudentGrade st[], int& num) 
{
    ifstream inFile;
    string fileName;

    // ask the user for the name of the file
    cout << "Enter the name of the file that contains the data: ";
    cin >> fileName;

    // open the file
    inFile.open(fileName.data());

    // error check
    while (!inFile)             // see if the file can open for reading
    {
        cout << "File does not exist!" << endl;
        cout << "Enter the name of the file: ";
        cin >> fileName;
        inFile.open(fileName.data());
    } // end while

    // reading begins
    int i = 0;
    getline(inFile, st[i].name);
    while (inFile) {
        inFile >> st[i].number >> st[i].eng101 >> st[i].hist201;
        i++;
        inFile >> st[i].name;
    }
    num = i;
}

void print_result(const StudentGrade st[], int num) 
{ 
    for (int i = 0; i < num; i++) {
        cout << st[i].name << " " << st[i].number << " "
             << st[i].eng101 << " " << st[i].hist201 << endl;
    }
}
Last edited on
First problem: fin >> string will not read "John Doe", only "John". Thereafter, attempting to read an int or some like object will put the stream in an error state, meaning all future input attempts fail.

Use getline().

This will lead to the next consideration:
http://www.cplusplus.com/forum/beginner/22128/#msg116074
Remember, each piece of input in your file is on a line by itself, so read the input line-by-line.

Hope this helps.
I did use getline() in line 59. I'm not sure where you got fin >> string from in my code. It appears that from the link you provided I am supposed to use cin.ignore? I dont know how to use that and we were never taught that in class.
And does it matter that the score for eng101 and the score for hist201 are on the same line? You said each piece of input is on a line by itself but those scores arent.
Last edited on
Topic archived. No new replies allowed.