Created
January 22, 2017 17:53
-
-
Save TechNinjaWeb/a3b5b86c24dc190f23389dd58844c128 to your computer and use it in GitHub Desktop.
Archers End Game Tracker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /****************************************************************************** | |
| @filename: Archers_End_Game_Score_Tracker.cpp | |
| @author: TechNinja | |
| @date: January 15, 2017 | |
| @description: 3 Archers will compete in 4 rounds/ends. Each end will be | |
| tracked and scored out of 60. The program will display each | |
| archer's average end score and the overall match average. | |
| @credits: http://www.cplusplus.com/forum/articles/9645/ | |
| *****************************************************************************/ | |
| /****************************************************************************** | |
| Main Includes | |
| *****************************************************************************/ | |
| # include <iostream> | |
| # include <iomanip> | |
| # include <sstream> // To String method dependency | |
| using namespace std; | |
| /****************************************************************************** | |
| Global Declarations | |
| *****************************************************************************/ | |
| // Constants | |
| const int NUMBER_OF_ENDS = 4; | |
| const int NUMBER_OF_ARCHERS = 3; | |
| const int MINIMUM_END_SCORE = 0; | |
| const int MAXIMUM_END_SCORE = 60; | |
| // Variables | |
| int archersTotalEndScore = 0; | |
| double overallAverageEndScore = 0.0; | |
| /****************************************************************************** | |
| Helper Functions | |
| *****************************************************************************/ | |
| // Clear Console | |
| void clearTheConsole() | |
| { | |
| cin.clear(); | |
| cin.sync(); | |
| } | |
| // Set Console Default Formatting Options | |
| void setConsoleDefaults() | |
| { | |
| cout << setprecision(2); | |
| } | |
| // Converts integer to string | |
| string convertIntToString(int value) | |
| { | |
| ostringstream ss; | |
| long num = value; | |
| ss << num; | |
| // Return converted string | |
| return ss.str(); | |
| } | |
| /****************************************************************************** | |
| Primary Functions | |
| *****************************************************************************/ | |
| // Validate the archer's end score and return a boolean upon completion | |
| // Prompt user of range error if falsy | |
| bool archersEndScoreIsWithinRange(int archersEndScore) | |
| { | |
| bool isWithinRange = false; | |
| if (archersEndScore >= MINIMUM_END_SCORE && archersEndScore <= MAXIMUM_END_SCORE) | |
| { | |
| isWithinRange = true; | |
| } | |
| else | |
| { | |
| cout << "You must enter a score from " << MINIMUM_END_SCORE | |
| << " to " << MAXIMUM_END_SCORE << " inclusive!" | |
| << endl << endl; | |
| } | |
| return isWithinRange; | |
| } | |
| // Validate the users input to an integer and return a boolean | |
| // Prompt the user of invalid response and clear the console | |
| bool nonNumericInputDetected() | |
| { | |
| bool hasFailed = false; | |
| if (cin.fail()) | |
| { | |
| hasFailed = true; | |
| cout << "You must enter a valid whole number!" << endl << endl; | |
| } | |
| clearTheConsole(); // Clear input buffera | |
| return hasFailed; | |
| } | |
| // Get, validate and return the archer's end score | |
| int getArchersEndScore(int endNumber) | |
| { | |
| int archersEndScore = 0; | |
| // Get end score from user input | |
| do | |
| { | |
| cout << "Enter your score for end #" + convertIntToString(endNumber) + ": "; | |
| cin >> archersEndScore; | |
| } while(!archersEndScoreIsWithinRange(archersEndScore) || nonNumericInputDetected()); // Loop getting endScore if not numeric or within range | |
| return archersEndScore; | |
| } | |
| // Handle outputting the end score | |
| void displayEndScore(string heading, double averageEndScore) | |
| { | |
| cout << endl << heading << " End Score is => " | |
| << averageEndScore << endl << endl; | |
| } | |
| // Game mechanics | |
| void startTheGame() | |
| { | |
| // Get End Scores for each archer | |
| for (int archersIndex = 1; archersIndex <= NUMBER_OF_ARCHERS; archersIndex++) | |
| { | |
| // Declare a temporary string to represent the archers name/position | |
| string currentArcher = "Archer #" + convertIntToString(archersIndex) + "'s"; | |
| for (int currentEnd = 1; currentEnd <= NUMBER_OF_ENDS; currentEnd++) | |
| { | |
| // Get end score and add it to the archer's total | |
| archersTotalEndScore += getArchersEndScore(currentEnd); | |
| } | |
| // Add archers total to the overall average | |
| overallAverageEndScore += archersTotalEndScore; | |
| // Output the Total End Score | |
| displayEndScore(currentArcher, archersTotalEndScore); | |
| // Reset the average | |
| archersTotalEndScore = 0; | |
| } | |
| // Finalize Overall Average End Score Calculations | |
| overallAverageEndScore /= NUMBER_OF_ARCHERS; | |
| // Display overall average end score | |
| displayEndScore("Overall Average", overallAverageEndScore); | |
| } | |
| /****************************************************************************** | |
| Main Function | |
| *****************************************************************************/ | |
| int main() | |
| { | |
| // Set the console defaults | |
| setConsoleDefaults(); | |
| // Begin game | |
| startTheGame(); | |
| // End Program | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment