-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule7SemesterProjectA-Section7.cpp
More file actions
456 lines (359 loc) · 15.2 KB
/
Copy pathModule7SemesterProjectA-Section7.cpp
File metadata and controls
456 lines (359 loc) · 15.2 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//************************************************************************
// Author: Rolando Rey Carreon
// Date: mar 28 2025
// Language: C++
// Assignment: Project A - Section 7
// Description: In this section, i will create a contact card structure
// The structure will be pre-populated with values and stored
// in an array of structures.
//************************************************************************
/* MODIFICATIONS
{ Module 1
created a project/ workspace for my course project
created a welcome message header
prompted the user for their first and last name
outputted the welcome message with their name
} feb 7 2025
{ Module 2
prompted the user to make a selection from 1 to 3
prompted the use to enter 0 to exit the program
selection 1 will ask for a number to calculate the square root
selection 2 will ask for a base and exponent to calculate the power
selection 3 will ask for a GPA value to convert to a letter grade
selection 0 will exit the program
} feb 21 2025
{ Module 3
wrapped the menu in a loop to continue executing until 0 is selected
created selection 4 to allow user to create a right triangle
enhanced the menu so that it is visually contained within a box
} mar 7 2025
{ Module 4
refactored selections 3 and 4 to be user defined void functions
created selection 5 to calculate weekly pay
selection 5 will prompt the user for hours worked and pay rate,
then calculate the pay based on 40 hours at normal rate and overtime
create selection 6 to calculate interest earned
selection 6 will prompt the user for a balance and interest rate,
then display the balance for the next 5 years
} mar 28 2025
{ Module 5
created a void function to display the header that outputs the course
created a function that takes 2 string parameters by reference
to refactor the prompt the user for their first and last name
} apr 10 2025
{ Module 6
created an array of 5 grades in main
add selection 7 to allow the user to calculaye a quiz grade average
created a void function that accepts the array of grades,
the function will request each grade from the user
created a void function that will display each of the grades in the array
creted a function that will return double the average of the grade array
selection 7 will manage the grades, when selected ask for the grades,
then show the grades and show the average.
} apr 25 2025
{ Module 7
created a struct called 'Contacts' with 3 attributes like name and number
populated elements of an array called 'arrayContacts' with attributes
created a void function called 'printContacts' that will take the array
as a parameter and print the list of contacts that are pre-populated
added option 8 to the menu to display the contacts
moved case 7 and added case 0 to be before defaut case
fixed case 6 to get the balnce and rate and pass to the display function
} may 9 2025
*/
#include<iostream>
#include<iomanip> // added iomanip for set precision
#include<cmath> // added cmath for sqrt and pow functions
using namespace std;
// defined structure for contacts case 8
struct Contacts
{
string firstName;
string lastName;
string phoneNumber;
};
// function prototypes
void displayHeader();
void requestName( string&, string& );
void convertGPA(); // case 3
void rightTri( ); // case 4
double calculatePay(int&, double&); // case 5
void displayInterest(double&, double&); // case 6
// option case 7 function prototypes
void getGrades(double[]);
void displayGrades(double[]);
double averageGrades(double[]);
void printContacts( Contacts pContact ); // case 8
int main()
{
// declare local variables
// welcome message
string firstName, lastName;
// main menu
int menuChoice;
int valueOne, valueTwo;
int result;
//case 5
int hours;
double rate;
// case 6
double balance;
double interestRate;
// case 7
double grades[5]; // array of 5 grades
// case 8
Contacts arrayContacts [3] = // array of 3 contacts with data
{
{"Scott", "Raymond", "(210)563-1234"},
{"Fred", "Flintstone", "(210)622-5678"},
{"Sally", "Smith", "(210)555-9753"}
};
// display course info header and prompt for first and last name
displayHeader();
requestName( firstName, lastName );
cout << "Welcome to my course project " << firstName << "!\n"
<< "****************************************" << endl;
// prompt menu that asks the user to choose a listed choice
// always reprompts till user exits with '0'
do
{
cout << "|----------------------------------------------------|\n"
<< "| Enter 1 to Calculate Square Root |\n"
<< "| Enter 2 to Enter X to the Y Power |\n"
<< "| Enter 3 to Convert Numerical GPA to a Letter Grade |\n"
<< "| Enter 4 to Create a Right Triangle |\n"
<< "| Enter 5 to Calculate Weekly Pay |\n"
<< "| Enter 6 to Calculate Interest Earned |\n"
<< "| Enter 7 to Calculate Grades |\n"
<< "| Enter 8 to Print Contacts |\n"
<< "| Enter 0 to Exit |\n"
<< "|----------------------------------------------------|\n"
<< "Enter Your Request: ";
cin >> menuChoice;
// switch containing 10 menu items and default case
switch ( menuChoice )
{
// menu choice 1 prompts for one value to square root
case 1:
cout << "Enter a number to calculate the Square Root: ";
cin >> valueOne;
// square root formula saves to result int
result = sqrt( valueOne );
// displays square root result
cout << "The Square Root of " << valueOne << " is "
<< result << endl;
break;
// menu choice 2 prompts for one base and one exponent value
case 2:
cout << "Enter a number X to raise to the Y Power\n"
<< "Enter X: ";
cin >> valueOne;
cout << "Enter Y: ";
cin >> valueTwo;
// exponent formula saves to result int
result = pow( valueOne, valueTwo) ;
// displays exponent result
cout << valueOne << " to the " << valueTwo << " power is "
<< result << endl;
break;
// menu choice 3 prompts for GPA value function
case 3:
convertGPA();
break;
// menu choice 4 prompts for right triangle function
case 4:
rightTri();
break;
// menu choice 5 prompts for weekly pay calculation
case 5:
// prompt for hours
cout << "Enter Hours: ";
cin >> hours;
// prompt for rate
cout << "Enter Pay Rate: ";
cin >> rate;
cout << "Weekly Pay is $" << calculatePay(hours, rate)
<< endl; // display calculated pay using the function
break;
// menu choice 6 prompts for interested earned calculation
case 6:
// prompt for balance
cout << "Enter Initial Balance: ";
cin >> balance;
// prompt for rate
cout << "Enter yearly Interest Rate: ";
cin >> interestRate;
displayInterest(balance, interestRate);
break;
// menu choice 7 - calculate grades
case 7:
cout << "Grade Calculator:\n";
getGrades(grades); // call to get grades function
displayGrades(grades); // call to display grades function
cout << "The grade average is " << averageGrades(grades)
<< endl;// display calculated average using the function
break;
// menu choice 8 - display contacts
case 8:
// display the contact's details
cout << "Contact List:" << endl;
for( Contacts contact : arrayContacts ) // display the contact list details
printContacts( contact );
break;
// menu choice 6 prompts for interested earned calculation
case 0:
cout << "Thank you for using my program!" << endl;
break;
// choice 0 exit statment displayed if no valid options chosen
default:
cout << "Invalid Entry" << endl << endl;
break;
} // end of switch statment
} while ( menuChoice != 0 ); // reprompts intil user selects 0
return 0;
}
// function definitions
// void function to display the header
void displayHeader()
{
// output the header when this function is called
cout << "****************************************\n"
<< "* Programming 1 Course Project *\n"
<< "****************************************\n";
}
// reference function handles to request for the user's first and last
// name. alters the values of the first and last name variables
void requestName(string& firstName, string& lastName)
{
cout << "Please enter your first name: ";
cin >> firstName; // user defined pass by reference value
cout << "Please enter your last name: ";
cin >> lastName; // user defined pass by reference value
}
// option case 3 - convertt numerical GPA to letter grade
void convertGPA()
{
double GPA; // user defined GPA value
cout << "Enter your numerical Grade (GPA): ";
cin >> GPA;
// determines 5 letter grade base on user input
if ( GPA >= 3.7 ) // grade value >= 3.7 list as A
cout << "Letter Grade is an A" << endl;
else if ( GPA >= 2.7 ) // grade value >= 2.7 list as B
cout << "Letter Grade is an B" << endl;
else if ( GPA >= 1.7 ) // grade value >= 1.7 list as C
cout << "Letter Grade is an C" << endl;
else if ( GPA >= 0.7) // grade value >= 0.7 list as D
cout << "Letter Grade is an D" << endl;
else // all grade values under 0.7 list as F
cout << "Letter Grade is an F" << endl;
}
// option case 4 - create a right triangle
void rightTri()
{
int row, col; // displays '*' until user defined valueOne limit
int valueOne;
cout << "Enter the size of the triangle: ";
cin >> valueOne;
// nested loop to display the right side triagnle size
// row line height determined by user
for ( row = 1; row <= valueOne; row++ )
{
// collumn displaying '*' determined by row hieght
for ( col = 1; col <= row; col++ )
cout << "*";
// go to the next row
cout << endl;
}
}
// option case 5 - calculate weekly pay
// prompt the user for the number of hours worked and hourly pay.
// overtime will be calculated at 1.5 times rate for each hour over 40 hours
// takes 2 parameters and returns a double
double calculatePay(int& hours, double& rate)
{
// local variables
int overtime = 0; // initialized to start at 0 hours worked
double calculatedPay; // calculated overtime pay owed
// set the output to two decimal places
cout << fixed << showpoint << setprecision(2);
// if hours worked is less than or equal to 40 hours then calculate pay
//
if ( hours <= 40 )
calculatedPay = hours * rate;
else
{
overtime = hours - 40; // calc overtime hours by subtraction
calculatedPay = ( 40 * rate ) + // normal rate for 40 hours
( overtime * rate * 1.5 ); // overtime rate afterhrs
} // end of if else
return calculatedPay; // return the calculated pay
}
// option case 6 - calculate interest earned
// display the balance of the savings acount for the nect five years
void displayInterest(double& balance, double& interestRate)
{
// local varible
int year;
// display interest using a for loop instead
// intialized at year 1, repeats or loop until year is equal 5
for ( year = 1; year <= 5; year++ )
{
balance += balance * interestRate; // interest times yearly bal.
cout << "Year " << year << ": $" << balance << endl; // display bal
}
}
// option case 7 - calculate grades
// void function that accepts the array as a parameter by reference,
// the function will request each grade from the user and populate the array
void getGrades(double grades[])
{
// loop to request 5 grades from the user to array
for ( int i = 0; i < 5; i++ )
{
// prompt for each grade, each loop incraeses index number displayed
cout << "Enter grade " << (i + 1) << ": ";
cin >> grades[i]; // user populate the each array with grade
}
}
// option 7 continued - calculate grades
// void function that will display each of the grades in the array
void displayGrades(double grades[])
{
// set the output to two decimal places
cout << fixed << showpoint << setprecision(2);
// loop to display 5 grades from the user array
for ( int i = 0; i < 5; i++ )
{
// display each grade, each loop incraeses index number displayed
cout << "\nGrade " << (i + 1) << ": "
<< grades[i]; // display each grade
}
cout << endl;
}
// option 7 continued - calculate grades
// create a function that will return double the average of the grade array
// the function will take the array of grades as a parameter
double averageGrades(double grades[])
{
double total = 0; // initialized/ reset total variable to 0
double average; // decalre varible to hold the average
// set the output to two decimal places
cout << fixed << showpoint << setprecision(2);
// loop throu each array to calculate the average of the grades
for ( int i = 0; i < 5; i++ )
{
total += grades[i]; // add each grade to the total
}
average = total / 5; // divide the total by the number of grades (5)
return average; // return the average back to main
}
// option case 8 - print contacts
// create a void function called printContacts that will take the array
// as a parameter and print out the list of contacts that i have
// pre-populated
void printContacts( Contacts pContact )
{
cout << "Name: " << pContact.firstName << " " << pContact.lastName
<< endl << "Phone: " << pContact.phoneNumber << endl << endl;
}