Help converting inputs.txt to outputs.txt

Pages: 1... 567
Second quick technical thought, you can use a stringstream like this to duplicate your output for both your file and console window:

1
2
3
4
stringstream ss;
ss << "this is my output " << " it works just like cin ";
cout << ss.str();
currentFile << ss.str();


be sure to #include <sstream>
Last edited on
yes kemort is a straight up boss and i am eternally grateful for his guidance theres no question. I'm not stressed at all tbh. I just have a hard time articulating my problem because it makes sense in my head but maybe not someone else. But kemort has been understanding awesomely if thats a word :po ... but i have all the values perfectly though thats the thing. If you mean go through all 137 students with letter A grades and and add up there attendance numbers and divide by that number i will do that. I just dont know what to put where to output my problem. i dont know how to write code very well. but this is my 4th assignment and i learn more every time. just am at a loss as to have my program go through my input.txt file and selectively pick the A students first and avg there attendance. i just dont. ive been trying for the literally 8 hours in the past 24 hours
closed account (48T7M4Gy)
wizebin has given wise counsel phil. I think the idea with stringstream is a very good idea and that is something that can be incorporated now or in the production version.

Like he writes, you are very close to the answer and arrays make it more compact but all they do is make the component parts that you still have to total a little less cumbersome with all these variables. But don't lose sight of the fact that a variable is just a bucket and the variable name is just the name of the bucket.

I obviously don't know what you time schedule is but why not take a break? Don't kick you dog or whatever just go for a walk with it for an hour or so.

Enjoy your break. I did - I just came back from feeding some crappy pigeons for half an hour.

