fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip> // For output formatting
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. // Variables to store the runners' names and their times
  9. string runner1, runner2, runner3;
  10. double time1, time2, time3;
  11.  
  12. // Ask for the names of the runners
  13. cout << "Enter the name of the first runner: ";
  14. getline(cin, runner1);
  15.  
  16. cout << "Enter the name of the second runner: ";
  17. getline(cin, runner2);
  18.  
  19. cout << "Enter the name of the third runner: ";
  20. getline(cin, runner3);
  21.  
  22. // Ask for the times it took each runner to finish
  23. cout << "Enter the time (in seconds) for " << runner1 << ": ";
  24. cin >> time1;
  25.  
  26. // Input validation for the time to be positive
  27. if (time1 <= 0) {
  28. cout << "Error: Time must be a positive number.\n";
  29. return 1;
  30. }
  31.  
  32. cout << "Enter the time (in seconds) for " << runner2 << ": ";
  33. cin >> time2;
  34.  
  35. // Input validation for the time to be positive
  36. if (time2 <= 0) {
  37. cout << "Error: Time must be a positive number.\n";
  38. return 1;
  39. }
  40.  
  41. cout << "Enter the time (in seconds) for " << runner3 << ": ";
  42. cin >> time3;
  43.  
  44. // Input validation for the time to be positive
  45. if (time3 <= 0) {
  46. cout << "Error: Time must be a positive number.\n";
  47. return 1;
  48. }
  49.  
  50. // Determine who came in first, second, and third place
  51. string first, second, third;
  52. double firstTime, secondTime, thirdTime;
  53.  
  54. // Find the first place
  55. if (time1 < time2 && time1 < time3) {
  56. first = runner1;
  57. firstTime = time1;
  58. if (time2 < time3) {
  59. second = runner2;
  60. secondTime = time2;
  61. third = runner3;
  62. thirdTime = time3;
  63. } else {
  64. second = runner3;
  65. secondTime = time3;
  66. third = runner2;
  67. thirdTime = time2;
  68. }
  69. } else if (time2 < time1 && time2 < time3) {
  70. first = runner2;
  71. firstTime = time2;
  72. if (time1 < time3) {
  73. second = runner1;
  74. secondTime = time1;
  75. third = runner3;
  76. thirdTime = time3;
  77. } else {
  78. second = runner3;
  79. secondTime = time3;
  80. third = runner1;
  81. thirdTime = time1;
  82. }
  83. } else {
  84. first = runner3;
  85. firstTime = time3;
  86. if (time1 < time2) {
  87. second = runner1;
  88. secondTime = time1;
  89. third = runner2;
  90. thirdTime = time2;
  91. } else {
  92. second = runner2;
  93. secondTime = time2;
  94. third = runner1;
  95. thirdTime = time1;
  96. }
  97. }
  98.  
  99. // Display the results
  100. cout << fixed << setprecision(2); // Set the time format to two decimal places
  101. cout << "\nRace Results:\n";
  102. cout << "-------------------------------------\n";
  103. cout << "1st place: " << first << " with a time of " << firstTime << " seconds.\n";
  104. cout << "2nd place: " << second << " with a time of " << secondTime << " seconds.\n";
  105. cout << "3rd place: " << third << " with a time of " << thirdTime << " seconds.\n";
  106.  
  107. return 0;
  108. }
Success #stdin #stdout 0s 5308KB
stdin
Standard input is empty
stdout
Enter the name of the first runner: Enter the name of the second runner: Enter the name of the third runner: Enter the time (in seconds) for : Enter the time (in seconds) for : Enter the time (in seconds) for : 
Race Results:
-------------------------------------
1st place:  with a time of 0.00 seconds.
2nd place:  with a time of 0.00 seconds.
3rd place:  with a time of 0.00 seconds.