Reading wrong int value from external file

Here I have code for a homework assignment. I am required to read from two different files.
From the first file, I have to read 10 names.
From the second, 5 grades for each of the 10 names.
I was close to completing the project, but come to find out, when I open "grades.txt", it is reading the wrong value for the 5th integer, and the rest after that are completely off.
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
//This program reads 10 names and 5 grades from various files and averages them.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;  // to prevent having to use std:: every time we use certain functions

	int main()   // main function
	{
		ifstream nam, grd;
		ofstream aver;             // this will be the output file's stream
		int grd1, grd2, grd3, grd4, grd5;  //Holds the sums of the 5 grades
		string nam1, nam2, nam3, nam4, nam5, nam6, nam7, nam8, nam9, nam10; //These are variables that hold the name strings
		int grade [9] [4];  // I decided to try and use an array to hold the grade values

		cout<< "Welcome to the grade averager.\n";

		nam.open("C:\\Users\\Alex\\Desktop\\names.txt");
		cout <<"Opening names.txt\n";
		if (!nam)
		{
			cout << "Error Opening File. Sorry...\n";
			return 0;
		}

		nam >> nam1 >> nam2>> nam3 >> nam4 >> nam5
			>> nam6 >> nam7 >> nam8 >> nam9 >> nam10;       // Assigns the names in the file "names.txt" to a variable accesible in
		                                                    // the program.
		nam.close();
			cout << "The names have been loaded. \n";       // close the stream
			cout << "Press ENTER to continue..\n";
			cin.get();
			
		grd.open("C:\\Users\\Alex\\Desktop\\grades.txt");   // Open the file with the sets of grades in them

		cout << "Attemtping to open grades.txt\n";
		if(!grd)
		{
			cout << "Was unable to locate ""grades.txt""\n"; // makes sure that grades.txt was successfully opened.
			return 0;
		}

		grd >> grade [0] [0] >> grade [0] [1] >> grade [0] [2] >> grade [0] [3] >> grade [0] [4]
		>> grade [1] [0] >> grade [1] [1] >> grade [1] [2] >> grade [1] [3]>> grade [1] [4]
		>> grade [1] [0] >> grade [1] [1] >> grade [1] [2] >> grade [1] [3]>> grade [1] [4]
		>> grade [1] [0] >> grade [1] [1] >> grade [1] [2] >> grade [1] [3]>> grade [1] [4]
		>> grade [1] [0] >> grade [1] [1] >> grade [1] [2] >> grade [1] [3]>> grade [1] [4];    //load all the grades from the file. I read that
		                                                                                       //you are only allowed to assign one array at a time.
		                                                                                       // I am unsure if there is a faster way of doing this
		grd1= ((grade [0] [0] + grade [0] [1] + grade [0] [2] + grade [0] [3] + grade [0] [4])/5);  //Adds then divides, assigns to grd1
		cout << nam1<< "'s grade is ["<< grd1<<  "]" << endl;

		grd2= (( grade [1] [0] + grade [1] [1] + grade [1] [2] + grade [1] [3]+ grade [1] [4])/5);
		cout << nam2<< "'s grade is ["<< grd2<<  "]" << endl;
		
		grd3= (( grade [2] [0] + grade [2] [1] + grade [2] [2] + grade [2] [3]+ grade [2] [4])/5);
		cout << nam3<< "'s grade is ["<< grd3<<  "]" << endl;

		grd4= (( grade [3] [0] + grade [3] [1] + grade [3] [2] + grade [3] [3]+ grade [3] [4])/5);
		cout << nam4<< "'s grade is ["<< grd4<<  "]" << endl;

		grd5= (( grade [4] [0] + grade [4] [1] + grade [4] [2] + grade [4] [3]+ grade [4] [4])/5);
		cout << nam5<< "'s grade is ["<< grd5<<  "]" << endl;

		cout<< grade [0] [4];

		cout << "Press ENTER to end.";
		cin.get();
		return 0;
	} 
Last edited on
Ok, with the help of xcode, i found that on line 15, int [9] [4] is wrong, and was cleared up by changing the 4 to a 5. I am unsure as to why this is, since an array initialized to 4 would hold 5 values..
You declared the array as

int grade [9] [4];

So the indexes of the second dimension can be in the range 0 - 3 but you are trying access elements with the second index equal to 4.