( PS Don't buy a dog if you haven't got one - it's all about simplifying matters now, not complicating. )
Last edited on
closed account (48T7M4Gy)
READ ONLY AFTER DOGWALKING COMPLETE

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

using namespace std;

int main()
{
	ifstream sampleGrades("sampleGrades.txt");
	ofstream outputGrades("outputGrades.txt");

	string firstLine;
	int TotalStudents;

	int ID = 0;
		double Attendance, hw1, hw2, hw3, hw4, hw5, hw6, hw7, exam1, exam2, finalExam;

	double LetterGrade;

	double attendAp = 0, attendA = 0, attendAm = 0;
	int countAp = 0, countA = 0, countAm = 0;

	double attendBp = 0, attendB = 0, attendBm = 0;
	int countBp = 0, countB = 0, countBm = 0;

	double attendCp = 0, attendC = 0, attendCm = 0;
	int countCp = 0, countC = 0, countCm = 0;

	double attendD = 0;
	int countD = 0;

	double attendF = 0;
	int countF = 0;

	int NumA = 0;
	int NumB = 0;
	int NumC = 0;
	int NumD = 0;
	int NumF = 0;

	string Grade = "";

	if (sampleGrades.is_open())
	{
		getline(sampleGrades, firstLine);

		sampleGrades >> TotalStudents;
		cout << "No. of students: " << TotalStudents << endl;

		cout << firstLine << endl;
		cout <<         "ID     Course  Letter"<< endl;
		cout <<         "       Grade   Grade" << endl;

		outputGrades << "ID     Course  Letter" << endl;
		outputGrades << "       Grade   Grade" << endl;

		while (!sampleGrades.eof())
		{
			sampleGrades >> ID >> Attendance >> hw1 >> hw2 >> hw3 >> hw4 >> hw5 >> hw6 >> hw7 >> exam1 >> exam2 >> finalExam;

			Grade = "Error: if this appears you're #$%@*^~ wrong!!";

			LetterGrade = 100*(Attendance*0.10/25 + (hw1 + hw2 + hw3 + hw4 + hw5 + hw6 + hw7)/700*0.35 + exam1/25*0.15 + exam2/25*0.15 + finalExam/40 * 0.25);

			if (LetterGrade >= 97 && LetterGrade <= 100)
			{
				Grade = " A+ ";
				NumA++;
				attendAp += Attendance;
				countAp++;
			}
			else if (LetterGrade >= 93 && LetterGrade < 97)
			{
				Grade = " A ";
				NumA++;
				attendA += Attendance;
				countA++;
			}
			else if (LetterGrade >= 90 && LetterGrade < 93)
			{
				Grade = " A- ";
				NumA++;
				attendAm += Attendance;
				countAm++;
			}
			else if (LetterGrade >= 87 && LetterGrade < 90)
			{
				Grade = " B+ ";
				NumB++;
				attendBp += Attendance;
				countBp++;
			}
			else if (LetterGrade >= 83 && LetterGrade < 87)
			{
				Grade = " B ";
				NumB++;
				attendB += Attendance;
				countB++;
			}
			else if (LetterGrade >= 80 && LetterGrade < 83)
			{
				Grade = " B- ";
				NumB++;
				attendBm += Attendance;
				countBm++;
			}
			else if (LetterGrade >= 77 && LetterGrade < 80)
			{
				Grade = " C+ ";
				NumC++;
				attendCp += Attendance;
				countCp++;
			}
			else if (LetterGrade >= 73 && LetterGrade < 77)
			{
				Grade = " C ";
				NumC++;
				attendC += Attendance;
				countC++;
			}
			else if (LetterGrade >= 70 && LetterGrade < 73)
			{
				Grade = " C- ";
				NumC++;
				attendCm += Attendance;
				countCm++;
			}
			else if (LetterGrade >= 60 && LetterGrade < 70)
			{
				Grade = " D ";
				NumD++;
				attendD += Attendance;
				countD++;
			}
			else if (LetterGrade >= 0 && LetterGrade < 60)
			{
				Grade = " F ";
				NumF++;
				attendF += Attendance;
				countF++;
			}
			cout <<         ID << '\t' << LetterGrade << '\t' << Grade << endl;
			outputGrades << ID << '\t' << LetterGrade << '\t' << Grade << endl;
		}

		outputGrades << "The # of A's: " << NumA << endl;
		outputGrades << "The # of B's: " << NumB << endl;
		outputGrades << "The # of C's: " << NumC << endl;
		outputGrades << "The # of D's: " << NumD << endl;
		outputGrades << "The # of F's: " << NumF << endl;


		outputGrades << "A+'s: " << countAp << " students. Average attendance marks: " << attendAp / countAp << endl;
		outputGrades << " A's: " << countA <<  " students. Average attendance marks: " << attendA  / countA << endl;
		outputGrades << "A-'s: " << countAm<<  " students. Average attendance marks: " << attendAm / countAm << endl;

		outputGrades << "B+'s: " << countBp << " students. Average attendance marks: " << attendBp / countBp << endl;
		outputGrades << " B's: " << countB <<  " students. Average attendance marks: " << attendB  / countB << endl;
		outputGrades << "B-'s: " << countBm << " students. Average attendance marks: " << attendBm / countBm << endl;

		outputGrades << "C+'s: " << countCp << " students. Average attendance marks: " << attendCp / countCp << endl;
		outputGrades << " C's: " << countC <<  " students. Average attendance marks: " << attendC  / countC << endl;
		outputGrades << "C-'s: " << countCm << " students. Average attendance marks: " << attendCm / countCm << endl;

		outputGrades << " D's: " << countD <<  " students. Average attendance marks: " << attendD  / countD << endl;

		outputGrades << " F's: " << countF <<  " students. Average attendance marks: " << attendF  / countF << endl;

		outputGrades << "Letter   Count    Avg    " << endl;
		outputGrades << "Letter   Count Attendance" << endl;
		outputGrades << "  A" << '\t' << countAp + countA + countAm << '\t' << ( attendAp + attendA + attendAm )/ (countAp + countA + countAm) << endl;
		outputGrades << "  B" << '\t' << countBp + countB + countBm << '\t' << (attendBp + attendB + attendBm) / (countBp + countB + countBm) << endl;
		outputGrades << "  C" << '\t' << countCp + countC + countCm << '\t' << (attendCp + attendC + attendCm) / (countCp + countC + countCm) << endl;
		outputGrades << "  D" << '\t' << countD << '\t' << attendD / countD << endl;
		outputGrades << "  F" << '\t' << countF << '\t' << attendF / countF<< endl;

		sampleGrades.close();
		outputGrades.close();
	}
	else
		cout << "Unable to open file";

	return 0;
}
LOL too funny. .. i got some sleep instead of a walk but your right of course i just have to figure it out. I have 4 days to finish it. Thank you ! :p and i already do have a dog he's just in another state at home. im going to study ^ that code and try and figure out what makes it work. It worked and it was what i needed so now that i have some time, ill try to understand it. Thank you kemort. Once my program is done to completion ill let you know :)
-Thank you too wizebin
Last edited on
this is my finished program of exactly how it should be layed out. thank you kemort. i see how close i was. just had to make total a double instead of an int. I am going to study this some more so if i wanted too i could re-create it with a different sized file. you deserve a reward for your awesome guidance. you could probably guide a person out of a crashing airplane if you wanted to

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

using namespace std;

int main()
{
    ifstream sampleGrades4("sampleGrades4.txt");
    ofstream outputGrades4("outputGrades4.txt");
    
    string firstLine;
    int TotalStudents;
    
    int ID = 0;
    double Attendance, hw1, hw2, hw3, hw4, hw5, hw6, hw7, exam1, exam2, finalExam;
    
    double LetterGrade;
    
    double totalAp = 0, totalA = 0, totalAm = 0, AvgAttendanceA = 0.0;
    int countAp = 0, countA = 0, countAm = 0, Acount = 0;
    
    double totalBp = 0, totalB = 0, totalBm = 0, AvgAttendanceB = 0.0;
    int countBp = 0, countB = 0, countBm = 0, Bcount = 0;
    
    double totalCp = 0, totalC = 0, totalCm = 0, AvgAttendanceC = 0.0;
    int countCp = 0, countC = 0, countCm = 0, Ccount = 0;
    
    double totalD = 0, AvgAttendanceD = 0.0;
    int countD = 0, Dcount = 0;
    
    double totalF = 0, AvgAttendanceF = 0.0;
    int countF = 0, Fcount = 0;
    
    int NumA = 0;
    int NumB = 0;
    int NumC = 0;
    int NumD = 0;
    int NumF = 0;
    
    string Grade = "";
    
    if (sampleGrades4.is_open())
    {
        getline(sampleGrades4, firstLine);
        
        sampleGrades4 >> TotalStudents;
        cout << "Number of students: " << TotalStudents << endl;
        
        cout <<         "Letter  Count  Avg. Attendance"<< endl;
        
        outputGrades4 << "ID  Course Grade   Letter Grade" << endl;
     
        
        while (!sampleGrades4.eof())
        {
            sampleGrades4 >> ID >> Attendance >> hw1 >> hw2 >> hw3 >> hw4 >> hw5 >> hw6 >> hw7 >> exam1 >> exam2 >> finalExam;
            
            Grade = "Error: This is wrong!!";
            
            LetterGrade = 100*(Attendance*0.10/25 + (hw1 + hw2 + hw3 + hw4 + hw5 + hw6 + hw7)/700*0.35 + exam1/25*0.15 + exam2/25*0.15 + finalExam/40 * 0.25);
            
            if (LetterGrade >= 97 && LetterGrade <= 100)
            {
                Grade = " A+ ";
                NumA++;
                totalAp += Attendance;
                countAp++;
            }
            else if (LetterGrade >= 93 && LetterGrade < 97)
            {
                Grade = " A ";
                NumA++;
                totalA += Attendance;
                countA++;
            }
            else if (LetterGrade >= 90 && LetterGrade < 93)
            {
                Grade = " A- ";
                NumA++;
                totalAm += Attendance;
                countAm++;
            }
            else if (LetterGrade >= 87 && LetterGrade < 90)
            {
                Grade = " B+ ";
                NumB++;
                totalBp += Attendance;
                countBp++;
            }
            else if (LetterGrade >= 83 && LetterGrade < 87)
            {
                Grade = " B ";
                NumB++;
                totalB += Attendance;
                countB++;
            }
            else if (LetterGrade >= 80 && LetterGrade < 83)
            {
                Grade = " B- ";
                NumB++;
                totalBm += Attendance;
                countBm++;
            }
            else if (LetterGrade >= 77 && LetterGrade < 80)
            {
                Grade = " C+ ";
                NumC++;
                totalCp += Attendance;
                countCp++;
            }
            else if (LetterGrade >= 73 && LetterGrade < 77)
            {
                Grade = " C ";
                NumC++;
                totalC += Attendance;
                countC++;
            }
            else if (LetterGrade >= 70 && LetterGrade < 73)
            {
                Grade = " C- ";
                NumC++;
                totalCm += Attendance;
                countCm++;
            }
            else if (LetterGrade >= 60 && LetterGrade < 70)
            {
                Grade = " D ";
                NumD++;
                totalD += Attendance;
                countD++;
            }
            else if (LetterGrade >= 0 && LetterGrade < 60)
            {
                Grade = " F ";
                NumF++;
                totalF += Attendance;
                countF++;
            }
            
            outputGrades4 << ID << '\t' << setprecision(2) << fixed << LetterGrade << '\t' << '\t'<< Grade << endl;
        
            Acount = countAp + countA + countAm;
            Bcount = countBp + countB + countBm;
            Ccount = countCp + countC + countCm;
            Dcount = countD;
            Fcount = countF;
            
            AvgAttendanceA = (totalAp + totalA + totalAm )/ (countAp + countA + countAm);
            AvgAttendanceB = (totalBp + totalB + totalBm) / (countBp + countB + countBm);
            AvgAttendanceC = (totalCp + totalC + totalCm) / (countCp + countC + countCm);
            AvgAttendanceD = totalD / countD;
            AvgAttendanceF = totalF / countF;
        
        }
        
        
        cout << "  A" << setw(10) << Acount << setw(8) << AvgAttendanceA << endl;
        cout << "  B" << setw(10) << Bcount << setw(10) << setprecision(2) << fixed << AvgAttendanceB << endl;
        cout << "  C" << setw(10) << Ccount << setw(10) << AvgAttendanceC << endl;
        cout << "  D" << setw(10) << Dcount << setw(10) << AvgAttendanceD << endl;
        cout << "  F" << setw(10) << Fcount << setw(10) << AvgAttendanceF << endl;
        
        sampleGrades4.close();
        outputGrades4.close();
    }
    else
        cout << "Unable to open file";
    
    return 0;
}
closed account (48T7M4Gy)
Good on you phil. All the best - it looks great. Give my regards to Rover next time you see him/her.

Cheers

PS
Just out of interest here is a version using arrays:
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
#include<fstream>
#include<string>
#include<iostream>
#include <iomanip>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ifstream;
using std::ofstream;
using std::setw;
using std::setprecision;
using std::fixed;

int main()
{
	ifstream sampleGrades( "sampleGrades.txt" );
	ofstream outputGrades( "outputGrades.txt" );

	int tab = 15;

	string firstLine;
	int totalNoStudents;

	int ID = 0;
	const int LIMIT = 11;
	double item[LIMIT] = { 0 };

	string grade[] = { "A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D", "F", "Error: If this appears you're #$%@*^~ wrong!!" };
	int gradeLimit[] = { 100, 97, 93, 90, 87, 83, 80, 73, 77, 70, 60, 0 };

	int gradeGroup[] = { 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 4 };
	char gradeGroupName[] = { 'A', 'B', 'C', 'D', 'F' };

	int noOfGrades = sizeof( gradeLimit ) / sizeof(int) - 1;
	int noOfGroups = sizeof( gradeGroupName ) / sizeof(char);

	// Stats per individual grade
	double *attendance = new double[noOfGrades];
	int *count = new int[noOfGrades]; // Number of students attaining particular grade

	// Stats per grade group
	double *groupAttendance = new double[noOfGroups];
	int *groupCount = new int[noOfGroups];


	//Initialise the individual arrays
	for (int i = 0; i < noOfGrades; i++)
	{
		attendance[i] = 0;
		count[i] = 0;
	}

	//Initialise the group arrays
	for (int i = 0; i < noOfGroups; i++)
	{
		groupAttendance[i] = 0;
		groupCount[i] = 0;
	}

	double numberGrade = 0;

	if (sampleGrades.is_open())
	{
		// Read header material
		getline( sampleGrades, firstLine );

		sampleGrades >> totalNoStudents;

		outputGrades << setw(tab) << "ID" << setw(tab) << "Grade" << setw(tab) << "Score" << endl;

		while ( !sampleGrades.eof() )
		{

			// Read student
			sampleGrades >> ID;

			// Read student's results 
			for ( int i = 0; i < LIMIT; i++ )
				sampleGrades >> item[i]; // note item i is the raw score for an assessment item

			// Grade results numerically
			numberGrade = item[0] * 0.4 + (item[1] + item[2] + item[3] + item[4] + item[5] + item[6] + item[7]) * 0.05 + item[8] * 0.6 + item[9] * 0.6 + item[10] * 0.625;

			// Determine alphabetic grading and accumulate statistics
			int gradeIndex = 0;
			for ( gradeIndex = 0; gradeIndex < noOfGrades; gradeIndex++ )
			{
				if ( numberGrade > gradeLimit[ gradeIndex + 1 ] && numberGrade <= gradeLimit[gradeIndex] )
				{
					attendance[gradeIndex] += item[1];
					count[gradeIndex]++;

					groupAttendance[ gradeGroup[ gradeIndex ] ] += item[1];
					groupCount[ gradeGroup[ gradeIndex ] ]++;
					break;
				}
			}
			outputGrades << setw(tab) << ID << setw(tab) << grade[gradeIndex] << setw(tab) << fixed << setprecision(1) << numberGrade << endl;
		}
		outputGrades << endl;

		// Detailed statistics
		outputGrades << setw(tab) << "Grade" << setw(tab) << "Count" << setw(tab) << "Total Att" << setw(tab) << "Average" << endl;
		double average = 0;
		for ( int i = 0; i < noOfGrades; i++ )
		{
			if (count[i] == 0)
				average = 0;
			else
				average = attendance[i] / count[i];

			outputGrades << setw(tab) << grade[i] << setw(tab) << count[i] << setw(tab) << attendance[i] << setw(tab) << average << endl;
		}
		outputGrades << endl;

		// Group statistics
		average = 0;
		for ( int i = 0; i < noOfGroups; i++ )
		{
			if ( groupCount[i] == 0 )
				average = 0;
			else
				average = groupAttendance[i] / groupCount[i];

			outputGrades << setw(tab) << gradeGroupName[i] << setw(tab) << groupCount[i] << setw(tab) << groupAttendance[i] << setw(tab) << average << endl;
		}

		// Cleanup
		sampleGrades.close();
		outputGrades.close();

		delete[] attendance;
		delete[] count;
                delete[] groupAttendance;
		delete[] groupCount;
	}
	else
		cout << "Unable to open file";

	return 0;
}
Last edited on
Topic archived. No new replies allowed.
Pages: 1... 567