need assistance! Write a program that reads students’ names followed by their test scores.

Been having a lot of bugs with this code and can't really figure out why. Any help would be appreciated.

Instructions

Write a program that reads students’ names followed by their test scores. The program should output each student’s name followed by the test scores and the relevant grade. It should also find and print the highest test score and the name of the students having the highest test score.

Student data should be stored in a struct variable of type studentType, which has four components: studentFName and studentLName of type string, testScore of type int (testScore is between 0 and 100), and grade of type char. Suppose that the class has 20 students. Use an array of 20 components of type studentType. Your program must contain at least the following functions:

A function to read the students’ data into the array.
A function to assign the relevant grade to each student.
A function to find the highest test score.
A function to print the names of the students having the highest test score.

Your program must output each student’s name in this form: last name followed by a comma, followed by a space, followed by the first name; the name must be left justified. Moreover, other than declaring the variables and opening the input and output files, the function main should only be a collection of function calls.

Your program should accept no input and save the output to Ch9_Ex2Out.txt.

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

using namespace std;

struct studentType{
   string studentFname;
   string studentLname;
   int testScore;
   char grade;
}

studentType students[20];

void readIn(ifstream& infile, studentType students[]);
void assignGrade(ifstream& infile, studentType students[]);
int topScore(ifstream& infile, studentType students[]);
void printInfo(ofstream& outfile, studentType students[]);


int main(){
   ifstream infile;
   ofstream outfile;

   infile.open("Ch9_Ex2Data.txt");

   if (!infile)
   {
	cout << "Cannot open input file." << endl;
	return 1;
   }

   outfile.open("Ch9_Ex2Out.txt");

   readIn(infile, students);
   assignGrade(infile, students);
   topScore(infile, students);
   printInfo(outfile, students);

   infile.close();
   outfile.close();

    system("pause");
   return 0;

}

void readIn(ifstream& infile, studentType students[]){
   int i;

   for (i = 0; i < 20; i++)
   {
	infile >> students[i].studentFname >>    
students[i].studentLname
		>> students[i].testScore;
   }

}

void assignGrade(ifstream& infile, studentType students[]){
   int i;
   int score;

   for (i = 0; i < 20; i++){
      infile >> students[i].testScore;
      score = students[i].testScore;

      if (score >= 90)
	students[i].grade = 'A';
      else if (score >= 80)
	students[i].grade = 'B';
      else if (score >= 70)
	students[i].grade = 'C';
      else if (score >= 60)
	students[i].grade = 'D';
      else
	students[i].grade = 'F';
   }
}

int topScore(ifstream& infile, studentType students[]){
   int i;
   int highScore = 0;

   for (i = 0; i < 20; i++){
	if (i > highScore)
	   highScore = i;

   }
   return highScore;
}

void printInfo(ofstream& outfile, studentType students[]){
   int i;

   for (i = 0; i < 20; i++){
      outfile << left << students[i].studentLname << ", " << 
      left << students[i].studentFname << " " << students[i].grade <<
      " " << students[i].testScore << endl;
   }
}


heres the input file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Duckey Donald 85
Goof Goofy 89
Brave Balto 93
Snow Smitn 93
Alice Wonderful 89
Samina Akthar 85
Simba Green 95
Donald Egger 90
Brown Deer 86
Johny Jackson 95
Greg Gupta 75
Samuel Happy 80
Danny Arora 80
Sleepy June 70
Amy Cheng 83
Shelly Malik 95
Chelsea Tomek 95
Angela Clodfelter 95
Allison Nields 95
Lance Norman 88
> void assignGrade(ifstream& infile, studentType students[])
You've already read the file, so you don't need infile to be a parameter, and you don't need line 67 to read the file again.

What are you supposed to do with the topscore when it's returned to main?
You presently ignore the result.
topScore doesn't need the inFile parameter either.


Perhaps something like:

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

struct studentType {
	std::string studentFname;
	std::string studentLname;
	int testScore {};
	char grade {};
};

constexpr size_t MaxStud { 20 };

void readIn(std::ifstream& infile, studentType students[]);
void assignGrade(studentType students[]);
int topScore(const studentType students[]);
void printInfo(std::ofstream& outfile, const studentType students[]);
void printHighest(std::ofstream& outfile, const studentType studemnts[]);