grd >> grade [0] [0] >> grade [0] [1] >> grade [0] [2] >> grade [0] [3] >> grade [0] [4] >> grade [1] [0] >> grade [1] [1] >> grade [1] [2] >> grade [1] [3]>> grade [1] [4]
>> grade [1] [0] >> grade [1] [1] >> grade [1] [2] >> grade [1] [3]>> grade [1] [4]
>> grade [1] [0] >> grade [1] [1] >> grade [1] [2] >> grade [1] [3]>> grade [1] [4]
>> grade [1] [0] >> grade [1] [1] >> grade [1] [2] >> grade [1] [3]>> grade [1] [4];


Also you are filling only two rows of the array: 0 and 1 but in the code you are using rows 2 - 4.

grd3= (( grade [2] [0] + grade [2] [1] + grade [2] [2] + grade [2] [3]+ grade [2] [4])/5);
cout << nam3<< "'s grade is ["<< grd3<< "]" << endl;

grd4= (( grade [3] [0] + grade [3] [1] + grade [3] [2] + grade [3] [3]+ grade [3] [4])/5);
cout << nam4<< "'s grade is ["<< grd4<< "]" << endl;

grd5= (( grade [4] [0] + grade [4] [1] + grade [4] [2] + grade [4] [3]+ grade [4] [4])/5);
cout << nam5<< "'s grade is ["<< grd5<< "]" << endl;


Thanks, i fixed the array input rows already, and I now see my mistake. I knew it started at 0, but i figured since it starts at 0, then initializing to 4 would give me 5 values to input. Thanks Vlad.
delete
Last edited on
Show your updated program.
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


//
//  main.cpp
//  averages
//
//  Created by Paul Reed on 1/22/13.
//  Copyright (c) 2013 Paul Reed. All rights reserved.
//

//This program reads 10 names and 5 grades from various files and averages them.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;  // to prevent having to use std:: every time we use certain functions

