-5

The 15th line is what I cant fix. Please be kind enough to look at my code and diagnose the issue. I am new to programming and will appreciate anyone pointing me in the right direction.

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

#define TEAMS 200
#define RUNNERS 10000
#define LENGTH 20
#define TEAMSIZE 50

FILE *fptr;
fptr = fopen("myfile.txt","w");
void getdetails();

struct person {
    char name[LENGTH];
    int number;
    int age;
    int event;
    float money;
    float time;
}p;

struct team {
    char tname[LENGTH];
    int nummembers;
    float money;
    struct person members[TEAMSIZE];
}t;

int main() {

    int c,flag=0,i=0,j,k=0;

    printf("\n---------------------------------------------------");
    printf("\n---------------------------------------------------");
    printf("\nHeader Specification");

    while(flag==0) {
        printf("\n1.Individual Registration");
        printf("\n2.Team Registration");
        printf("\n3.Running Events");
        printf("\n4.Donation Totals");
        printf("\n5.Exit");

        printf("\nEnter your choice:");
        scanf("%d",&c);

        switch(c) {
        case 1:
            printf("\n For Individual Registration");
            printf("\n1.Early Registration");
            printf("\n2.regular Registration");
            int ch;

            printf("\nEnter your choice:");
            scanf("%d",&ch);

            switch(ch) {
            case 1: 
                printf("\n For Early Registration");
                i=i+1;
                getdetails(i);
                break;
            case 2: 
                printf("\n For Early Registration");
                i=i+1;
                getdetails(i);
                break;

            default: 
                printf("\n not valid");
                break;
            }
            break;
        case 2:
            printf("\n For Team Registration");

            printf("\n Enter team name:");
            scanf("%s",t.tname);

            printf("\n Enter team participant number:");
            scanf("%d",&t.nummembers);

            k=k+1;

            for(j=1;j<=t.nummembers;j++) {
                getdetails(k);
            }
            break;
        case 5:
            flag=1;
            break;
        }
    }
     return 0;
}

void getdetails(int i) {

    printf("Enter your name:");
    scanf("%s",p.name);

    printf("Enter your age:");
    scanf("%d",&p.age);

    printf("Enter the event:");
    scanf("%d",&p.event);

    printf("Enter the donation amount:");
    scanf("%f",&p.money);

    if(fptr == NULL) {
       printf("Error!");
       exit(1);
    }

    fprintf(fptr,"\n%s register for\t%dk race\tand the number is%d.",p.name,p.event,i);
    fclose(fptr);
}
Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
  • 3
    You should do `fptr = fopen("myfile.txt","w");` in some function. – MayurK Dec 02 '16 at 16:21
  • 4
    Why on earth are there random ridiculous spaces in there? – MD XF Dec 02 '16 at 16:22
  • 1
    You should look into [proper C formatting](//prohackr112.tk/r/proper-c-formatting). Although I think you'd rather learn to [thoroughly obfuscate your code](//prohackr112.tk/r/proper-c-obfuscation). – MD XF Dec 02 '16 at 16:28
  • 4
    Fix your formatting before posting on stack overflow. – Sethmr Dec 02 '16 at 16:28
  • Would this even compile, after being fixed? I was under the impression it was impossible to declare variables inside a switch case. – MD XF Dec 02 '16 at 16:30
  • I never tried to compile it, but I reformatted it to make it ledgable lol – Sethmr Dec 02 '16 at 18:38

1 Answers1

2

A runtime-executable statement, like

 fptr = fopen("myfile.txt","w");

cannot reside in global scope. It has to reside in block scope, i.e., in some function body.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 2
    Beat me to it. I was just about to mention that. Too bad I reached my vote limit for today... *sigh* – MD XF Dec 02 '16 at 16:23