1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

a players record system IN DESPAIR.

Discussion in 'C' started by jose_peeterson, Aug 8, 2011.

  1. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    Dear experts, i am making a player record for my pathetic minesweeper game. please correct me i dont under stand the errors.



    Code:
    
    
    #include<stdio.h>
    #include<string.h>
    
    struct player
    {
     char name[20];
     int score;
    };
    
    int read(struct player p);
    void write(struct player p,int idx);
    
    int main()
    {
     struct player p[1000];
     int idx;
     char pname[20];
     int same = 1;
     int ctr=0;
         
     idx = read(p);        
     idx = idx + 1;
     
     while(same == 1)
      {
       printf("Enter your first name(only) MAROONED player!\n");
       scanf("%s",pname);
       
       while(ctr < idx) // index has been incremented already
        {
         if(p[ctr].name == pname);
          { 
           same = 0;
           break;
          }
         ctr++;
        }
       if(ctr == (idx - 1))
        {
         strcpy(p[idx].name,pname);
         p[idx].score = 0;
         break;
        }
      }
    
     write(p,idx);
     
    system("pause > null");
    return 0;          
    }
    
    
    
    int read(struct player p)
    {
    int i=0;
    FILE *f;
     
      f = fopen("player.txt","r");        
      
      if(f == NULL)
       {
        printf("ERROR : unable to read!\n");
        return 0;
       } 
      while(fscanf(f,"%s %d\n",p[i].name,&p[i].score) == 2);        
       {
        i++;
       }       
     
     fclose(f);
     
    return i;
    }
    
    void write(struct player p,int idx)
    {
     FILE *fw;
     int ctr;
     
     fw = fopen("player.txt","w");
     
     if(fw == NULL)
      {
       printf("ERROR opening file!\n");
       return;
      }
     
     for(ctr=0;ctr<(idx+1);ctr++)
      {
       fprintf(fw,"%s %d\n",p[ctr].name,p[ctr].score);
      }
    
    fclose(fw);
    
    printf("\nplayer records updated\n");
    
    return;
    }
    
    
    
    



    the errors i get.
    21 C:\Dev-Cpp\REVIEWS\file.c incompatible type for argument 1 of `read'
    46 C:\Dev-Cpp\REVIEWS\file.c incompatible type for argument 1 of `write'
    66 C:\Dev-Cpp\REVIEWS\file.c subscripted value is neither array nor pointer
    66 C:\Dev-Cpp\REVIEWS\file.c subscripted value is neither array nor pointer
    91 C:\Dev-Cpp\REVIEWS\file.c subscripted value is neither array nor pointer

    the player.txt file has
    Code:
    peeter 10
    xipitos 15
    shabbir 16
    
     
  2. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    ************ error************
    if(p[ctr].name == pname);

    the above line is has an error because strings need to be compared using if(strcmp(str1,str2) == 0);
    sorry about that.
     
  3. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    SOMEBODY HELP
    PLEASEEEEEEEEEEEEEEEEEEEEEEEEE.....

    thanks
     
  4. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    maybe this will help...

    Code:
    #include<stdio.h>
    #include<string.h>
    
    struct player{
        char name[20];
        int score;
    };
    
    int read(struct player *p);
    void write(struct player *p,int idx);
    
    int main()
    {
     struct player p[1000];
     int idx;
     char pname[20];
     int same = 1;
     int ctr=0;
         
     idx = read(p);        
     idx = idx + 1;
     
     while(same == 1){
       printf("Enter your first name(only) MAROONED player!\n");
       scanf("%s",pname);getchar();
       while(ctr < idx){ // index has been incremented already
         if(strcmp(p[ctr].name,pname)==0){ 
           same = 0;
           break;
          }
         ctr++;
       }
       if(ctr == (idx - 1)){
         strcpy(p[idx].name,pname);
         p[idx].score = 0;
         break;
        }
      }
    
     write(p,idx);
     
    system("pause > null");
    return 0;          
    }
    
    
    
    int read(struct player *p){
    int i=0;
    FILE *f;
     
      f = fopen("player.txt","r");        
      
      if(f == NULL)   {
        printf("ERROR : unable to read!\n");
        return 0;
       } 
      while(fscanf(f,"%s %d\n",p[i].name,&p[i].score) == 2)  
       {
        i++;
       }       
     
     fclose(f);
     
    return i;
    }
    
    void write(struct player *p,int idx){
     FILE *fw;
     int ctr;
     
     fw = fopen("player.txt","w");
     
     if(fw == NULL)
      {
       printf("ERROR opening file!\n");
       return;
      }
     
     for(ctr=0;ctr<(idx+1);ctr++)
      {
       fprintf(fw,"%s %d\n",p[ctr].name,p[ctr].score);
      }
    
    fclose(fw);
    
    printf("\nplayer records updated\n");
    
    return;
    }
    
    
     
  5. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    ok thanks i will try and let you know
     

Share This Page