Longest code I've worked on.

Hello, I'm a first time poster and new to c++. The code that I am working on is from my programming class. I am not looking for anyone to fix it but I do need help figuring out what I am doing wrong. I think I am writing the functions the wrong way... or something with the functions.

The project idea is to take information from an input file which includes the name, Id, homework/participation scores, test scores, and total class score(which equals the previous 2 together) and then write this information to an output file.

There are a lot of specifics but when I compile the program I cannot get it to run. I keep getting errors, some which are in the 130 line area. If I could at least get the program to run, then I can work out the specifics later.

Thanks for any advice.



#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;


//hw scores and the class participation score, maximum 50.
float compute_hw_participation(istream& infile)
{
float hw, part, hwp; float total=0;
cout << " Scores are: ";
for(int i=1; i <= 11; i++)
{
infile >> hw;
infile >> part;
hwp = hw + part;
cout << hwp << endl;
}
return total;
}

//score on the tests, maximum 50.
float compute_tests(float tests, istream& infile)
{

infile >> tests;
cout << " tests are " << tests << endl;
return 0.0;
}



float hwp, tests;
//homework and participation + tests, maximum 100.
float compute_totalscore(float totalscore)
{
totalscore = hwp + tests;
cout << " Total score is " << totalscore << endl;
return 0.0;
}




if (totalscore >= 90)
{

cout << "A" << endl; }
else
{

if (totalscore >= 80)
{

cout << "B" << endl; }
else
{

if (totalscore >= 70)
{

cout << "C" << endl; }
else
{

if (totalscore >= 60)
{

cout << "D" << endl; }
else
{

if (totalscore < 60)
{

cout << "F" << endl; }
}
}
}
}



void printRecord (char name[20], char Id[8], float hwp, float tests, float totalscore, float grade, ostream& outfile)
{
outfile << "Name: "<< name << endl
<< "Id: "<< Id << endl
<< "Homework/Participation: " << hwp << endl
<< "Tests: " << tests << endl
<< "Total course score: " << totalscore << endl
<< "Letter Grade: " << grade << endl
<<"-------------------------------------------------"<<endl<< endl;
}

int main()
{
ofstream outfile;
ifstream infile;


char file_name[21], name[20], Id[20];

float hwp, tests, totalscore;
int deductions;

cout << "Please enter name of input file: ";
cin >> file_name;
infile.open(file_name);
if ( !infile)
{
// abandons operation with error mesg
cout << "Could not open input file \n";
return 0;
}


outfile.open("ScoreResult");
if ( !outfile)
{
cout << "Could not open output file \n";
return 0;
}


infile >> name;
while(!infile.eof())
{
infile >> Id;
cout << name << " " << Id << endl;
hwp = compute_hw_participation(infile);
tests = compute_tests(tests, infile);
totalscore = compute_totalscore(totalscore, infile);
// grade
printRecord(name, Id, hwp, tests, totalscore, outfile);
infile >> name;
}

return 0;
}

The problem is, around line 130, you call the function "compute_totalscore" and pass two parameters into it (totalscore and infile). The function definition earlier in the code only takes one parameter. Either change the definition to accept "totalscore" and "infile", or make the call to the function with just "totalscore".

All of the "if" statements between lines 40 and 80 need to be part of a function. I've named the function "GetGrade" for obvious reasons. Instead of outputting the grade, it returns a char.

Also, when you make the function call to "printRecord" around line 140, you need to pass the "grade" parameter between "totalscore" and "outfile". After making those changes, it complies for me. Obviously, I don't have the input file, so who knows if it actually works.

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
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;


//hw scores and the class participation score, maximum 50.
float compute_hw_participation(istream& infile)
{
float hw, part, hwp; float total=0;
cout << " Scores are: ";
for(int i=1; i <= 11; i++)
{
infile >> hw;
infile >> part;
hwp = hw + part;
cout << hwp << endl;
}
return total;
}

//score on the tests, maximum 50.
float compute_tests(float tests, istream& infile)
{

infile >> tests;
cout << " tests are " << tests << endl;
return 0.0;
}



float hwp, tests;
//homework and participation + tests, maximum 100.
float compute_totalscore(float totalscore)
{
totalscore = hwp + tests;
cout << " Total score is " << totalscore << endl;
return 0.0;
}



char GetGrade(float totalscore)
{
char grade;
if (totalscore >= 90)
{

grade = 'A';}
else
{

if (totalscore >= 80)
{

grade = 'B';}
else
{

if (totalscore >= 70)
{

grade = 'C';}
else
{

if (totalscore >= 60)
{

grade = 'D';}
else
{

if (totalscore < 60)
{

grade = 'F';}
}
}
}
}
return grade;
}



void printRecord (char name[20], char Id[8], float hwp, float tests, float totalscore, float grade, ostream& outfile)
{
outfile << "Name: "<< name << endl
<< "Id: "<< Id << endl
<< "Homework/Participation: " << hwp << endl
<< "Tests: " << tests << endl
<< "Total course score: " << totalscore << endl
<< "Letter Grade: " << grade << endl
<<"-------------------------------------------------"<<endl<< endl;
}

int main()
{
ofstream outfile;
ifstream infile;


char file_name[21], name[20], Id[20];

float hwp, tests, totalscore;
int deductions;

cout << "Please enter name of input file: ";
cin >> file_name;
infile.open(file_name);
if ( !infile)
{
// abandons operation with error mesg
cout << "Could not open input file \n";
return 0;
}


outfile.open("ScoreResult");
if ( !outfile)
{
cout << "Could not open output file \n";
return 0;
}


infile >> name;
while(!infile.eof())
{
infile >> Id;
cout << name << " " << Id << endl;
hwp = compute_hw_participation(infile);
tests = compute_tests(tests, infile);
totalscore = compute_totalscore(totalscore);
char grade = GetGrade(totalscore);
// grade
printRecord(name, Id, hwp, tests, totalscore, grade, outfile);
infile >> name;
}

return 0;
}

Last edited on
Thanks Brent for your help. I will take another look tonight and let you know how it goes.

Ryan
Topic archived. No new replies allowed.