-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo.NestedStructuresStudentGradeInfo.cpp
More file actions
160 lines (120 loc) · 4.48 KB
/
Copy pathDemo.NestedStructuresStudentGradeInfo.cpp
File metadata and controls
160 lines (120 loc) · 4.48 KB
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
//************************************************************************
// Author: Rolando Carreon
// Date: apr 24 2025
// Language: C++
// Assignment: Demo
// Description: Program to manage student info uncluding name, grades,
// and degree program.
//************************************************************************
#include<iostream>
#include<string> // to use getline
#include<iomanip> // to use setprecision
using namespace std;
// define strcutures
struct Name
{
string firstName;
char middleInitial;
string lastName;
};
struct Grades
{
double homework;
double midTermExam;
double finalExam;
};
// nested structure
struct Student
{
Name studentName;
string program;
Grades studentGrades;
};
// use protoypes after strcuts to include them
// function prototypes
// passes the student and name structures by reference
void addStudentData(Student&, Name&);
void printStudentData(Student);
void printGradesAverage(Grades);
int main()
{
// declare local variables
Student studentArray[3];
Name nameEntered;
// display the program purpose
cout << "This program holds the name, program, and grades of "
<< "3 students\n\n";
// loop to get the name data for the 3 students, then call the
// addStudent function with the name and get the rest of the data
for (int i = 0; i < 3; i++)
{
// get the 3 parts of the name
cout << "Enter the first name: ";
cin >> nameEntered.firstName;
cout << "Enter the middle initial: ";
cin >> nameEntered.middleInitial;
cout << "Enter the last name: ";
cin >> nameEntered.lastName;
// call the function to add the student and get the remaining data
// taking one element (Array index) , passing to addStudent function
addStudentData(studentArray[i], nameEntered);
}
// loop to get the program and grades for the 3 students
// display the students information
// print the student data
// for each student, display student array data
for (Student student : studentArray)
printStudentData(student);
return 0;
}
// function definitions
// this function will add the name to the student element,then get
// the program and grades information and add them to the student element
void addStudentData(Student& pStudent, Name& pName)
{
// copy the name to the array element
pStudent.studentName = pName;
// not needed as .studentName is a structure that holds firstName
// pStudent.studentName.firstName = pName.firstName;
cin.ignore(); // clear the buffer/ clear the input stream for new input
// get the program from the user
cout << "What program is " << pName.firstName << " in: ";
//programs can use mulitple words so we use getline
// requires #inlcude<string>
getline(cin, pStudent.program);
// get the grade data
cout << "Enter the Homework, Mid-Term Exam, and Final Exam grades: ";
// gets stored in the homework element of the grades element in the
// student element and so on
cin >> pStudent.studentGrades.homework
>> pStudent.studentGrades.midTermExam
>> pStudent.studentGrades.finalExam;
// alswyas use multple prompts, so user knows what to enter
// this way comes in handy for reading files
cout << endl;
}
// fucntion to display the student data
void printStudentData(Student pStudent)
{
cout << pStudent.studentName.firstName << " "
<< pStudent.studentName.middleInitial << ". "
<< pStudent.studentName.lastName << endl;
cout << "\tProgram: " << pStudent.program << endl;
cout << "\t**** Grades ****\n";
printGradesAverage(pStudent.studentGrades);
}
//display the average of the grades
void printGradesAverage(Grades pGrades)
{
// declare local variables
// use 3.0 to make the answer a double instead of casting later
double courseAvg = (pGrades.homework + pGrades.midTermExam +
pGrades.finalExam) / 3.0;
// set the output formatting to 2 decimals places for percentages
cout << fixed << showpoint << setprecision(2);
// display the Grades
cout << "\t Homework: " << pGrades.homework << endl
<< "\t Mid-Term Exam: " << pGrades.midTermExam << endl
<< "\t Final Exam: " << pGrades.finalExam << endl
<< "\t Course Average: " << courseAvg << endl << endl;
}