csfl0 csfl0 - 3 months ago 50
C++ Question

How do I add four seperate times together?

I need to add the four times (leg1 through to leg4) together as an output. The times are inputted by the user and in seconds, but on the output needs to be shown as minutes and seconds, which I had before but after adding the legs to the code it no longer works.

I'm completely new and fully confused.

#include <stdio.h>
#include <string>

int main() {
char snail_name[15] = "";
int time_taken = 0;
int leg1, leg2, leg3, leg4;

printf("\nEnter name of snail: ");
scanf("%s", &snail_name);
printf("Time to finish first leg:");
scanf("%d", &leg1);
printf("Time to finish second leg:");
scanf("%d", &leg2);
printf("Time to finish third leg:");
scanf("%d", &leg3);
printf("Time to finish fourth leg:");
scanf("%d", &leg4);
printf("%s finished the course in %d minutes and %d seconds\n", &snail_name, time_taken / 60, time_taken % 60);

return 0;

And so I would like the last line here to calculate the number entered for the legs and show it? Image


You can add integers in C++ and virtually every other language out there:

time_taken = leg1 + leg2 + leg3 + leg4;

That line would go right above your last print statement.