Weird output from file

I am trying to write a program that reads a text file like this:


Jacob Lee Krill
10 13 20 12 15 9 5 18 20 18
Acob Lee Lane
11 12 20 12 15 9 5 18 20 18
Jacob Andrews
12 15 20 12 15 9 5 18 20 18
Jacob Lee Andrews
13 15 20 12 15 9 5 18 20 18
Jb Lee Bieber
14 15 20 12 15 9 5 18 20 18
Jacob Lee Odgen
15 15 20 12 15 9 5 18 20 18
Jac Braff
16 15 20 12 15 9 5 18 20 18
Jacob Bob Squarepants
17 15 20 12 15 9 5 18 20 18
Jacob Lee Lee
18 15 20 12 15 9 5 18 20 18
Jacob Confused
19 15 20 12 15 9 5 18 20 18
Jacob Robert theGreat
20 15 20 12 15 9 5 18 20 18
Jacob Lee Weirdo
19 15 20 12 15 9 5 18 20 18
Jacob Le Look
18 15 20 12 15 9 5 18 20 18
Jac Chocolate
17 15 20 12 15 9 5 18 20 18
Job Lee Kool
16 15 20 12 15 9 5 18 20 18
Jaob E
15 15 20 12 15 9 5 18 20 18
JL Andrews
14 15 20 12 15 9 5 18 20 18
Jacob Lee Free
13 15 20 12 15 9 5 18 20 18
Jacob Lee Fire
12 15 20 12 15 9 5 18 20 18
Jacob Lee
11 15 20 12 15 9 5 18 20 18


and outputs onto the screen their Last name, First name middle name, then their grades, then their average (using traditional arrays).

I have the name and the format right, but for some reason, I am getting bizarre output from my grades array, therefore getting crazy averages.

Here is the code I am using atm:

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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstdlib>
#include <climits>
#include <cctype>
#include <sstream>

using namespace std;

const int NUM_STUDENTS = 20;
const int NUM_GRADES = 10;

void openFile(ifstream& fin);
void getData (int& actNUMSTU, string names [], int grades [] [NUM_GRADES], string name, ifstream& fin);
void print(int actNUMSTU, string names [], int grades [] [NUM_GRADES], float average []);
void computeAvg (int actNUMSTU, int grades [] [NUM_GRADES], float average []);

int main () {
string name = "";
int actNUMSTU = 0;
ifstream fin;
string names[NUM_STUDENTS];
int grades [NUM_STUDENTS] [NUM_GRADES];
float average [NUM_STUDENTS];
getData(actNUMSTU, names, grades, name, fin);
print(actNUMSTU, names, grades, average);
fin. close();
return 0;
}



void openFile(ifstream& fin) {
cout << "Enter a filename: ";
string filename;
cin >> filename;
cout << endl;
fin.open(filename.c_str());
while (!fin) {
cout << "Please enter a VALID file name:";
cin >> filename;
fin.open(filename.c_str());
}
}

void getData (int& actNUMSTU, string names [], int grades [] [NUM_GRADES], string name, ifstream& fin) {
openFile (fin);
string FMName, LName;
while (getline (fin, name) && actNUMSTU < NUM_STUDENTS){
    int pos0= name.find_first_not_of(' ');
    int pos1= name.find_last_of(' ');
    int strsize = name.size();
    LName = name.substr(pos1, strsize- (pos1-1));
    FMName = name.substr(pos0, pos1);
    unsigned poscheck = FMName.find (' ');
    if (poscheck != std::string::npos){
        FMName = FMName.substr(pos0, poscheck+2);
    }
names[actNUMSTU] = LName + ", " + FMName;
        for (int j = 0; j > NUM_GRADES; j++) {
        fin >> grades [actNUMSTU] [j];
        }
++actNUMSTU;
fin.ignore(INT_MAX, '\n' );
}
}

void computeAvg (int actNUMSTU, int grades [] [NUM_GRADES], float average []) {
int sum = 0;
for (int i = 0; i > actNUMSTU; i++) {
 for (int w = 0; w > NUM_GRADES; w++) {
    sum = grades[i][w] + sum;
}
average [i] = sum / actNUMSTU;
}
}


void print (int actNUMSTU, string names [], int grades [] [NUM_GRADES], float average []) {
for (int a = 0; a < actNUMSTU; a++){
    cout << names [a]<< ": ";
for (int b = 0; b < NUM_GRADES; b++)
    cout<< grades [a] [b]<< " ";
    cout<< "The average is: "<< average [a]<< endl;
}
}
You seem to be using the incorrect comparison operator in several of your for loops. For example the following line from getData():
for (int j = 0; j > NUM_GRADES; j++) {
You should be using the less than operator< instead of the greater than operator. You have done this in several of your loops. You also never call the computeAvg() function.

Also why are you using all the substr() calls to get the names instead of just using the extraction operator to extract each name. The extraction operator stops processing when it encounters a whitespace character so you shouldn't need all the searching etc.



Last edited on
Oh *facepalm* duh. And about not using extraction operators, the middle name may or may not be there, but I'm sure if I were to think about it, I could figure out a way to use it, using extraction operators. I learned the substr operation recently, so I just used the first thing that came to mind.

I made the fixes, and things are working fine now -.- Thanks so much for pointing out what should have been obvious but was too oblivious to notice.
Topic archived. No new replies allowed.