int main() {
	std::ifstream infile("Ch9_Ex2Data.txt");

	if (!infile)
		return (std::cout << "Cannot open input file.\n"), 1;

	std::ofstream outfile("Ch9_Ex2Out.txt");

	if (!outfile)
		return (std::cout << "Cannot open output file.\n"), 2;

	studentType students[MaxStud];

	readIn(infile, students);
	assignGrade(students);
	printInfo(outfile, students);
	printHighest(outfile, students);
}

void readIn(std::ifstream& infile, studentType students[]) {
	for (size_t i {}; i < MaxStud; ++i)
		infile >> students[i].studentFname >>
			students[i].studentLname >>
			students[i].testScore;
}

void assignGrade(studentType students[]) {
	for (size_t i {}; i < MaxStud; ++i) {
		const auto& score { students[i].testScore };

		if (score >= 90)
			students[i].grade = 'A';
		else if (score >= 80)
			students[i].grade = 'B';
		else if (score >= 70)
			students[i].grade = 'C';
		else if (score >= 60)
			students[i].grade = 'D';
		else
			students[i].grade = 'F';
	}
}

int topScore(const studentType students[]) {
	int highScore {};

	for (size_t i {}; i < MaxStud; ++i)
		if (students[i].testScore > highScore)
			highScore = students[i].testScore;

	return highScore;
}

void printInfo(std::ofstream& outfile, const studentType students[]) {
	for (size_t i {}; i < MaxStud; ++i)
		outfile << std::left << students[i].studentLname << ", " <<
			std::left << students[i].studentFname << " " << students[i].testScore <<
			" " << students[i].grade << '\n';
}

void printHighest(std::ofstream& outfile, const studentType students[]) {
	const auto high { topScore(students) };

	outfile << "\nHigh Score: " << high << '\n';

	for (size_t i {}; i < MaxStud; ++i)
		if (students[i].testScore == high)
			outfile << students[i].studentLname << ", " << students[i].studentFname << '\n';
}


Which generates an output file of:


Donald, Duckey 85 B
Goofy, Goof 89 B
Balto, Brave 93 A
Smitn, Snow 93 A
Wonderful, Alice 89 B
Akthar, Samina 85 B
Green, Simba 95 A
Egger, Donald 90 A
Deer, Brown 86 B
Jackson, Johny 95 A
Gupta, Greg 75 C
Happy, Samuel 80 B
Arora, Danny 80 B
June, Sleepy 70 C
Cheng, Amy 83 B
Malik, Shelly 95 A
Tomek, Chelsea 95 A
Clodfelter, Angela 95 A
Nields, Allison 95 A
Norman, Lance 88 B

High Score: 95
Green, Simba
Jackson, Johny
Malik, Shelly
Tomek, Chelsea
Clodfelter, Angela
Nields, Allison

Last edited on
thank you very much friend. why don't you use using namespace std? just preference?
https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

There are possible risks involved with having the using namespace std; directive in your code, risks that usually are not mentioned when learning C++.

C++ Core Guidelines, SF6 & SF7: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rs-using

Read the explanatory text before and after the SF6 sample.

Having the using namespace std; directive in code is not an error. The people who have it in their code presumably know the risks. Do you know what can happen?

It was originally supposed to be used with older C++ code that didn't have standard library namespaces and make it easier to transition that legacy code to newer C++ language versions.

There are safer using declarations that are less likely to have risks, using declarations.

https://www.learncpp.com/cpp-tutorial/using-declarations-and-using-directives/

There are a few select features of the C++ stdlib that require a using declaration to activate the desired feature.

A popular 3rd party library, Boost, is namespace structured that not having namespace declarations for the library is a pain with lots of repetitive typing.

The C++ Core Guidelines are suggestions, not "you must do this" requirements.

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#SS-aims

Personally I never have the using directive in my code, ever. I prefer to prefix std:: when using C++ stdlib features. Now it is automatic typing std::. I actually have to stop and think to NOT use it.

I do have using declarations on occasions, though I try to use them very sparingly. On a case by case basis.
Topic archived. No new replies allowed.