int main()   // main function
{
    ifstream nam, grd;
    ofstream aver;             // this will be the output file's stream
    int Grade [10];  //Holds the sums of the 5 grades
    string nam1, nam2, nam3, nam4, nam5, nam6, nam7, nam8, nam9, nam10, nam11; //These are variables that hold the name strings
    int grade [9] [5];  // I decided to try and use an array to hold the grade values
    
    cout<< "Welcome to the grade averager.\n";
    
    nam.open("/Volumes/BOOTCAMP/Users/Alex/Desktop/names.txt");
    cout <<"Opening names.txt\n";
    if (!nam)
    {
        cout << "Error Opening File. Sorry...\n";
        return 0;
    }
    
    nam >> nam1 >> nam2>> nam3 >> nam4 >> nam5
    >> nam6 >> nam7 >> nam8 >> nam9 >> nam10>>nam11;
    
    // Assigns the names in the file "names.txt" to a variable accesible inq
    // the program.
    nam.close();
    cout << "The names have been loaded. \n";       // close the stream
    cout << "Press ENTER to continue..\n";
    cin.get();
    
    grd.open("/Volumes/BOOTCAMP/Users/Alex/Desktop/grades.txt");   // Open the file with the sets of grades in them
    
    cout << "Attemtping to open grades.txt\n";
    if(!grd)
    {
        cout << "Was unable to locate ""grades.txt""\n"; // makes sure that grades.txt was successfully opened.
        return 0;
    }
    
    grd >> grade [0] [0] >> grade [0] [1] >> grade [0] [2] >> grade [0] [3] >> grade [0] [4]
    >> grade [1] [0] >> grade [1] [1] >> grade [1] [2] >> grade [1] [3]>> grade [1] [4]
    >> grade [2] [0] >> grade [2] [1] >> grade [2] [2] >> grade [2] [3]>> grade [2] [4]
    >> grade [3] [0] >> grade [3] [1] >> grade [3] [2] >> grade [3] [3]>> grade [3] [4]
    >> grade [4] [0] >> grade [4] [1] >> grade [4] [2] >> grade [4] [3]>> grade [4] [4]
    >> grade [5] [0] >> grade [5] [1] >> grade [5] [2] >> grade [5] [3]>> grade [5] [4]
    >> grade [6] [0] >> grade [6] [1] >> grade [6] [2] >> grade [6] [3]>> grade [6] [4]
    >> grade [7] [0] >> grade [7] [1] >> grade [7] [2] >> grade [7] [3]>> grade [7] [4]
    >> grade [8] [0] >> grade [8] [1] >> grade [8] [2] >> grade [8] [3]>> grade [8] [4]
    >> grade [9] [0] >> grade [9] [1] >> grade [9] [2] >> grade [9] [3]>> grade [9] [4]
    >> grade [10] [0] >> grade [10] [1] >> grade [10] [2] >> grade [10] [3]>> grade [10] [4];

    
    //load all the grades from the file. I read that
    //you are only allowed to assign one array at a time.
    // I am unsure if there is a faster way of doing this
    
    
    
    aver.open("//Users/AlexMac/Desktop/graded.txt", ios::out); //Prepare for file output
    if (!aver)
    {
        cout<< "cannot create file\n.";
        exit(1);
    }

     Grade [0]= ((grade [0] [0] + grade [0] [1] + grade [0] [2] + grade [0] [3] + grade [0] [4])/5);  //Adds                                 then divides, assigns to Grade array [1]
    cout << nam1<< "'s grade is ["<< Grade [0]<<  "]" << endl;
    aver << nam1<< "'s grade is ["<< Grade [0]<<  "]" << endl;
    
    Grade[1]= (( grade [1] [0] + grade [1] [1] + grade [1] [2] + grade [1] [3]+ grade [1] [4])/5);
    cout << nam2<< "'s grade is ["<< Grade[1]<<  "]" << endl;
    aver << nam2<< "'s grade is ["<< Grade[1]<<  "]" << endl;
    
    Grade[2]= (( grade [2] [0] + grade [2] [1] + grade [2] [2] + grade [2] [3]+ grade [2] [4])/5);
    cout << nam3<< "'s grade is ["<< Grade[2]<<  "]" << endl;
    aver << nam3<< "'s grade is ["<< Grade[2]<<  "]" << endl;
    
    Grade[3]= (( grade [3] [0] + grade [3] [1] + grade [3] [2] + grade [3] [3]+ grade [3] [4])/5);
    cout << nam4<< "'s grade is ["<< Grade[3]<<  "]" << endl;
    aver << nam4<< "'s grade is ["<< Grade[3]<<  "]" << endl;

    
    Grade[4]= (( grade [4] [0] + grade [4] [1] + grade [4] [2] + grade [4] [3]+ grade [4] [4])/5);
    cout << nam5<< "'s grade is ["<< Grade[4]<<  "]" << endl;
    aver << nam5<< "'s grade is ["<< Grade[4]<<  "]" << endl;

    
    Grade[5]= (( grade [5] [0] + grade [5] [1] + grade [5] [2] + grade [5] [3]+ grade [5] [4])/5);
    cout << nam6<< "'s grade is ["<< Grade[5]<<  "]" << endl;
    aver << nam6<< "'s grade is ["<< Grade[5]<<  "]" << endl;

    
    Grade[6]= (( grade [6] [0] + grade [6] [1] + grade [6] [2] + grade [6] [3]+ grade [6] [4])/5);
    cout << nam7<< "'s grade is ["<< Grade[6]<<  "]" << endl;
    aver << nam7<< "'s grade is ["<< Grade[6]<<  "]" << endl;

    
    Grade[7]= (( grade [7] [0] + grade [7] [1] + grade [7] [2] + grade [7] [3]+ grade [7] [4])/5);
    cout << nam8<< "'s grade is ["<< Grade[7]<<  "]" << endl;
    aver << nam8<< "'s grade is ["<< Grade[7]<<  "]" << endl;


    Grade[8]= (( grade [8] [0] + grade [8] [1] + grade [8] [2] + grade [8] [3]+ grade [8] [4])/5);
    cout << nam9<< "'s grade is ["<< Grade[8]<<  "]" << endl;
    aver << nam9<< "'s grade is ["<< Grade[8]<<  "]" << endl;


    Grade[9]= (( grade [9] [0] + grade [9] [1] + grade [9] [2] + grade [9] [3]+ grade [9] [4])/5);
    cout << nam10<< "'s grade is ["<< Grade[9]<<  "]" << endl;
    aver << nam10<< "'s grade is ["<< Grade[9]<<  "]" << endl;

    
    aver.close();
    nam.close();
    grd.close();
    
    cout << "Press ENTER to end.";
    cin.get();
    return 0;
}



All done!

Please advise me on anything that could have been done more efficiently.
Last edited on
int grade [9] [5]; // I decided to try and use an array to hold the grade values

1
2
    >> grade [9] [0] >> grade [9] [1] >> grade [9] [2] >> grade [9] [3]>> grade [9] [4]
    >> grade [10] [0] >> grade [10] [1] >> grade [10] [2] >> grade [10] [3]>> grade [10] [4];


The first dimension of grade is 9. Which means valid indices for the first dimension of the array are 0-8.
Last edited on
Topic archived. No new replies allowed.