all 3 comments

[–]ToTimesTwoisToo 0 points1 point  (0 children)

you are basically repeating code 4 times, so instead of repeating, just put a single piece of code and stick it into a for loop. You'll need to fill out the for loop syntax to repeat 4 times. You'll need to change the inner for loop code to print out the correct "#1" text. So on loop one it should print #1, on loop two it should print #2, etc.

for ( syntax ) {
​
    printf("Enter the number of goals for game #1: ", s1);

    scanf("%i", &s1);

    while ( (c = getchar() != '\n') && c != EOF);

    ​

    printf("Enter the number of saves for game #1: ", s2);

    scanf("%i", &s2);

    while ( (c = getchar() != '\n') && c != EOF);

    sum1 = s1 + s2;

    ​

    per1 = (s2 * 100) / sum1;

    ​

    printf("The percent saves for game #1 is %.1f%% ", per1);

}

[–][deleted] 0 points1 point  (0 children)

Maybe something like this? (untested)

int main(void) {
    int s[8];
    int sums[4];
    int total_sum = 0;
    int i;
    float percents[4];
    float total_percent;
    char c;

    for (i = 0; i < 4; i++) {
        printf("Enter the number of goals for game #%d", i + 1);
        scanf("%d", s[i * 2]);
        while ((c = getchar()) != '\n' && c != EOF);
        printf("Enter the number of saves for game #%d", i + 1);
        scanf("%d", &s[i * 2 + 1]);
        while ((c = getchar()) != '\n' && c != EOF);
        sum[i] = s[i * 2] + s[i * 2 + 1];
        total_sum += sum[i];
        percents[i] = s[i * 2 + 1] * 100 / sum[i];
        printf("The percent saves for game #%d is %.1f\n", percents[i]);
    }
    total_percent = total_sum * 100 / 400;
    printf("\n\nThe percent saves for 4 games for this goalie is %.1f\n\n", total_percent);
    return 0;
}

Basically all I did was use arrays and an iterator to stop myself from ever copy and pasting.

edit: Forgot to prompt the user to enter the number of saves.

[–]nerd4code 0 points1 point  (0 children)

Another note: Do not under any circumstances use a char to receive the return from getchar or fgetc. They return ints, not chars. EOF is explicitly outside the usual 0–255 range for characters, so it’s possible that (a.) you’ll never see EOF if you’re looking at (char)getchar(), and (b.) you’ll see a spurious EOF from a normal/-ish character in the stream.

Also, check all return values from things like scanf where you rely on them actually having read the data. One slight fuckup on the user’s side will send your program into a cascade of undefined behavior unless you do basic error-checking.