I have a program that lets me enter participants in a race, their startingnumber, their start time and end time. The program then tell me who won the race and the winning time.
This is my program:

#include <iostream>
using namespace std;

int main ()
{
int start_Nr, start_Hour, start_Min, start_Sec, end_Hour, end_Min, end_Sec;
int end_Time, shortest_Time, fastest_Runner;
bool firstPerson = true;

cout << "Enter start Number: ";
cin >> start_Nr;

while (start_Nr >0)
{
  cout << "Enter start time: ";
  cin >> start_Hour >> start_Min >> start_Sec;
  
  cout << "Enter end time: ";
  cin >> end_Hour >> end_Min >> end_Sec;
  
  end_Time = (((end_Hour * 3600) + (end_Min * 60) + end_Sec) - ((start_Hour * 3600) + (start_Min * 60) + start_Sec));
  if (end_Time < shortest_Time || firstPerson == true)
  {
    shortest_Time = end_Time;
    fastestRunner = start_Nr;
  }

  cout << "Enter start Number: ";
  cin >> start_Nr;
  firstPerson = false;
}
cout << "The fastest time was: " << shortest_Time << endl;
cout << "The fastest runner was: " << fastest_Runner << endl;
}

My only problem is that what if someone starts five minutes to midnight and end ten minutes AFTER midnight. How do I handle the time then? It's bound to be negative if I dont come up with something smart. I want it to be as simple as possible (I'm very new to this) so no arrays or such... Thanks

I have a program that lets me enter participants in a race, their startingnumber, their start time and end time. The program then tell me who won the race and the winning time.
This is my program:

#include <iostream>
using namespace std;

int main ()
{
int start_Nr, start_Hour, start_Min, start_Sec, end_Hour, end_Min, end_Sec;
int end_Time, shortest_Time, fastest_Runner;
bool firstPerson = true;

cout << "Enter start Number: ";
cin >> start_Nr;

while (start_Nr >0)
{
  cout << "Enter start time: ";
  cin >> start_Hour >> start_Min >> start_Sec;
  
  cout << "Enter end time: ";
  cin >> end_Hour >> end_Min >> end_Sec;
  
  end_Time = (((end_Hour * 3600) + (end_Min * 60) + end_Sec) - ((start_Hour * 3600) + (start_Min * 60) + start_Sec));
  if (end_Time < shortest_Time || firstPerson == true)
  {
    shortest_Time = end_Time;
    fastestRunner = start_Nr;
  }

  cout << "Enter start Number: ";
  cin >> start_Nr;
  firstPerson = false;
}
cout << "The fastest time was: " << shortest_Time << endl;
cout << "The fastest runner was: " << fastest_Runner << endl;
}

My only problem is that what if someone starts five minutes to midnight and end ten minutes AFTER midnight. How do I handle the time then? It's bound to be negative if I dont come up with something smart. I want it to be as simple as possible (I'm very new to this) so no arrays or such... Thanks

Hello!
A thought would be to ask from user to enter a.m. or p.m. and store it in a string. Then, you would have to face the specific problem you refer to, if start time was p.m. and end time was a.m. Moreover, if the user enters midnight time as 24:00 then i assume you would have no problem..

Edited 6 Years Ago by vanalex: n/a

Yes but the times I get is already fixed. I don't get the luxury to ask them for am or pm. And the chance of the input being "24" for midnight doesn't matter, the race time could be 23:50:20 to 01:20:12 (for instance).

I think you have almost expressed your own solution, :). If the race is ALWAYS going to be less than 12 hours, or in some way constrained to be in some range which is less than 12 hours, then you simple calculate the time, and if the time goes negative, then add 24 hours.

However, if you can have competitors that taking 4 hours and others taking 20 hours, you are going to have to have some date flag. [you might like to do that say by looking at the start number, or the order, or simple putting the day.]

Edited 6 Years Ago by StuXYZ: n/a

This question has already been answered. Start a new discussion instead.