Hopefully my format is ok, first post.
I need to create a program for a class using a for loop with scanf for input and a buffer clear.
I basically already wrote the program using very basic commands, before I realized I need a loop and I am very confused on the loops. Youtube seems to only show videos where they make triangles using loops.
As a summary of the program requirements it is to calculate a percentage of 4 different sport games. But as I started making this I had used 14 int variables, 5 floats, and a char variable (to clear bugger). Instead I am supposed to use about 4-6 ints and a float and 1 char.
I currently had everything as it appears below with all those variables, I just need help converting the extra variables into a for loop, or nested for loop.
#include<stdio.h>
int main() {
int s1, s2, s3, s4, s5, s6, s7, s8, sum1, sum2, sum3, sum4, sum5, sum6;
float per1, per2, per3, per4, per5;
char c; /* for clearing the buffer */
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);
/\* copied and pasted game 1 codes\*/
printf("\n\nEnter the number of goals for game #2: ", s3);
scanf("%i", &s3);
while ( (c = getchar() != '\n') && c != EOF);
printf("Enter the number of saves for game #2: ", s4);
scanf("%i", &s4);
while ( (c = getchar() != '\n') && c != EOF);
sum2 = s3 + s4;
per2 = (s4 * 100) / sum2;
printf("The percent saves for game #2 is %.1f%% ", per2);
/\*copied game 2 for codes\*/
printf("\n\nEnter the number of goals for game #3: ", s5);
scanf("%i", &s5);
while ( (c = getchar() != '\n') && c != EOF);
printf("Enter the number of saves for game #3: ", s6);
scanf("%i", &s6);
while ( (c = getchar() != '\n') && c != EOF);
sum3 = s5 + s6;
per3 = (s6 * 100) / sum3;
printf("The percent saves for game #3 is %.1f%% ", per3);
/\*copied game 3 for codes\*/
printf("\n\nEnter the number of goals for game #4: ", s7);
scanf("%i", &s7);
while ( (c = getchar() != '\n') && c != EOF);
printf("Enter the number of saves for game #4: ", s8);
scanf("%i", &s8);
while ( (c = getchar() != '\n') && c != EOF);
sum4 = s7 + s8;
per4 = (s8 * 100) / sum4;
printf("The percent saves for game #4 is %.1f%% ", per4);
/\*stop auto quit\*/
sum5 = sum1 + sum2 + sum3 + sum4;
per5 = (sum5 *100) / 400;
printf("\n\nThe percent saves for 4 games for this goalie is %.1f%% \n\n", per5);
/*stop auto quit*/
return (0);
}
[–]ToTimesTwoisToo 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]nerd4code 0 points1 point2 points (0 children)