0

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

enter image description here

the busybee
  • 10,755
  • 3
  • 13
  • 30
Han
  • 3
  • 3
  • Welcome to StackOverflow! Please take the [tour] and read "[ask]". - You might like to [edit] your question, for example: instead of posting images of text better post the text as such. – the busybee May 09 '20 at 10:35
  • `fflush(stdin)` is undefined behavior https://stackoverflow.com/questions/2979209/using-fflushstdin – Abhay Aravinda May 09 '20 at 10:39

1 Answers1

0

The reason of getting wrong output is that the array point is overwritten by information of later round. You have to store the data separately.

To fix fgets() problem, I will read all lines via fgets() to have them read newline characters.

possible fix:

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

int readInt(void) {
    char buffer[512];
    int ret;
    fgets(buffer, sizeof(buffer), stdin);
    sscanf(buffer, "%d", &ret);
    return ret;
}

void readString(char* str, size_t str_size) {
    char* p;
    fgets(str, str_size, stdin);
    for (p=str; *p!='\0' && *p!='\n'; p++);
    *p='\0';
}

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?:");
    round=readInt();

    printf("How many player:");
    player=readInt();

    printf("Name player 1:");
    readString(name1, sizeof(name1));

    printf("Name player 2:");
    readString(name2, sizeof(name2));

    printf("Name player 3:");
    readString(name3, sizeof(name3));

    point=malloc(sizeof(*point)*round);

    for(times=1; times<=round; times++)
    {
        printf("## Round %d ##\n", times);
        printf("Point for player 1(%s):",name1);
        scanf("%d", &point[times-1][0]);

        printf("Point for player 2(%s):", name2);
        scanf("%d", &point[times-1][1]);

        printf("Point for player 3(%s):", name3);
        scanf("%d", &point[times-1][2]);

        total1=total1+point[times-1][0];
        total2=total2+point[times-1][1];
        total3=total3+point[times-1][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[times-1][0]);

       printf("Point for player 2(%s): %d\n", name2,  point[times-1][1]);

       printf("Point for player 3(%s): %d\n", name3,  point[times-1][2]);

    }

    free(point);

    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;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Thanks for your suggestion. I'm very appreciate that! But I have a question, actually how to read the strings with perfectly? I mean reading the strings without skipping inputs I have searched a lot about this, some people say put fgets(), sscanf() and so on, but I have tried these in my previous coding, but sometimes they will skip my inputs. So can you suggest me what is the best way to read the strings? – Han May 10 '20 at 10:49