Looking for hints on reading file

I have to read a text file and then output the information from that text file. I have gotten the file to be read, but I need to format it under a heading using iomanip, then I need to take the data from the file and create a letter grade from it. I am looking for hints not a complete answer.

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
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

int main () {
  string line;
  string grade, STRING;
	double scr1, scr2, scr3, scr4, avg, g;
	g=1;
	cout << std::right << setw(8) << "score1" << setw(8) << std::right << "score2" << std::right << setw(8) << "score3" << std::right << setw(8)
		<< "score4" << std::right << setw(8) << "average" << std::right << setw(8) << "grade" <<endl;
  ifstream myfile ("c:\\temp\\grades.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << std::right << setw(8) << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 
  system("pause");
  return 0;
}
You didn't give an example of what the data looks like. I am assuming that each line has four fields per line -- with each field being a score. If so,

1
2
3
4
5
6
7
    For each line
        Parse the fields and convert the data in the field to a double and assign it to one of your 
        doubles (scr1, scr2, scr3, or scr4).
        Add up all four fields and assign the sum to g.
        Divide g by four and assign this value to avg.
        Assign a letter grade to grade based upon avg.
        Using iomanip, output each value under the appropriate column.
Whoops sorry heres the data.

10 20 90 100
70 70 80 80
80 80 80 80
10 20 -30 40
-10 20 20 50
75 76 77 78
75.83 74.17 90.12 89.88

Thanks for the idea.
Last edited on
What do you mean by:
I need to format it under a heading
?

Should we assume the negative numbers are 'F's? Even I never got a negative grade on a test\assignment.

Hint 1: You need to parse on whitespace.

Hint 2: You cannot relay on a set number of characters to be read in from the stream.

In regards to the negative numbers, the program is supposed to say a whole line is invalid when a negative number is found.
Ok, that parts easy then. If NumberRead is less then ASpecificNumberThatDefinesThePositiveNegativeNumberBoundry do this...

Where are you stuck right now?
I am confused on how exactly I can separate the fields, if I can get each line divided into fields for the score variables I think I can get the, grade, the negative argument and formatting done.

Part of the confusion I think is that this is a continuation of a assignment from last week where instead of having a file to input, I had the user directly input the data. Is there any way to reuse the old file?
I couldn't say regarding your second question since I don't have that project in front of me :p.

You could seperate the data by reading them into an array. Remember to increament the position in the array for every piece of data your read in.
So I have each line being read into a array now, how do I get each field to be a array so I can convert it into a integer so I can sum each line and divide? Thanks for your patience I am not very good at this.

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
 #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    #include <cmath>
    using namespace std;
     
    int main ()
    {
    string array[7]; // creates array to hold names
    string sum,g;
    short loop=0; //short for loop for input
    cout << std::right << setw(8) << "score1" << setw(8) << std::right << "score2" << std::right << setw(8) << "score3" << std::right << setw(8)
		<< "score4" << std::right << setw(8) << "average" << std::right << setw(8) << "grade" <<endl;
     
    string line; //this will contain the data read from the file
    ifstream myfile ("c:\\temp\\grades.txt"); //opening the file.
    if (myfile.is_open()) //if the file is open
    {
    while (! myfile.eof() ) //while the end of file is NOT reached
    {
    getline (myfile,line); //get one line from the file
    array[loop] = line;
    sum=line;
    cout << array[loop] << endl; //and output it
    loop++;
    }
    system("pause");
    exit(0);
  
    }
    }
Last edited on
Don't use "getline(...)" use the extraction operator ">>" when you get the data from the file. Also you should read them into double values so that you don't have to cast them later. So from Line 16 on:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Line 10:
double array[7]; //Changed Type On Your Array
//...
//Line 16:
double line; //Changed The Type On Your Variable

ifstream myfile("c:\\temp\\grades.txt", ios_base::in); // What You Have Is Fine

if(!myfile.is_open()) //Slight Change In Style
    {
      cout << "Could Not Open File\n"; //Error Message To User
      cin.ignore(); //Hold Window Open While User Reads Message
      return 1; //Return 1 To OS Indicating Failure At First Error Check Point
     } //End Of If

while(myfile >> line) //While Valid Data Is Being Read From File
{
    array[loop] = line;
    sum = line; //What Does "sum" Do?

    cout << array[loop] << endl; //This Is Fine
    loop++; //This Is Right
}
I got my own version to work, i'll I need to do still is create the errors for the negative numbers.

There is only one problem which is it won't work when decimals are inputted.

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    #include <cmath>
    using namespace std;
int main() {
    double arr[31],g;  
    string grade;
    int count;
    int i;
    double sum;
    ifstream inf;
    inf.open("c:\\temp\\grades.txt");
    if (!inf) {
       cout << "problem with input file: c:\\temp\\grades.txt";
       system("pause");
       exit(0);      
}
    count=0;
    while (inf >> arr[count]) count++;      
    inf.close();
    
    
    
  
    cout << endl;
    cout << std::right << setw(8) << "score1" << setw(8) << std::right << "score2" << std::right << setw(8) << "score3" << std::right << setw(8)
		<< "score4" << std::right << setw(8) << "average" << std::right << setw(8) << "grade" <<endl;
		g=(arr[0]+arr[1]+arr[2]+arr[3])/4;
			if (g<0) {
		grade= "Error";
	}
	if ((g<60) && (g>=0)) {
		grade = "E";
	}
	if ((g<70) && (g>=60)){
		grade = "D";
	}
	if ((g<80) && (g>=70)){
		grade="C";
	}
	if ((g<90) && (g>=80)) {
		grade="B";
	}
	if (g>=90) {
		grade="A";
	}
		cout << setw(8) << arr[0] << setw(8) << arr[1] << setw(8) << arr[2] << setw(8) << arr[3] << setw(8) << g << setw(8) << grade << endl;
        g=(arr[4]+arr[5]+arr[6]+arr[7])/4;
        			if (g<0) {
		grade= "Error";
	}
	if ((g<60) && (g>=0)) {
		grade = "E";
	}
	if ((g<70) && (g>=60)){
		grade = "D";
	}
	if ((g<80) && (g>=70)){
		grade="C";
	}
	if ((g<90) && (g>=80)) {
		grade="B";
	}
	if (g>=90) {
		grade="A";
	}
		cout << setw(8) << arr[4] << setw(8) << arr[5] << setw(8) << arr[6] << setw(8) << arr[7] << setw(8) << g << setw(8) << grade << endl;
        g=(arr[8]+arr[9]+arr[10]+arr[11])/4;
        			if (g<0) {
		grade= "Error";
	}
	if ((g<60) && (g>=0)) {
		grade = "E";
	}
	if ((g<70) && (g>=60)){
		grade = "D";
	}
	if ((g<80) && (g>=70)){
		grade="C";
	}
	if ((g<90) && (g>=80)) {
		grade="B";
	}
	if (g>=90) {
		grade="A";
	}
		cout << setw(8) << arr[8] << setw(8) << arr[9] << setw(8) << arr[10] << setw(8) << arr[11] << setw(8) << g << setw(8) << grade << endl;
        g=(arr[12]+arr[13]+arr[14]+arr[15])/4;
        			if (g<0) {
		grade= "Error";
	}
	if ((g<60) && (g>=0)) {
		grade = "E";
	}
	if ((g<70) && (g>=60)){
		grade = "D";
	}
	if ((g<80) && (g>=70)){
		grade="C";
	}
	if ((g<90) && (g>=80)) {
		grade="B";
	}
	if (g>=90) {
		grade="A";
	}
		cout << setw(8) << arr[12] << setw(8) << arr[13] << setw(8) << arr[14] << setw(8) << arr[15] << setw(8) << g << setw(8) << grade << endl;
        g=(arr[16]+arr[17]+arr[18]+arr[19])/4;
        			if (g<0) {
		grade= "Error";
	}
	if ((g<60) && (g>=0)) {
		grade = "E";
	}
	if ((g<70) && (g>=60)){
		grade = "D";
	}
	if ((g<80) && (g>=70)){
		grade="C";
	}
	if ((g<90) && (g>=80)) {
		grade="B";
	}
	if (g>=90) {
		grade="A";
	}
		cout << setw(8) << arr[16] << setw(8) << arr[17] << setw(8) << arr[18] << setw(8) << arr[19] << setw(8) << g << setw(8) << grade << endl; 
		g=(arr[20]+arr[21]+arr[22]+arr[23])/4;
					if (g<0) {
		grade= "Error";
	}
	if ((g<60) && (g>=0)) {
		grade = "E";
	}
	if ((g<70) && (g>=60)){
		grade = "D";
	}
	if ((g<80) && (g>=70)){
		grade="C";
	}
	if ((g<90) && (g>=80)) {
		grade="B";
	}
	if (g>=90) {
		grade="A";
	}
		cout << setw(8) << arr[20] << setw(8) << arr[21] << setw(8) << arr[22] << setw(8) << arr[23] << setw(8) << g << setw(8) << grade << endl;
	    g=(arr[24]+arr[25]+arr[26]+arr[27])/4;
	    			if (g<0) {
		grade= "Error";
	}
	if ((g<60) && (g>=0)) {
		grade = "E";
	}
	if ((g<70) && (g>=60)){
		grade = "D";
	}
	if ((g<80) && (g>=70)){
		grade="C";
	}
	if ((g<90) && (g>=80)) {
		grade="B";
	}
	if (g>=90) {
		grade="A";
		cout << setw(8) << arr[24] << setw(8) << arr[25] << setw(8) << arr[26] << setw(8) << arr[27] << setw(8) << g << setw(8) << grade << endl;
    }
     g=(arr[28]+arr[29]+arr[30]+arr[31])/4;
	    			if (g<0) {
		grade= "Error";
	}
	if ((g<60) && (g>=0)) {
		grade = "E";
	}
	if ((g<70) && (g>=60)){
		grade = "D";
	}
	if ((g<80) && (g>=70)){
		grade="C";
	}
	if ((g<90) && (g>=80)) {
		grade="B";
	}
	if (g>=90) {
		grade="A";
		cout << setw(8) << arr[28] << setw(8) << arr[29] << setw(8) << arr[30] << setw(8) << arr[31] << setw(8) << g << setw(8) << grade << endl;
    }
system("pause");
	}
Last edited on
Never mind I got it to work, problem solved. Thanks for the help.
Topic archived. No new replies allowed.