• C

structures

I have written a program involving structures. I have not been able to get this program to compile without errors and warnings. These errors and warnings are not helpful in telling me what the problem is. I have tried various combinations and it still doesn't work. Based on the error descriptions, it seems like it is something simple. What have I done wrong with this program?
#include <stdio.h>
struct employee {
     char name [10];
       int hours;
       float wage;
       }
int main (void)
{
   float pay;
   pay = hours * wage;
   struct employee person;
   FILE *in data;
   in data=fopen("a:payroll.dat", 'r');
   fscanf(in data, "%s%d%f; person.name, &person.hours, &person.wage);
   {
      pay = person.hours * person.wage;
        printf("person.hours, person.wage, pay");
   }
printf("Total Salary");
fclose (indata);
return 0;
}

A:\payroll.c(12) : error C2628: 'employee' followed by 'int' is illegal (did you forget a ';'?)
A:\payroll.c(15) : error C2065: 'hours' : undeclared identifier
A:\payroll.c(15) : error C2065: 'wage' : undeclared identifier
A:\payroll.c(15) : warning C4244: '=' : conversion from 'int ' to 'float ', possible loss of data
A:\payroll.c(16) : error C2143: syntax error : missing ';' before 'type'
A:\payroll.c(17) : error C2275: 'FILE' : illegal use of this type as an expression
        c:\program files\microsoft visual studio\vc98\include\stdio.h(156) : see declaration of 'FILE'
A:\payroll.c(17) : error C2065: 'in' : undeclared identifier
A:\payroll.c(17) : error C2146: syntax error : missing ';' before identifier 'data'
A:\payroll.c(17) : error C2065: 'data' : undeclared identifier
A:\payroll.c(18) : error C2146: syntax error : missing ';' before identifier 'data'
A:\payroll.c(18) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'const int '
A:\payroll.c(18) : warning C4024: 'fopen' : different types for formal and actual parameter 2
A:\payroll.c(18) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct _iobuf *'
A:\payroll.c(19) : warning C4047: 'function' : 'struct _iobuf *' differs in levels of indirection from 'int '
A:\payroll.c(19) : warning C4024: 'fscanf' : different types for formal and actual parameter 1
A:\payroll.c(19) : error C2146: syntax error : missing ')' before identifier 'data'
A:\payroll.c(19) : error C2198: 'fscanf' : too few actual parameters
A:\payroll.c(19) : error C2001: newline in constant
A:\payroll.c(24) : error C2143: syntax error : missing ')' before 'string'
A:\payroll.c(24) : error C2143: syntax error : missing '{' before 'string'
A:\payroll.c(24) : error C2059: syntax error : '<Unknown>'
A:\payroll.c(24) : error C2059: syntax error : ')'
A:\payroll.c(26) : error C2059: syntax error : 'return'
A:\payroll.c(27) : error C2059: syntax error : '}'
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.

mmessuriCommented:
cbrookhart:

here are the fixes to your code (note that while not knowing the format of your .dat file [is it just text or is it binary] I have not tested this for the desire results):

#include <stdio.h>

struct employee {
   char name [10];
      int hours;
      float wage;
};

int main (void)
{
      float pay;
   struct employee person;
   FILE *indata = NULL;

   indata=fopen("a:payroll.dat", "r");
   fscanf(indata, "%s%d%f", person.name, &person.hours, &person.wage);
   fclose (indata);

   pay = person.hours * person.wage;
   printf("%d, %f, %f", person.hours, person.wage, pay);
      printf("Total Salary");

      return 0;
}

-----------

Since it looks like you are dealing with a know data size, I would recommend that you store your .dat file in binary format.  In this format you can perform an fread and read in the entire struct.  Also when sending the source data to a file you could then perform an fwrite.

For example, the above code for a program described as above would be:

#include <stdio.h>
#include <mem.h>

struct employee {
   char name [10];
      int hours;
      float wage;
};

int main (void)
{
      float pay = 0;
   struct employee person;
   FILE *indata = NULL;

   memset(&person, 0x0, sizeof(struct employee));

   indata=fopen("a:payroll.dat", "r");

   if(indata != NULL){
         fread(&person, sizeof(struct employee), 1, indata);
         fclose (indata);
   }

   pay = person.hours * person.wage;
   printf("%d, %f, %f", person.hours, person.wage, pay);
      printf("Total Salary");

      return 0;
}
0
cbrookhartAuthor Commented:
The reason I am rejecting this answer is due to the fact that this question has some how been posted twice.

mmessuri:  thank you for your help. I will try your suggestions and see what happens.
0
laaxCommented:
Hai cbrookhart,

   I have modified your program.  It is getting compiled in Ansi C compiler.  If you have any doubts post them.  
--------------------------------
#include <stdio.h>
struct employee {
     char name [10];
int hours;
float wage;
};  /* semicolon added */

int main (void)
{
   float pay;
 /*  pay = hours * wage;
     hours and wage are not visible here.
 */
   float totalSalary = 0;  /* new Variable */
   struct employee person;
   FILE *indata; /* space removed */
   indata=fopen("a:payroll.dat", "r"); /* have put 'r in double quotes */
   /*
      Please check here whether the file is opened or not.
   */
   fscanf(indata, "%s%d%f", person.name, &person.hours, &person.wage);
   {
      pay = person.hours * person.wage;
  /*  printf("person.hours, person.wage, pay");  */
      printf ( "Person.hours  %d Person.Wage  %f Pay %f\n", \
             person.hours, person.wage, pay );
      totalSalary += pay;  /* newly added line */
   }
printf("Total Salary %f", totalSalary);
fclose (indata);
return 0;
}


--------------------------------

  This program will work only on the first record.  Put the scanf() and calculation is a for loop.

Bye.
Laax.
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
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

cbrookhartAuthor Commented:
This program is still not compiling correctly. This is the updated code that I have.

struct employee {
     char name [10];
       int hours;
       float wage;
       };
main ()
{
   float pay;
   float total=0;
   struct employee person;
   /*pay = person.hours * person.wage;*/
   FILE *indata;
   indata = fopen("a:payroll.dat","r");
   while ( !(feof(indata)) )
   {
   fscanf(indata,"%s%d%f", person.name, &person.hours,&person.wage);
      pay = person.hours * person.wage;
        total += pay;
        printf("%d %.2f %.2f\n",person.hours,person.wage,pay);
   }
}
printf("\nTotal Salary is %.2f\n",total);
fclose (indata);
return 0;
}

These are the error messages I am receiving:

a:\payroll.c(28) : error C2143: syntax error : missing ')' before 'string'
a:\payroll.c(28) : error C2143: syntax error : missing '{' before 'string'
a:\payroll.c(28) : error C2059: syntax error : '<Unknown>'
a:\payroll.c(28) : error C2059: syntax error : ')'
a:\payroll.c(30) : error C2059: syntax error : 'return'
a:\payroll.c(31) : error C2059: syntax error : '}'

The data file that I created in notepad is getting saved as filename.dat.txt even though I specified the file type as all files and saved the file as filename.dat

I thought the problem might be with two '\n' in printf statement at the end of the program. The first four errors seem to be with this particular line. How would I use the for statement in this program?
0
laaxCommented:
Hai cbrookhart,

   you have put one extra '}' before the last printf() statement.  Please remove it.

--------------------------
  printf("%d %.2f %.2f\n",person.hours,person.wage,pay);
   }
} /*  <---- REMOVE this line */
printf("\nTotal Salary is %.2f\n",total);
fclose (indata);
return 0;
}

---------------------------
Laax.
0
cbrookhartAuthor Commented:
I removed the extra bracket, but I am getting an illegal escape sequence error. I have gone back through the program to be sure I have ended each statement with a ; and each printf statement has two quote marks. What does illegal escape sequence mean?
0
cbrookhartAuthor Commented:
I think I figured out what was causing the above error. I removed the backslash from the following line:

printf ( "Person.hours  %d Person.Wage  %f Pay %f\n", \
person.hours, person.wage, pay );

The program compiles and links without errors and warnings, but crashes from an invalid page fault when it is executed.
0
laaxCommented:
Hai cbrookhart,

char name [10];

You have declared 10 bytes for 'name'.  Does any of your record has the 'name' field value more than 10 characters?

  If so, please increase the size of the array according to your (application) need.  Ok?

Laax.
0
cbrookhartAuthor Commented:
I think the problem was actually with the data file. It was in the format of filename.dat.txt. I renamed it and it worked. The program output is not correct. The output displays the following:

Person.hours 15 Person.wage 7.250000 Pay 108.750000

Total salary is 108.75

It supposed to take $10 *40 (2 times) and $7.25 * 20 and $7.25 * 15 display those totals, and then add it up and display the total.
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.