2016-12-02 1 views
-5

Die 15. Zeile ist was ich nicht beheben kann. Bitte seien Sie so freundlich, sich meinen Code anzusehen und das Problem zu diagnostizieren. Ich bin neu im Programmieren und werde es schätzen, wenn mich jemand in die richtige Richtung weist.Ich habe Probleme mit der Neudefinition von "fptr"

#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); 
} 
+3

Sie sollten 'fptr = fopen (" myfile.txt "," w ");' in einer Funktion. – MayurK

+4

Warum in aller Welt gibt es zufällige lächerliche Räume? –

+1

Sie sollten in [richtige C-Formatierung] (// prohackr112.tk/r/proper-c-formatting) schauen. Obwohl ich denke, dass du lieber lernst, [deinen Code gründlich zu verschleiern] (// prohackr112.tk/r/proper-c-obfuscation). –

Antwort

2

Eine Runtime-ausführbare Anweisung, wie

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

kann in globalem Bereich nicht befinden. Es muss sich im Blockbereich befinden, d. H. In einem Funktionskörper.

+2

Beat mich dazu. Ich wollte das gerade erwähnen. Schade, dass ich meine Abstimmungsgrenze für heute erreicht habe ... * seufz * –

Verwandte Themen