-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA32studentinfo_groupwork.cpp
169 lines (147 loc) · 4.48 KB
/
A32studentinfo_groupwork.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
// Course grade - using pointers to structure
// By Alina Corpora, Emily Dayanghirang, and Sarra Osman
#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<<"\nHow many students are you inputting data for? : ";
cin>>numOfStudents;
while(numOfStudents<1)
{
cin.clear();
cin.ignore(100, '\n');
cout<<"Please enter a value above 0. : ";
cin>>numOfStudents;
}
cout<<"How many tests did each students take? : ";
cin>>numOfTests;
while(numOfTests<1)
{
cin.clear();
cin.ignore(100, '\n');
cout<<"Please enter a value above 0. : ";
cin>>numOfTests;
}
cin.ignore();
students = new Student [numOfStudents];
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* students, int numOfStudents, int numOfTests)
{
cout << left << setw(15) << "Name" << setw(15) << "ID";
for (int i = 0; i < numOfTests; i++)
cout << "Test " << setw(10) << (i + 1);
cout << setw(15) << "Average Score" << setw(15) << "Grade" << endl;
cout << setprecision(2) << fixed;
for (int i = 0; i < numOfStudents; i++)
{
cout << setw(15) << left << (students+i)->name << setw(15) << (students+i)->idNum;
for (int j = 0; j < numOfTests; j++)
{
cout << setw(15) << *((students+i)->tests+j);
}
cout << setw(15) << (students+i)->average << setw(15) << (students+i)->grade << endl;
}
}