如何一起添加四个单独的时间?

时间:2016-10-24 00:00:33

标签: c++

我需要将四次(leg1到leg4)加在一起作为输出。时间由用户输入并在几秒钟内输出,但输出需要显示为分钟和秒,这是我之前的但是在将代码添加到代码后它不再有效。

我是全新的,完全迷茫。

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

所以我想在这里的最后一行计算为腿输入的数字并显示它? Image

1 个答案:

答案 0 :(得分:0)

你可以用C ++和几乎所有其他语言添加整数:

time_taken = leg1 + leg2 + leg3 + leg4;

该行将在您上一次打印声明之上。