lundi 5 novembre 2018

Assigning variable in IF statement C

The code below is to output the time taken for a snail to complete a race. I understand an if statement is to be used however I am struggling to find a way to assign my final code- TimeMinutes1 + TimeMinutes2 + TimeMinutes3 + TimeMinutes4, TimeSeconds1 + TimeSeconds2 + TimeSeconds3 + TimeSeconds4 a variable which can be used in conjunction with an IF statement?

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char SquirrelName [20]; 
    int TimeMinutes1;
    int TimeMinutes2;
    int TimeMinutes3;
    int TimeMinutes4;
    int TimeSeconds1;
    int TimeSeconds2;
    int TimeSeconds3;
    int TimeSeconds4;
    int TotalSeconds1;
    int TotalSeconds2;
    int TotalSeconds3;
    int TotalSeconds4;

    printf("What is the name of the squirrel? \n");
    scanf("%s", SquirrelName);

    printf("How long did it take to complete the first lap in Seconds? \n");
    scanf("%d", &TotalSeconds1);

    TimeMinutes1 = TotalSeconds1 / 60;
    TimeSeconds1 = TotalSeconds1 % 60;

    printf("Lap 1 finished in %d minutes and %d seconds\n", TimeMinutes1, TimeSeconds1);

    printf("How long did it take to complete the second lap in Seconds? \n");
    scanf("%d", &TotalSeconds2);

    TimeMinutes2 = TotalSeconds2 / 60;
    TimeSeconds2 = TotalSeconds2 % 60;

    printf("Lap 2 finished in %d minutes and %d seconds\n", TimeMinutes2, TimeSeconds2);

    printf("How long did it take to complete the third lap in Seconds? \n");
    scanf("%d", &TotalSeconds3);

    TimeMinutes3 = TotalSeconds3/ 60;
    TimeSeconds3 = TotalSeconds3 % 60;

    printf("Lap 3 finished in %d minutes and %d seconds\n", TimeMinutes3, TimeSeconds3);

    printf("How long did it take to complete the fourth lap in Seconds? \n");
    scanf("%d", &TotalSeconds4);

    TimeMinutes4 = TotalSeconds4 / 60;
    TimeSeconds4 = TotalSeconds4 % 60;

    printf("Lap 4 finished in %d minutes and %d seconds\n", TimeMinutes4, TimeSeconds4);

    printf("The total time it took for the course to complete was %d minutes and %d seconds\n", TimeMinutes1 + TimeMinutes2 + TimeMinutes3 + TimeMinutes4, TimeSeconds1 + TimeSeconds2 + TimeSeconds3 + TimeSeconds4);

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire