• C

Searching for values

I am trying to get a particular value from this program and match that value with a name. It seems that I have done something wrong, since the program doesn't compile. Do I need to create another set of '{' for the subscripts? Am I going about getting this value the wrong way? The value is actually a salary and the highest salary along with the name should be printed. This program was working fine before I added the subscript statements.

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char player[9];
int jersey;
long int salary;
char sport[12];
int hisub=0;
int sub=0;
FILE * infile;
infile = fopen("a:\\sports.dat","r");
for (sub=0; sub<9; sub++);
printf("Player Name    Salary      Jersey        Sport\n");
while (fscanf(infile, "%s%d%d%s",player,&jersey,&salary,&sport)!=EOF)
if (salary[sub]>salary[hisub])
hisub=sub;
{
printf("%s           %3d       %4d      %5s\n",player,salary,jersey,sport);
printf("%s has the highest salary of %d\n",player[hisub],salary[hisub]);
}
fclose(infile);
return 0;
}

A:\sports.c(21) : error C2109: subscript requires array or pointer type
A:\sports.c(21) : error C2109: subscript requires array or pointer type
A:\sports.c(25) : error C2109: subscript requires array or pointer type
cbrookhartAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
What do salary[sub] and salary[hisub] mean?
You've declared salary as a long int, not an array.
0
zultiCommented:
maybe that what you wanted
pay attention for the slight changes.



#include <stdio.h>
#include <stdlib.h>
int main(void)
{
   char player[9],hiplayer[9];
   int jersey;
   long int salary, hisalary;
   char sport[12];
   FILE * infile;
   infile = fopen("a:\\sports.dat","r");
   if (! infile)
      return -1 ;
   printf("Player Name    Salary      Jersey        Sport\n");
   while (fscanf(infile, "%s%d%d%s",player,&jersey,&salary,&sport)!=EOF)
   {
      if (salary>hisalary)
      {
         hisalary=salary ;
         strcpy(hiplayer,player) ;
      }
      printf("%s           %3d       %4d      %5s\n",player,salary,jersey,sport);
   }
   printf("%s has the highest salary of %d\n",hiplayer,hisalary);
   fclose(infile);
   return 0;
}
0
cbrookhartAuthor Commented:
strcpy is undefined.

A:\sports.c(24) : warning C4013: 'strcpy' undefined; assuming extern returning int

Is there something in the wrong place?
0
deightonprogCommented:
#include <string.h>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cbrookhartAuthor Commented:
I realized after posting the above message that I forgot to put in the string header file.

Thanks for the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.