Guest User

Untitled

a guest
Dec 16th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.54 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.    
  6.     // Declare all the variables we need for the application,
  7.     // in groups so that the related variables are easy to find.
  8.     int contestantNumber;
  9.     int startHour, startMinute, startSecond, startTotalSeconds;
  10.     int endHour, endMinute, endSecond, endTotalSeconds;
  11.     int contestantResultSeconds, bestResultSeconds, bestResultContestantNumber;
  12.  
  13.     // Since we're going to increase this every loop, this has to be intitialized.
  14.     // Working with an uninitialized variable will result in a program error.
  15.     int totalContestants = 0;
  16.  
  17.     // When the first contestant result is entered, it will have
  18.     // no best result to compare with, which will cause a program error.
  19.     // Therefore, we set it to something really bad, which means that
  20.     // practically any result will beat it.
  21.     bestResultSeconds = 60*60*24*365*1000; // A thousand years. Not a very good.
  22.     bestResultContestantNumber = -1;
  23.  
  24.     // Keep asking for new contestants forever,
  25.     // until something in the loop breaks the loop
  26.     // (which we have decided a contestantNumber of < 0 does)
  27.     while(true) {
  28.         // Let the user know that we are collecting
  29.         // time data about a new contestant
  30.         cout << "New Contestant!" << endl;
  31.  
  32.         // Collect a number for the contestant
  33.         cout << "[Start number] (-1 to quit): ";
  34.         cin >> contestantNumber;
  35.  
  36.         // If you enter a number less than
  37.         // zero, we're done collecting results
  38.         if(contestantNumber < 0) {
  39.             break; // break means to end the while loop right here
  40.         }
  41.  
  42.         // The user did not decide to end the application,
  43.         // which means that we are adding a new contestant.
  44.         // We begin by increasing the number of total contestants we have,
  45.         // since we want to keep track of that for the end of the application.
  46.         totalContestants++; // Increase by one
  47.  
  48.         // Collect start time
  49.         cout << "[Start Time]" << endl;
  50.         cout << "Hour: ";
  51.         cin >> startHour;
  52.         cout << "Minute: ";
  53.         cin >> startMinute;
  54.         cout << "Second: ";
  55.         cin >> startSecond;
  56.  
  57.         // Collect end time
  58.         cout << "[End Time]" << endl;
  59.         cout << "Hour: ";
  60.         cin >> endHour;
  61.         cout << "Minute: ";
  62.         cin >> endMinute;
  63.         cout << "Second: ";
  64.         cin >> endSecond;
  65.  
  66.         // Awesome design feature: Spacing after each contestant entry
  67.         cout << endl;
  68.  
  69.         // We need to transform hours, minutes and seconds to a
  70.         // single number which we can use to compare and count with.
  71.        
  72.         // Lets convert them all to seconds
  73.         startTotalSeconds = startHour*60*60 + startMinute*60 + startSecond;
  74.         endTotalSeconds = endHour*60*60 + endMinute*60 + endSecond;
  75.  
  76.         // Now we can use these single numbers to
  77.         // calculate the time between start and end
  78.         contestantResultSeconds = endTotalSeconds-startTotalSeconds;
  79.  
  80.         // Now that we have the contestants result, we can compare it
  81.         // to our best result so far and see if the new contestant beats it
  82.         // NOTE: a better result is a faster time, which is a lower amount of seconds
  83.         if(contestantResultSeconds < bestResultSeconds) {
  84.             // Yes, we have a new best result!
  85.             // Lets overwrite the old one. Haha, sucker!
  86.             bestResultSeconds = contestantResultSeconds;
  87.             bestResultContestantNumber = contestantNumber;
  88.         }
  89.     }
  90.  
  91.     // Even more awesome design, more spacing!
  92.     cout << endl;
  93.  
  94.     // When the application reaches this point it means the
  95.     // while loop is done, so the user is satisfied with the
  96.     // number of contestants we have and want to know who won.
  97.  
  98.     // If the user decides not to enter any contestants, we don't have a best
  99.     // result (bad user!). But sadly, we programmers need to handle every scenario!
  100.     if(bestResultContestantNumber == -1) {
  101.         // Let the user know that we are very dissapointed
  102.         cout << "Bad user, you didn't enter any contestants!" << endl;
  103.     }
  104.    
  105.     // Or, we have a nice user and we have our winner.
  106.     // Let the user know who the winner is!
  107.     else {
  108.  
  109.         // Convert the best result from total seconds to hours, minutes and seconds.
  110.         int hours = (bestResultSeconds/60/60) % 24;
  111.         int minutes = (bestResultSeconds/60) % 60;
  112.         int seconds = bestResultSeconds % 60;
  113.  
  114.         cout << "The winner is contestant #" << bestResultContestantNumber;
  115.         cout << " with the time " << hours << ":" << minutes << ":" << seconds << endl;
  116.         cout << "We thank all the " << totalContestants << " contestants, ";
  117.         cout << "but there can be only one winner." << endl;
  118.         cout << "See you next time!" << endl;
  119.     }
  120.  
  121.     // This is the end of the program, so we tell the system to pause
  122.     // so that she has time to read all the magnificent text we have output!
  123.     system("pause");
  124.     return 0;
  125. }
Add Comment
Please, Sign In to add comment