Hi, I'm trying to make a program that lets a user punch in the results from a race. What I got is a loop for entering the contestants startnumber, the time they started the race (hh, mm, ss) and when they finished the race (hh, mm, ss). I'm stuck with a two parted problem (yes I know it's probably simple but I'm new to this. Because I'm new I wanna keep it really simple, just using the basic code, no arrays or functions). Part 1: how do I keep both the fastest time and startnumber in memory? Part 2: how do I convert the times (that I have in seconds) in to a readable hh : mm : ss , preferbly using modulus?
Thankfull for any help.

Recommended Answers

All 9 Replies

I'm not quite sure what you mean by

how do I keep both the fastest time and startnumber in memory?

Perhaps you just mean store the data for the duration of the programs execution?
The rest is just basic Math.
If you know the start and end time then you can calculate how long it took for a person to finish the race.
NB: Depending on how much data is input you may end up with a lot of variables if you avoid the use of basic data types such as arrays.
Anyway you could store the times in float variables ready to be converted to a time format.

The time calculations depend upon how you decide to measure the time taken to complete the race (e.g. seconds or minutes or hours).
NB: Again you could end up with a lot of variables that aren't required so I would suggest a struct or similar.
e.g.

typedef struct
{
    int seconds;
    int minutes;
    int hours;
} tStruct;

This is how far I've gotten. The program is in swedish so perheps I'll explain: Tim=hour, Sek= seconds, slut_Tim= end_Hour, Ange startnummer= type in startnumber, minsta_Tid = shortest_Time.
What happens here is that what ever time you put in it's always the last input that the program treats as the shortest. I guess that means that I miss to store the data. How do I store the shortest time and link it to the right startnumber?

echo "#include <iostream>
using namespace std;
int main ()
{
int start_Nr, start_Tim, start_Min, start_Sek, slut_Tim, slut_Min, slut_Sek;
cout << "Ange startnummer: ", cin >> start_Nr;
int slut_Tid, minsta_Tid = 999999, timme, minut, sekund;

    while (start_Nr >0)
    {
    cout << "Ange starttid: ", cin >> start_Tim >> start_Min >> start_Sek;
    cout << "Ange sluttid: ", cin >> slut_Tim >> slut_Min >> slut_Sek;
    cout << "Ange startnummer: ", cin >> start_Nr;
    slut_Tid = (((slut_Tim * 3600) + (slut_Min * 60) + slut_Sek) - ((start_Tim * 3600) + (start_Min * 60) + start_Sek));
    if (slut_Tid < minsta_Tid);
     minsta_Tid = slut_Tid;


    }
cout << "Den minsta tiden var: " << slut_Tid;

You've almost got it by the looks of it.
I've tried to translate it into English so that other people will find it a bit easier to follow the code, I hope you dont mind.

#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;
  
  cout << "Enter start Number: ";
  cin >> start_Nr;
  
  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;
  }
  firstPerson = false;
}
cout << "The fastest time was: " << shortest_Time << endl;
cout << "The fastest runner was: " << fastest_Runner << endl;
}

You just needed to store the runner's ID the same way you were storing the shortest time.

Colezy

EDIT:
@caged_fire, the OP said to keep it as simple as possible as he/she is new. Although you are correct about the struct, I think it will only serve to confuse him/her at this point.

Looking back I don't think your use of cin is correct, at least I've never seen it used with multiple >>
I don't have time to test it though, I'll leave that up to you.

commented: Very concise information provided +2

Perfect!
Thanks. But the only thing I don't get is the Bool firstPerson. What does that do? I get that Bool is to verify if something is true or false but the firtsPerson, what is the use of that variable?

Its instead of initially setting the fastest time to 999999. If the current runner is the first then just store the time as the fastest.
The way you did it is probably fine for the small scale application but what if all the runners did take longer than 999999 seconds? then you would have a bug. (Admittedly 999999 seconds is over 10 days lol).

The firstPerson variable is used to bypass the end_Time < shortestTime check with the use of || (or).

Regards,
Colezy

Ok!
Got it, thanks a lot!!

But hey, wait a minute... This isn't correct... This puts out the fastest time correctly but not the fastest runners startnumber. I end the input of runners and times by giving a negative startnumber (if (start_Nr>0)) and that is what the program tells me is the fastest runner.

#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;
}

Of course, sorry, try that

Colezy

But what if the race is started 11:55:00 PM and ends at 00:15:00 am (for instance).
How do I handle the times then?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.