a players record system IN DESPAIR.

jose_peeterson's Avatar, Join Date: May 2011
Contributor
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
0
jose_peeterson's Avatar, Join Date: May 2011
Contributor
************ 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.
0
jose_peeterson's Avatar, Join Date: May 2011
Contributor
SOMEBODY HELP
PLEASEEEEEEEEEEEEEEEEEEEEEEEEE.....

thanks
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
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;
}
0
jose_peeterson's Avatar, Join Date: May 2011
Contributor
ok thanks i will try and let you know