Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   a players record system IN DESPAIR. (http://www.go4expert.com/forums/players-record-despair-t26456/)

jose_peeterson 8Aug2011 22:22

a players record system IN DESPAIR.
 
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


jose_peeterson 9Aug2011 19:38

Re: a players record system IN DESPAIR.
 
************ 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.

jose_peeterson 10Aug2011 22:52

Re: a players record system IN DESPAIR.
 
SOMEBODY HELP
PLEASEEEEEEEEEEEEEEEEEEEEEEEEE.....

thanks

virxen 11Aug2011 03:36

Re: a players record system IN DESPAIR.
 
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;
}


jose_peeterson 11Aug2011 14:36

Re: a players record system IN DESPAIR.
 
ok thanks i will try and let you know


All times are GMT +5.5. The time now is 08:57.