using ifstream to read multiple lines of code from file

Hello,
I'm working on a program that reads data from a file then does some processing then writes it to another file. Although I can read strings and numbers without getline(); i cannot read all the lines in the file.

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
  3
Charles   89     76    99
Steven    80     90    65
Frank     95     73    84


#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	//Variables
	double grade1, grade2, grade3;
	double avg;
	int numSets;
	string name1, name2, name3;
	
	char letterGrade;

	ifstream readFrom;
	readFrom.open("grades.dat");
	ofstream writeTo;
	writeTo.open("Cunningham_Martin_Out.txt");

	if (readFrom && writeTo)
	{
		readFrom >> numSets;
		for (int x = 0; x < numSets; x++)
		{
			readFrom >> name1 >> grade1 >> grade2 >> grade3;
			readFrom >> name2 >> grade1 >> grade2 >> grade3;
			readFrom >> name3 >> grade1 >> grade2 >> grade3;
			
			avg = (grade1 + grade2 + grade3) / 3.0;
		}
	}
	else
		cout << "File problems\n";
	//Solving average in this format makes the compiler think all numbers from file are being added together
	//then divided by the amount of numbers in file before whitespace
	avg = (grade1 + grade2 + grade3) / 3.0;

	if (avg >= 90)
	{
		letterGrade = 'A';
	}
	else if (avg >= 80 && avg < 90)
	{
		letterGrade = 'B';
	}
	else if (avg >= 70 && avg < 80)
	{
		letterGrade = 'C';
	}
	else if (avg >= 60 && avg < 70)
	{
		letterGrade = 'D';
	}
	else
		letterGrade = 'F';


	writeTo << fixed << setprecision(1);
	writeTo << name1 << setw(12) << letterGrade << setw(12) << avg << endl;
	writeTo << name2 << setw(13) << letterGrade << setw(12) << avg << endl;
	writeTo << name3 << setw(14) << letterGrade << setw(12) << avg << endl;


	//closing the files
	cout << "Closing the files\n";
	readFrom.close();
	writeTo.close();



	system("pause");
	return 0;
}
Last edited on
Please, show some examples of inputs and expected outputs so that I can help you. =)
What is the expected output for:

3
Charles 89 76 99
Steven 80 90 65
Frank 95 73 84


I rewrote your code. I guess this answers your question.

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

using namespace std;

int main ()
{
	ifstream readFrom ("grades.dat");
	ofstream writeTo ("output.txt");

	string name;
	char letterGrade;
	double grade1, grade2, grade3;

	while (readFrom>>name)
	{
		readFrom >> grade1 >> grade2 >> grade3;
		double avg = (grade1+grade2+grade3)/3;
		if (avg>=90)
			letterGrade = 'A';
		else if (avg>=80 && avg < 90)
			letterGrade = 'B';
		else if (avg>=70 && avg < 80)
			letterGrade = 'C';
		else if (avg>=60 && avg < 70)
			letterGrade = 'D';
		else
			letterGrade = 'F';
		writeTo << left << setw(12) << name << setw(12) << letterGrade << setw(12) << avg << endl;

	}

	readFrom.close();
	writeTo.close();

        system("pause");
	return 0;
}
Last edited on
the average for Charles is 88
Steven is 78
and Frank is 84
This is the output I still receive

Charles B 84.0
Steven B 84.0
Frank B 84.0
Disregard the last post, I changed output.txt to the my file name and the output was corrected. Thanks for the help.
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
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main ()
{
	ifstream readFrom ("grades.dat");
	ofstream writeTo ("output.txt");

	string name;
	char letterGrade;
	double grade1, grade2, grade3;
	int number;

	readFrom >> number;

	writeTo << "the average for ";
	while (readFrom>>name)
	{
		readFrom >> grade1 >> grade2 >> grade3;
		double avg = (grade1+grade2+grade3)/3;
		if (avg>=90)
			letterGrade = 'A';
		else if (avg>=80 && avg < 90)
			letterGrade = 'B';
		else if (avg>=70 && avg < 80)
			letterGrade = 'C';
		else if (avg>=60 && avg < 70)
			letterGrade = 'D';
		else
			letterGrade = 'F';
		if (number==1)
			writeTo << "and ";
		writeTo << name << " is " << avg << endl;
		number--;

	} 

	readFrom.close();
	writeTo.close();

        system("pause");
	return 0;
}


input:
3
Charles 89 76 99
Steven 80 90 65
Frank 95 73 84


output:
the average for Charles is 88
Steven is 78.3333
and Frank is 84
Since you have declared avg as a double type, the values ​​will be "broken". It's normal.
Yes I corrected this with fixed and setprecision
Topic archived. No new replies allowed.