fork download
  1. //Max Kichuk CS1A Chapter 4, P. 223, #14
  2. //
  3. /****************************************************************
  4.  *
  5.  * FINDS FIRST, SECOND, AND THIRD PLACE RUNNERS IN A RACE
  6.  * ______________________________________________________________
  7.  * Asks the user to enter the times and names of three runners
  8.  * and then displays who came in first, second, and third
  9.  * according to their times.
  10.  * ______________________________________________________________
  11.  *
  12.  * INPUT
  13.  * nameOfRunner1 : name of first runner
  14.  * nameOfRunner2 : name of second runner
  15.  * nameOfRunner3 : name of third runner
  16.  *
  17.  * timeOfRunner1 : time of first runner
  18.  * timeOfRunner2 : time of second runner
  19.  * timeOfRunner3 : time of third runner
  20.  *
  21.  * OUTPUT
  22.  * places of the runners according to times
  23.  *
  24.  **************************************************************/
  25.  
  26. #include <iostream>
  27. #include <iomanip>
  28. #include <string>
  29. using namespace std;
  30.  
  31. int main() {
  32. //Declaring program variables
  33. string nameOfRunner1; // INPUT : name of first runner
  34. string nameOfRunner2; // INPUT : name of second runner
  35. string nameOfRunner3; // INPUT : name of third runner
  36.  
  37. float timeOfRunner1; // INPUT : time of first runner
  38. float timeOfRunner2; // INPUT : time of second runner
  39. float timeOfRunner3; // INPUT : time of third runner
  40.  
  41. // INPUT : Initialize variables
  42. cout << "Enter the name of the first runner and their time ";
  43. cout << "separated by a space.\n";
  44. cin >> nameOfRunner1 >> timeOfRunner1;
  45.  
  46. cout << "Enter the name of the second runner and their time ";
  47. cout << "separated by a space.\n";
  48. cin >> nameOfRunner2 >> timeOfRunner2;
  49.  
  50. cout << "Enter the name of the third runner and their time ";
  51. cout << "separated by a space.\n";
  52. cin >> nameOfRunner3 >> timeOfRunner3;
  53.  
  54. // Checks if any of the values are negative.
  55. if ((timeOfRunner1 < 0) || (timeOfRunner2 < 0) || (timeOfRunner3 < 0)) {
  56. cout << "\nYou cannot have negative values for time! Please try again.";
  57. return 0;
  58. }
  59.  
  60. // OUTPUT : checks how many books were purchased and returns points
  61. if (timeOfRunner1 > timeOfRunner2) {
  62. if (timeOfRunner1 > timeOfRunner3) {
  63. if (timeOfRunner2 > timeOfRunner3) {
  64. cout << "First Place: " << setw(10) << nameOfRunner3 << endl;
  65. cout << "Second Place: " << setw(10) << nameOfRunner2 << endl;
  66. cout << "Third Place: " << setw(10) << nameOfRunner1 << endl;
  67. }
  68.  
  69. else {
  70. cout << "First Place: " << setw(10) << nameOfRunner2 << endl;
  71. cout << "Second Place: " << setw(10) << nameOfRunner3 << endl;
  72. cout << "Third Place: " << setw(10) << nameOfRunner1 << endl;
  73. }
  74. }
  75.  
  76. else {
  77. cout << "First Place: " << setw(10) << nameOfRunner2 << endl;
  78. cout << "Second Place: " << setw(10) << nameOfRunner1 << endl;
  79. cout << "Third Place: " << setw(10) << nameOfRunner3 << endl;
  80. }
  81. }
  82. else {
  83. if (timeOfRunner2 > timeOfRunner3) {
  84. if (timeOfRunner3 > timeOfRunner1) {
  85. cout << "First Place: " << setw(10) << nameOfRunner1 << endl;
  86. cout << "Second Place: " << setw(10) << nameOfRunner3 << endl;
  87. cout << "Third Place: " << setw(10) << nameOfRunner2 << endl;
  88. }
  89. else {
  90. cout << "First Place: " << setw(10) << nameOfRunner3 << endl;
  91. cout << "Second Place: " << setw(10) << nameOfRunner1 << endl;
  92. cout << "Third Place: " << setw(10) << nameOfRunner2 << endl;
  93. }
  94. }
  95. else {
  96. cout << "First Place: " << setw(10) << nameOfRunner1 << endl;
  97. cout << "Second Place: " << setw(10) << nameOfRunner2 << endl;
  98. cout << "Third Place: " << setw(10) << nameOfRunner3 << endl;
  99. }
  100. }
  101.  
  102.  
  103.  
  104. return 0;
  105. }
Success #stdin #stdout 0.01s 5308KB
stdin
Standard input is empty
stdout
Enter the name of the first runner and their time separated by a space.
Enter the name of the second runner and their time separated by a space.
Enter the name of the third runner and their time separated by a space.
First Place:            
Second Place:           
Third Place: