0

I have written a c program to create a structure containing the name of cricketer (char array), his age (integer), and number of test matches (integer) that he has played and the average runs (float) that he has scored in each test match. while writing the input for the same when in for loop(for the input) value gets greater or equal to 2 it does take the input for the name(i.e %s).

//Write a C program to create a record containing the name of cricketer (char array), his age
//(integer), and number of test matches (integer) that he has played and the average runs
//(float) that he has scored in each test match. Create an array of structures to hold records
//of 20 such cricketers and then write a program to read these records and print it, also find
//the cricketer which has the highest average.
#include<stdio.h>
struct cricketer
{
    char name[40];
    int age;
    int match;
    float run;
};
int main()
{
    struct cricketer player[20];
    for(int i=0;i<3;i++)
    {
        printf("Enter the information of %d cricketer\n",i+1);
        printf("Enter name \n");
        scanf("%[^\n]s",&player[i].name);
        printf("Enter age\n");
        scanf("%d",&player[i].age);
        printf("Enter number of matches played\n");
        scanf("%d",&player[i].match);
        printf("Enter average\n");
        scanf("%d",&player[i].run); 
    }
    for(int i=0;i<3;i++)
    {
        printf("Name:%s\nage:%d\nNumber of matches:%d\nAverage:%f\n",player[i].name,player[i].age,player[i].match,player[i].run);
    }
    float high=player[0].run;
    for(int i=0;i<3;i++)
    {
        if(high<player[i].run)
        {
            player[i].run=high;
        }
    }
    printf("The higgest Average is %f",high);
    return 0;
}

help me in knowing the the reason behind the problem of not taking an input explanation and correction for the same.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335

1 Answers1

0

You are using invalid arguments in some calls of scanf.

Instead of this call

    scanf("%[^\n]s",&player[i].name);

where there is incorrect format string and the second argument expression you need to write

    scanf(" %39[^\n]", player[i].name);

Pay attention to the leading space in the format string. It allows to skip white space characters including the new line character '\n' that can be placed in the input buffer due to pressing the Enter key.

And in this call of scanf where the second argument expression has the type float

    scanf("%d",&player[i].run);

you are using incorrect conversion specifier d designed for objects of type int. You need to use conversion specifier f

    scanf("%f",&player[i].run); 

Also it is better to start this for loop

float high=player[0].run;
for(int i=0;i<3;i++)
//...

with i equal to 1

float high=player[0].run;
for(int i=1;i<3;i++)
//...

And also it is better to append the format string in this call of printf

printf("The higgest Average is %f",high);

with new line character '\n'

printf("The higgest Average is %f\n",high);

In general you should check whether calls of scanf are successful.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335