fork download
  1. //Diego Martinez CSC5 Chapter 4, P.223,#14
  2. /*******************************************************************************
  3. * RANKING RUNNERS BY FASTEST TIME
  4. * ______________________________________________________________________________
  5. * The program asks the user to enter the names of three runners and the time
  6. * each one took to finish a race. It ensures that all entered times are positive
  7. * numbers by validating the input and prompting the user again if an invalid
  8. * value is entered.
  9. *
  10. *
  11. * Computation is based on the Formula:
  12. * Order runners by increasing time:
  13. * smallest → 1st
  14. * middle → 2nd
  15. * largest → 3rd
  16. * ____________________________________________________________________________
  17. * INPUT
  18. * Runner 1 : name and time finished
  19. * Runner 2 : name and time finished
  20. * Runner 3 : name and time finished
  21. * OUTPUT
  22. * 1st Place : runner with the fastest time
  23. * 2nd Place : runner with the second fastest time
  24. * 3ed Place : runner with the slowest time
  25. *******************************************************************************/
  26. #include <iostream>
  27. #include <string>
  28. using namespace std;
  29.  
  30. int main() {
  31. string name1, name2, name3;
  32. double time1, time2, time3;
  33.  
  34. // Input names
  35. cout << "Enter the name of runner 1: ";
  36. getline(cin, name1);
  37.  
  38. cout << "Enter the name of runner 2: ";
  39. getline(cin, name2);
  40.  
  41. cout << "Enter the name of runner 3: ";
  42. getline(cin, name3);
  43.  
  44. // Input times with validation
  45. cout << "Enter the time for " << name1 << ": ";
  46. cin >> time1;
  47. while (time1 <= 0) {
  48. cout << "Invalid input. Time must be a positive number. Enter again: ";
  49. cin >> time1;
  50. }
  51.  
  52. cout << "Enter the time for " << name2 << ": ";
  53. cin >> time2;
  54. while (time2 <= 0) {
  55. cout << "Invalid input. Time must be a positive number. Enter again: ";
  56. cin >> time2;
  57. }
  58.  
  59. cout << "Enter the time for " << name3 << ": ";
  60. cin >> time3;
  61. while (time3 <= 0) {
  62. cout << "Invalid input. Time must be a positive number. Enter again: ";
  63. cin >> time3;
  64. }
  65.  
  66. // Determine order
  67. string first, second, third;
  68.  
  69. if (time1 <= time2 && time1 <= time3) {
  70. first = name1;
  71. if (time2 <= time3) {
  72. second = name2;
  73. third = name3;
  74. } else {
  75. second = name3;
  76. third = name2;
  77. }
  78. }
  79. else if (time2 <= time1 && time2 <= time3) {
  80. first = name2;
  81. if (time1 <= time3) {
  82. second = name1;
  83. third = name3;
  84. } else {
  85. second = name3;
  86. third = name1;
  87. }
  88. }
  89. else {
  90. first = name3;
  91. if (time1 <= time2) {
  92. second = name1;
  93. third = name2;
  94. } else {
  95. second = name2;
  96. third = name1;
  97. }
  98. }
  99.  
  100. // Output results
  101. cout << "\nRace Results:\n";
  102. cout << "1st place: " << first << endl;
  103. cout << "2nd place: " << second << endl;
  104. cout << "3rd place: " << third << endl;
  105.  
  106. return 0;
  107. }
Success #stdin #stdout 0.01s 5312KB
stdin
Oscar 
Jen 
Bill
5
8
4
stdout
Enter the name of runner 1: Enter the name of runner 2: Enter the name of runner 3: Enter the time for Oscar : Enter the time for Jen : Enter the time for Bill: 
Race Results:
1st place: Bill
2nd place: Oscar 
3rd place: Jen