/* Name: Florah Charles Date: 02/21/2022 Assignment: Assignment#5 Due Date: 02/21/2022 About this project: This program will calculate the NFL passer rating for 2 quarterbacks using the real formula used in the National Football League. The program will display the passer rating for each quarterback, and then prints a message detailing who's rating was the best, and what the difference between the ratings was. The program will then allow the user to repeat this process, if they choose. Assumptions: Assumes correct user input All work below was performed by Florah Charles */ #include <iostream> #include <iomanip> using namespace std; double getPasserRating(int, int, int, int, int); double check(double); int main() { int att1, att2, com1, com2, py1, py2, td1, td2, inter1, inter2, total; double rating1, rating2; char compare; cout << "Welcome to the NFL Quarterback Passer Rating Calculator!" << endl; do{ cout << "\nEnter single game information for Player A:" << endl; cout << "Attempts: "; cin >> att1; cout << "Completions: "; cin >> com1; cout << "Passing Yards: "; cin >> py1; cout << "Touchdowns: "; cin >> td1; cout << "Interceptions: "; cin >> inter1; cout << "\nEnter single game information for Player B:" << endl; cout << "Attempts: "; cin >> att2; cout << "Completions: "; cin >> com2; cout << "Passing Yards: "; cin >> py2; cout << "Touchdowns: "; cin >> td2; cout << "Interceptions: "; cin >> inter2; rating1 = getPasserRating(att1, com1, py1, td1, inter1); rating2 = getPasserRating(att2, com2, py2, td2, inter2); cout << "\nPlayer A's single game passer rating: " << fixed << setprecision(1) << rating1 << endl; cout << "Player B's single game passer rating: " << fixed << setprecision(1) << rating2 << endl; total++; if (rating1 > rating2) { cout << "Player A was better than Player B by a difference of "; cout << rating1-rating2; if(rating1 == 158.3) { cout << "Player A had a PERFECT passer rating."; } } else if(rating2 > rating1) { cout << "Player B was better than Player A by a difference of "; cout << rating2-rating1; if(rating2 == 158.3) { cout << "Player B had a PERFECT passer rating."; } } else { cout << "Player A and B have the same rating!"; } cout << "\n \nWould you like to compare another pair of players? (Y or N) "; cin >> compare; } while(compare == 'Y' || compare == 'y'); if(compare == 'N' || compare == 'n') { if(total == 1) { cout << "You compared " << total << " pair of players."; } else { cout << "You compared " << total << " pairs of players."; } } else { cout << "Invalid entry. Exiting program..."; } return 0; } double check(double checking) { if(checking > 2.375) { checking = 2.375; } else if (checking < 0) { checking = 0; } return checking; } double getPasserRating(int att, int comp, int yds, int td, int inter) { double a, b, c, d, pr; a = (((comp/att) - 0.3)*5); b = (((yds/att) - 3)*0.25); c = ((td/att)*20); d = 2.375 - ((inter/att)*25); a = check(a); b = check(b); c = check(c); d = check(d); pr = ((a+b+c+d)/6)*100; return pr; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}