Problems outputting hours, minuites and seconds into an array

Hi,
I have been working on an assignment where the task is to collect user input from 3 different events, convert them all to seconds and then display them all in an array (and multiply them back up to display in he correct format) , along with the overall total.

//long variables to display overall times in the array
long Swim = 0;
long Bike = 0;
long Run = 0;
long total = 0;

void setup() {
   
Serial.begin(9600);//open port at this speed


//swimming
Serial.println("Swimming Details- ");
 
Serial.println("how many hours?");
int swim_hours;
while (!Serial.available()) {
  //wait for a user to enter a value
  ;
}
swim_hours = Serial.parseInt();  //getting user hours

Serial.println("how many minuites?");
int swim_mins;
while (!Serial.available()) {
;  
}
swim_mins = Serial.parseInt();  //getting user mins

Serial.println("how many seconds?");
int swim_secs;
while (!Serial.available()) {
;
}
swim_secs = Serial.parseInt();  //getting user seconds

 
//biking
Serial.println();
Serial.println("Biking Details- ");
Serial.println("how many hours?");
int bike_hours;
while (!Serial.available()) {
    //wait for a user to enter a value
  ;
}
bike_hours = Serial.parseInt();

Serial.println("how many minuites?");
int bike_mins;
while (!Serial.available()) {
;  
}
bike_mins = Serial.parseInt();

Serial.println("how many seconds?");
int bike_secs;
while (!Serial.available()) {
;
}
bike_secs = Serial.parseInt();

 
//running
Serial.println();
Serial.println("Running Details- ");
 
Serial.println("how many hours?");
int running_hours;
while (!Serial.available()) {
  //wait for a user to enter a value
  ;
}
running_hours = Serial.parseInt();

Serial.println("how many minuites?");
int running_mins;
while (!Serial.available()) {
;  
}
running_mins = Serial.parseInt();

Serial.println("how many seconds?");
int running_secs;
while (!Serial.available()) {
;
}
running_secs = Serial.parseInt();

Serial.println();


//converting the user input (time) into seconds
Swim = swim_hours * 3600 + swim_mins * 60 + swim_secs;
Bike = bike_hours * 3600 + bike_mins * 60 + bike_secs;
Run = running_hours * 3600 + running_mins * 60 + running_secs;
total = Run + Swim + Bike;

String NamesArray[] = {"Swim Time: ", "Bike Time: ", "Run Time: ", "Total Time: "}; // creating string array for names

int SecondsArray[4]; //creating int array for values
SecondsArray[1] = Swim;
SecondsArray[2] = Bike;
SecondsArray[3] = Run;
SecondsArray[4] = total;

//printing array of the 3 events and dividing to conver back to correct format from seconds

int i = 0;//creating variable i
for(i=0;i<4;i++){//if i is less than 4 increment i by 1
  Serial.println(NamesArray[i] + (SecondsArray[i] / 3600) + "hr " + ((SecondsArray[i] / 60) % 60) + "min " + (SecondsArray[i] % 60) + "sec.");//matching names array with data to display in array
}

}

I have included an attachment of the output in the serial monitor, it seems to be a problem with swim and for some reason the hours have not added up correctly in the final total either.

Thanks in advance for any help offered!

SecondsArray[4] = total;

You don't have a SecondsArray[4]
Don't write to memory you don't own

AWOL:

SecondsArray[4] = total;

You don't have a SecondsArray[4]
Don't write to memory you don't own

But I do have a SecondsArray[4], I declared the size of the array to be 4, and the total variable is meant to be the sum of all 3 events added up.

It might just be me though, could you explain better, I'm a beginner and not amazing at this stuff sorry.

Array indices in C/C++ are zero based. Four elements are expressed in a range {0, 1, 2, 3}. The index 4 is a non existent fifth element.

Arrays are zero indexed, so the array is SecondsArray[0], SecondsArray[1], SecondsArray[2], and SecondsArray[3]

As AWOL says, there is no SecondsArray[4].

Edit--Oops, beaten to the punch.

The index 4 is a non existent fifth element.

Mila Jovovich was the fifth element

cattledog:
Arrays are zero indexed, so the array is SecondsArray[0], SecondsArray[1], SecondsArray[2], and SecondsArray[3]

As AWOL says, there is no SecondsArray[4].

Edit--Oops, beaten to the punch.

ahhhhh, thanks guys I will change it and reply with the results! :slight_smile:

Brilliant! It now works as intended, you guys are life-savers! :smiley: