Good day to everyone. I have a question. How to fix my incorrect result as shown here, on my Point Obtained part, Round 1, the result is not correct? The answer is should be 34 for player 1, 45 for player 2, 50 for player 3.
Also, I also know that fflush(stdin) is not suitable to be implemented in coding. But when I remove fflush(stdin), the fgets is skipping my input. Can anyone suggest me how to fix this?
This is my code:
#include<stdio.h>
int main(void)
{
int round;
int player;
char name1[30];
char name2[30];
char name3[30];
int total1;
int total2;
int total3;
int times;
int point[3];
total1=0;
total2=0;
total3=0;
printf("======League of Legends======\n");
printf("How many round?:");
scanf("%d", &round);
printf("How many player:");
scanf("%d", &player);
fflush(stdin);
printf("Name player 1:");
fgets(name1, 30, stdin);
printf("Name player 2:");
fgets(name2, 30, stdin);
printf("Name player 3:");
fgets(name3, 30, stdin);
for(times=1; times<=round; times++)
{
printf("## Round %d ##\n", times);
printf("Point for player 1(%s):",name1);
scanf("%d", &point[0]);
printf("Point for player 2(%s):", name2);
scanf("%d", &point[1]);
printf("Point for player 3(%s):", name3);
scanf("%d", &point[2]);
total1=total1+point[0];
total2=total2+point[1];
total3=total3+point[2];
}
for(times=1; times<=round; times++)
{
printf("******Point obtained******\n");
printf("## Round %d ##\n", times);
printf("Point for player 1(%s): %d\n", name1, point[0]);
printf("Point for player 2(%s): %d\n", name2, point[1]);
printf("Point for player 3(%s): %d\n", name3, point[2]);
}
printf("++++++TOTAL POINT++++++\n");
printf("Total point player 1(%s):%d\n", name1, total1);
printf("Total point player 2(%s):%d\n", name2, total2);
printf("Total point player 3(%s):%d\n", name3, total3);
return 0;
}