Skip to content

Instantly share code, notes, and snippets.

@ebba0194
Created November 27, 2017 02:00
Show Gist options
  • Save ebba0194/89f9e1b908bb7c0e5e385af69bd0fa45 to your computer and use it in GitHub Desktop.
Save ebba0194/89f9e1b908bb7c0e5e385af69bd0fa45 to your computer and use it in GitHub Desktop.
homework structs
#include <iostream>
using namespace std;
struct Time{
int hour, min, sec;
} times[3];
int add (Time times[3]);
int main(){
//get input + safeties
for (int i=0; i<2; i++)
{
cout<<"Enter the "<<i<<" time (hh:mm:ss): ";
cin>>times[i].hour>>times[i].min>>times[i].sec;
while(times[i].hour>=24){
cout<<"Hours must be less than or equal to 24.";
cin>>times[i].hour;
}
while(times[i].min>60){
cout<<"Minutes must be less than or equal to 60.";
cin>>times[i].min;
}
while(times[i].sec>60){
cout<<"Seconds must be less than or equal to 60.";
cin>>times[i].sec;
}
}
add (Time times[3]);
cout<<"Sum of times: "<<times[2].hour<<":"<<times[2].min<<":"<<times[2].sec;
return 0;
}
void add (Time times[3]){
//add times together
for (int i=0; i<2; i++){
times[2].hour+=times[i].hour;
times[2].min+=times[i].min;
times[2].sec+=times[i].sec;
}
while (times[2].hour>=24){
times[2].hour-=24;
}
while (times[2].min>60){
times[2].hour+=1;
times[2].min-=60;
}
while (times[2].sec>60){
times[2].min+=1;
times[2].sec-=60;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment