-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA32studentinfo_individualwork.cpp
184 lines (160 loc) · 4.87 KB
/
A32studentinfo_individualwork.cpp
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
// Student Information - using pointers to structure
// By Emily Dayanghirang
#include <iostream>
#include <iomanip>
using namespace std;
struct Student
{
string name;
string idNum;
float* tests;
float average;
char grade;
};
Student setInformation(int);
void setGrade(Student&);
bool validName(string);
void display(const Student*, int, int);
int main()
{
Student* students;
int numOfTests, numOfStudents;
cout << "\nThe program will do the following by order: \n"
<< "(1) Ask the number of students you have\n"
<< "(2) Ask the number of tests taken by your students\n"
<< "(3) Ask for information of each student including test scores\n"
<< "(4) Average the test scores of each student\n"
<< "(5) Determine the grade of each student\n"
<< "(6) Display the information of each student\n";
cout << "\nNumber of students: ";
cin >> numOfStudents;
while(numOfStudents < 1)
{
cout << "Please enter an input of more than zero for the # of students: ";
cin >> numOfStudents;
}
while (cin.fail())
{
cin.clear();
cin.ignore(1000, '\n');
cout << "Please enter a valid integer value for the # of students: ";
cin >> numOfStudents;
}
cin.ignore();
students = new Student[numOfStudents];
cout << "\nEnter number of tests taken by your students: ";
cin >> numOfTests;
while(numOfTests < 1)
{
cout << "Please enter an input of more than zero for the # of tests: ";
cin >> numOfTests;
}
while (cin.fail())
{
cout << "Please enter a valid integer value for the # of tests: ";
cin.clear();
cin.ignore(1000, '\n');
cin >> numOfTests;
}
cin.ignore();
for (int index = 0; index < numOfStudents; index++)
{
cout << "\nEnter Student #" << (index + 1) << " information: " << endl;
*(students + index) = setInformation(numOfTests);
}
display(students, numOfStudents, numOfTests);
for (int index = 0; index < numOfStudents; index++)
delete [] (students + index)->tests;
delete [] students;
return 0;
}
Student setInformation(int numOfTests)
{
float total = 0;
Student studentInfo;
cout << "Enter student name: ";
getline(cin, studentInfo.name);
while (!validName(studentInfo.name))
{
cout << "\nPlease try again: ";
cin.clear();
cin.ignore(1000, '\n');
getline(cin, studentInfo.name);
}
cout << "Enter student ID: ";
getline(cin, studentInfo.idNum);
while (cin.fail())
{
cout << "Input error. Please try again: ";
cin.clear();
cin.ignore(1000, '\n');
getline(cin, studentInfo.idNum);
}
studentInfo.tests = new float[numOfTests];
for(int index = 0; index < numOfTests; index++)
{
cout << "Enter test #" << index + 1 << " score: ";
cin >> *(studentInfo.tests+index);
while (cin.fail())
{
cout << "Please enter a valid floating point value for the test score: ";
cin.clear();
cin.ignore(1000, '\n');
cin >> *(studentInfo.tests+index);
}
cin.ignore();
total += *(studentInfo.tests+index);
}
studentInfo.average = total/static_cast<float>(numOfTests);
setGrade(studentInfo);
return studentInfo;
}
void setGrade(Student& student)
{
if(student.average >= 90.0)
student.grade = 'A';
if(student.average >= 80.0 && student.average < 90.0)
student.grade = 'B';
if(student.average >= 70.0 && student.average < 80.0)
student.grade = 'C';
if(student.average >= 60.0 && student.average < 70.0)
student.grade = 'D';
if(student.average < 60.0)
student.grade = 'F';
}
bool validName(string name)
{
if(name.length() == 0)
{
cout << "Name cannot be empty.";
return false;
}
for(int i = 0; i < name.length(); i++)
{
if(name[i] >= '0' && name[i] <= '9')
{
cout << "\nName cannot contain numerical values.";
return false;
}
}
return true;
}
void display(const Student* student, int numOfStudents, int numOfTests)
{
cout << "\nStudent Information:";
for (int i = 0; i < numOfStudents; i++)
{
cout << "\n|--------------------------------------------|";
cout << "\n|Name: " << (student+i)->name;
cout << "\n|ID: " << (student+i)->idNum;
for(int index = 0; index < numOfTests; index++)
{
cout << "\n|Test #" << index + 1 << " score: " << fixed << setprecision(2)
<< *((student+i)->tests+index);
}
cout << "\n|Average: " << fixed << setprecision(2)
<< (student+i)->average;
cout << "\n|Grade: " << (student+i)->grade;
cout << "\n|--------------------------------------------|";
}
}