how to avoid the blank line printed before the output in this program?

Viewed 52

my code is below
how to avoid the blank line printed before the required output? the question is to print the batsman with maximum runs.

#include<stdio.h>
#include<stdlib.h>
    
    int main()
    {
        int n,runs,maxRuns=0;
        char bat[100],maxBat[100];
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%[^,],%d",&bat,&runs);
            if(runs>maxRuns){
                maxRuns=runs;
                strcpy(maxBat,bat);
            }
        }
        printf("%s",maxBat);
    
    }

the output i'm getting is

enter image description here

1 Answers

Try the following

scanf( " %[^,],%d", bat, &runs );

See the blank before the first conversion specifier,

Also instead of &bat use bat because the type of the expression &bat (char( * )[100]) is not char * as it is required by the conversion specifier %[.

Related