Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main() {
- // Declare all the variables we need for the application,
- // in groups so that the related variables are easy to find.
- int contestantNumber;
- int startHour, startMinute, startSecond, startTotalSeconds;
- int endHour, endMinute, endSecond, endTotalSeconds;
- int contestantResultSeconds, bestResultSeconds, bestResultContestantNumber;
- // Since we're going to increase this every loop, this has to be intitialized.
- // Working with an uninitialized variable will result in a program error.
- int totalContestants = 0;
- // When the first contestant result is entered, it will have
- // no best result to compare with, which will cause a program error.
- // Therefore, we set it to something really bad, which means that
- // practically any result will beat it.
- bestResultSeconds = 60*60*24*365*1000; // A thousand years. Not a very good.
- bestResultContestantNumber = -1;
- // Keep asking for new contestants forever,
- // until something in the loop breaks the loop
- // (which we have decided a contestantNumber of < 0 does)
- while(true) {
- // Let the user know that we are collecting
- // time data about a new contestant
- cout << "New Contestant!" << endl;
- // Collect a number for the contestant
- cout << "[Start number] (-1 to quit): ";
- cin >> contestantNumber;
- // If you enter a number less than
- // zero, we're done collecting results
- if(contestantNumber < 0) {
- break; // break means to end the while loop right here
- }
- // The user did not decide to end the application,
- // which means that we are adding a new contestant.
- // We begin by increasing the number of total contestants we have,
- // since we want to keep track of that for the end of the application.
- totalContestants++; // Increase by one
- // Collect start time
- cout << "[Start Time]" << endl;
- cout << "Hour: ";
- cin >> startHour;
- cout << "Minute: ";
- cin >> startMinute;
- cout << "Second: ";
- cin >> startSecond;
- // Collect end time
- cout << "[End Time]" << endl;
- cout << "Hour: ";
- cin >> endHour;
- cout << "Minute: ";
- cin >> endMinute;
- cout << "Second: ";
- cin >> endSecond;
- // Awesome design feature: Spacing after each contestant entry
- cout << endl;
- // We need to transform hours, minutes and seconds to a
- // single number which we can use to compare and count with.
- // Lets convert them all to seconds
- startTotalSeconds = startHour*60*60 + startMinute*60 + startSecond;
- endTotalSeconds = endHour*60*60 + endMinute*60 + endSecond;
- // Now we can use these single numbers to
- // calculate the time between start and end
- contestantResultSeconds = endTotalSeconds-startTotalSeconds;
- // Now that we have the contestants result, we can compare it
- // to our best result so far and see if the new contestant beats it
- // NOTE: a better result is a faster time, which is a lower amount of seconds
- if(contestantResultSeconds < bestResultSeconds) {
- // Yes, we have a new best result!
- // Lets overwrite the old one. Haha, sucker!
- bestResultSeconds = contestantResultSeconds;
- bestResultContestantNumber = contestantNumber;
- }
- }
- // Even more awesome design, more spacing!
- cout << endl;
- // When the application reaches this point it means the
- // while loop is done, so the user is satisfied with the
- // number of contestants we have and want to know who won.
- // If the user decides not to enter any contestants, we don't have a best
- // result (bad user!). But sadly, we programmers need to handle every scenario!
- if(bestResultContestantNumber == -1) {
- // Let the user know that we are very dissapointed
- cout << "Bad user, you didn't enter any contestants!" << endl;
- }
- // Or, we have a nice user and we have our winner.
- // Let the user know who the winner is!
- else {
- // Convert the best result from total seconds to hours, minutes and seconds.
- int hours = (bestResultSeconds/60/60) % 24;
- int minutes = (bestResultSeconds/60) % 60;
- int seconds = bestResultSeconds % 60;
- cout << "The winner is contestant #" << bestResultContestantNumber;
- cout << " with the time " << hours << ":" << minutes << ":" << seconds << endl;
- cout << "We thank all the " << totalContestants << " contestants, ";
- cout << "but there can be only one winner." << endl;
- cout << "See you next time!" << endl;
- }
- // This is the end of the program, so we tell the system to pause
- // so that she has time to read all the magnificent text we have output!
- system("pause");
- return 0;
- }
Add Comment
Please, Sign In to add comment