0

I had originally made this program to take 7 inputs 11 times, but then for the sake of simplicity, set it to 2.

#include<stdio.h>
#include<string.h>

struct Cricketers{
   char Name[50];
   int ranking;
   int age;
   char Country[30];
   int Num_of_matches;
   int Bating_avg;
   int Bowling_avg;
};

int main() {

   struct Cricketers arr[2];        
   int i;
   
    for(i=0; i<2; i++)                             //For taking inputs 2 times
    {
        scanf("%s", arr[i].Name);
        scanf("%d", arr[i].ranking);
        scanf("%d", arr[i].age);
        scanf("%s", arr[i].Country);
        scanf("%d", arr[i].Num_of_matches);
        scanf("%d", arr[i].Bating_avg);
        scanf("%d", arr[i].Bowling_avg);
   }
    

    for(i=0; i<2; i++)                           //For displaying the output
    {
        printf("%s", arr[i].Name);
        printf("%d", arr[i].ranking);
        printf("%d", arr[i].age);
        printf("%s", arr[i].Country);
        printf("%d", arr[i].Num_of_matches);
        printf("%d", arr[i].Bating_avg);
        printf("%d", arr[i].Bowling_avg);
    }

return 0;

}

There are no compilation errors but after taking just 2 inputs the first time, the program just finishes execution. The program is supposed to take 7 inputs, 2 times.

After the execution finishes, this message appears:

"Process exited after 21.89 seconds with return value 3221225477 Press any key to continue . . ."

Why the value "3221225477"?

  • 1
    The value `3221225477` is equal to hexadecimal `0xC0000005`, which means your program *crashed* with an access violation. You seem to have forgotten something important about the `scanf` function and the arguments you pass to it. – Some programmer dude Sep 18 '20 at 13:09
  • Alright, then should I use `gets` ? Edit: That doesn't work, either. – Tanmay Patel Sep 18 '20 at 13:13
  • You should most definitely [*not* use `gets`](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) (which is used for strings only anyway). No please go back to your book or tutorial and look at how it uses `scanf` to read integers. Aren't you supposed to pass *pointers* to the variabled? – Some programmer dude Sep 18 '20 at 13:16
  • 1
    Nevermind. Finally got it, *&Facepalm*. – Tanmay Patel Sep 18 '20 at 13:23
  • I think there should be some automatic SO filter that detects mention of `scanf` and just answers with "pass it pointers!". It would cut out maybe 50% of questions. (This is not a flame of the OP, just an observation.) – Edd Inglis Sep 18 '20 at 15:20

0 Answers0