1: //--------------------------------------------------------------------- 2: // 3: // Name: Program 3, version 3; this version contains errors 4: // 5: // Course: CS 143, Fall 2000 6: // 7: // Purpose: This program is based on a game where the user has to input 8: // values that compute, to return a distance. The program has 9: // compute the distance from the value and return an answer to 10: // see if the user has won or lost in a set number of tries. 11: // The program keeps track of the tries, the games won, and 12: // the games played. Then the program displays the final 13: // results of the users games. 14: //--------------------------------------------------------------------- 15: 16: #include 17: #include 18: #include 19: 20: const int PRECISION = 3; 21: const int COUNTER_KICKOUT = 5; 22: const int UPDATED = 6; 23: const int RADIAN_MULTIPLIER = 2; 24: const int PERCENT_MULTIPLIER = 100; 25: const float PERCENT_OF_DEVIATION = 0.001; 26: const float VALUE_OF_PI = 3.14159265; 27: const float DEGREE_VALUE = 180.0; 28: const float RADIAN_DIVIDER = 32.2; 29: 30: float Radians (float degrees); 31: void Final_Stats (int &games_won, int &games_played); 32: void Missed_Results (int &count, int &games_won, int &games_played); 33: void Inner_Updating (int &count, int &strike); 34: void Get_Velocity (float &velocity); 35: void Get_Angle (float °rees, int &guess); 36: void Get_Distance (float &target_dist); 37: void Results (int &games_won, int &games_played); 38: void Distance_Off (float user_dist, float target_dist, float new_dist); 39: 40: void Update_or_Reset (int &games_played, int &count, int &guess, 41: int &strike); 42: 43: void Computed_Dist (float &velocity, float &user_dist, float &radians, 44: float °rees); 45: 46: void Display_Right_Or_Wrong (float target_dist, float high_range, 47: float low_range, int &games_won, 48: float user_dist, float new_dist, 49: int strike, int games_played, 50: float velocity, float degrees, 51: float radians, int &count); 52: 53: void Compute_Results (float target_dist, float high_range, 54: float low_range, int &games_won, float user_dist, 55: float new_dist, int strike, int games_played, 56: float velocity, float degrees, float radians, 57: int &count); 58: 59: void Body (float &target_dist, float &high_range, float &low_range, 60: int &games_won, float &user_dist, float &new_dist, 61: int &strike, int &games_played, float &velocity, 62: float °rees, float &radians, int &count, int &guess); 63: 64: int main() 65: { 66: int guess, count, strike, games_won = 0, games_played = 0; 67: float velocity, shooting_angle, target_dist, degrees = 0, 68: user_dist = 0, new_dist = 0, high_range = 0 , low_range = 0, 69: radians = 0; 70: 71: Get_Distance (target_dist); 72: 73: while (target_dist != 0) 74: { 75: Update_or_Reset (games_played, count, guess, strike); 76: while (count <= COUNTER_KICKOUT) 77: { 78: Body (target_dist, high_range, low_range, games_won, user_dist, 79: new_dist, strike, games_played, velocity, degrees, 80: radians, count, guess); 81: } 82: Missed_Results (count, games_won, games_played); 83: Get_Distance (target_dist); 84: } 85: Final_Stats (games_won, games_played); 86: return 0; 87: } 88: //--------------------------------------------------------------------- 89: // This function prompts then inputs the users desired distance to 90: // target. 91: // params: (out) 92: //--------------------------------------------------------------------- 93: void Get_Distance (float &target_dist) 94: { 95: cout << "Enter distance to target in feet (0 quit): "; 96: cin >> target_dist; 97: return; 98: } 99: //--------------------------------------------------------------------- 100: // This function prompts and inputs the user for an angle. Also it 101: // updates the value of the number of guesses. 102: // params: (out, inout) 103: //--------------------------------------------------------------------- 104: void Get_Angle (float °rees, int &guess) 105: { 106: cout << "Enter angle (in degrees) for guess " << guess << ": "; 107: cin >> degrees; 108: while (degrees <= 0) 109: { 110: cout << "Shooting into the ground will just make your feet wet!"; 111: cout << endl << "Please re-enter angle: "; 112: cin >> degrees; 113: } 114: guess++; 115: return; 116: } 117: //--------------------------------------------------------------------- 118: // This function prompts user for the velocity. 119: // parameters (out) 120: //--------------------------------------------------------------------- 121: void Get_Velocity (float &velocity) 122: { 123: cout << "Enter velocity (feet/sec): "; 124: cin >> velocity; 125: while (velocity <= 0) 126: { 127: cout << "Velocity must be positive; please re-enter it: "; 128: cin >> velocity; 129: } 130: return; 131: } 132: //--------------------------------------------------------------------- 133: // This function inputs the games won and games played. Then computes 134: // the percentage of games won and displays those results. 135: // params: (in, in) 136: //--------------------------------------------------------------------- 137: void Results (int &games_won, int &games_played) 138: { 139: float percent = 0; 140: if (games_played == 0) 141: percent = 0; 142: else 143: percent = (games_won / float(games_played)); 144: cout << setiosflags(ios::fixed) << setprecision(1) << endl << endl; 145: cout << "Score: " << games_won << " of " << games_played 146: << " games won (" << percent * PERCENT_MULTIPLIER 147: << "%)" << endl; 148: return; 149: } 150: //--------------------------------------------------------------------- 151: // This function converts degrees to radians. Then returns the 152: // conversion. 153: // params: (inout) 154: //--------------------------------------------------------------------- 155: float Radians (float degrees) 156: { 157: float radians = 0; 158: degrees = (degrees * VALUE_OF_PI) / DEGREE_VALUE; 159: return degrees; 160: } 161: //--------------------------------------------------------------------- 162: // This function computes the distance that the water traveled. 163: // params: (in, out, in, in) 164: //--------------------------------------------------------------------- 165: void Computed_Dist (float &velocity, float &user_dist, float &radians, 166: float °rees) 167: { 168: user_dist = 0; 169: user_dist = (velocity * velocity) * sin(RADIAN_MULTIPLIER * 170: Radians (degrees)) / RADIAN_DIVIDER; 171: return; 172: } 173: //--------------------------------------------------------------------- 174: // This function displays how much the computed distance is off from 175: // the desired distance. 176: // params: (in, in, out) 177: //--------------------------------------------------------------------- 178: void Distance_Off (float user_dist, float target_dist, float new_dist) 179: { 180: new_dist = target_dist - user_dist; 181: if (new_dist >= 0) 182: cout << new_dist << " feet too short."; 183: else 184: cout << new_dist * -1 << " feet too long."; 185: return; 186: } 187: //--------------------------------------------------------------------- 188: // This function outputs if the user hit the target or if he missed. 189: // Also it says how far he was off when he missed and it upadates the 190: // games played and the games won. 191: // prams: (in, out, out, inout, in, in, in, inout, in, in, in, inout) 192: //--------------------------------------------------------------------- 193: void Compute_Results (float target_dist, float high_range, 194: float low_range, int &games_won, float user_dist, 195: float new_dist, int strike, int games_played, 196: float velocity, float degrees, float radians, 197: int &count) 198: { 199: cout << setiosflags(ios::fixed) << setprecision(PRECISION); 200: high_range = (target_dist * PERCENT_OF_DEVIATION) + target_dist; 201: low_range = target_dist - (target_dist * PERCENT_OF_DEVIATION); 202: cout << "Strike " << strike << " goes " << user_dist << " feet." 203: << endl; 204: Display_Right_Or_Wrong (target_dist, high_range, low_range, 205: games_won, user_dist, new_dist, 206: strike, games_played, velocity, 207: degrees, radians, count); 208: cout << endl; 209: return; 210: } 211: //--------------------------------------------------------------------- 212: // This function updates the games played and set the values of the other 213: // variable used in the counters and program. 214: // params: (inout, out, out, out) 215: //--------------------------------------------------------------------- 216: void Update_or_Reset (int &games_played, int &count, int &guess, 217: int &strike) 218: { 219: games_played++; 220: count = 1; 221: guess = 1; 222: strike = 1; 223: return; 224: } 225: //--------------------------------------------------------------------- 226: // This function updates the inner while loop variables. 227: // params: (inout, inout) 228: //--------------------------------------------------------------------- 229: void Inner_Updating ( int &count, int &strike) 230: { 231: count++; 232: strike++; 233: return; 234: } 235: //--------------------------------------------------------------------- 236: // This function displays the final results of the totals of winning 237: // games, total games played, and total of percentage won. This 238: // function calls the function Results that was stated earlier. 239: // params: (in, in) 240: //--------------------------------------------------------------------- 241: void Final_Stats (int &games_won, int &games_played) 242: { 243: cout << endl << "FINAL RESULTS"; 244: Results (games_won, games_played); 245: return; 246: } 247: //--------------------------------------------------------------------- 248: // This function shows that the user missed on all tries allowed and 249: // displays the perentage of games won and games played. 250: // params: (in, in, in) 251: //--------------------------------------------------------------------- 252: void Missed_Results (int &count, int &games_won, int &games_played) 253: { 254: if (count == UPDATED) 255: { 256: cout << "Sorry, you missed on all " << COUNTER_KICKOUT << " tries."; 257: Results (games_won, games_played); 258: cout << endl; 259: } 260: return; 261: } 262: //--------------------------------------------------------------------- 263: // This function calls the: Get_Angle, Get_Velocity, Compute_Dist, 264: // Compute_Results, and Inner_Updating. This helps to make main shorter. 265: // params: (in, out, out, inout, in, in, in, inout, in, in, in, inout, 266: // inout) 267: //--------------------------------------------------------------------- 268: void Body (float &target_dist, float &high_range, float &low_range, 269: int &games_won, float &user_dist, float &new_dist, 270: int &strike, int &games_played, float &velocity, 271: float °rees, float &radians, int &count, 272: int &guess) 273: { 274: Get_Angle (degrees, guess); 275: Get_Velocity (velocity); 276: Computed_Dist (velocity, user_dist, radians, degrees); 277: Compute_Results (target_dist, high_range, low_range, games_won, 278: user_dist, new_dist, strike, games_played, velocity, 279: degrees, radians, count); 280: Inner_Updating (count, strike); 281: return; 282: } 283: //--------------------------------------------------------------------- 284: // This function prints out if the user hint the target of missed. 285: // params: (in, in, inout, in, in, in, in, inout) 286: //--------------------------------------------------------------------- 287: void Display_Right_Or_Wrong (float target_dist, float high_range, 288: float low_range, int &games_won, 289: float user_dist, float new_dist, 290: int strike, int games_played, 291: float velocity, float degrees, 292: float radians, int &count) 293: { 294: if (low_range <= user_dist && user_dist <= high_range) 295: { 296: games_won++; 297: count = UPDATED; 298: cout << "**** YOU HIT THE TARGET! ****"; 299: Results (games_won, games_played); 300: } 301: else 302: { 303: cout << "You missed: you were "; 304: Distance_Off (user_dist, target_dist, new_dist); 305: } 306: return; 307: }