• C

Program output

I am working on a program that is supposed to calculate a payroll for four individuals and display the payment amount for each person. The program is then supposed to give the overall payroll total. I'm not sure what I have done wrong to produce the following output:

Person.hours 15 Person.wage 7.250000 Pay 108.750000

Total salary is 108.75

I want to have it print in a column format with the appropriate data under each heading:

name hours worked  hourly wage  pay

This is the code which has been compiled and linked without errors and warnings:
#include <stdio.h>
struct employee {
     char name [8];
       int hours;
       float wage;
       };
int main (void)
{
   float pay;
   float totalSalary = 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;
        /*printf("person.hours,person.wage,pay");*/
        printf("Person.hours %d Person.Wage  %f Pay %f\n",person.hours, person.wage, pay);
        totalSalary += pay;
   }
printf("\nTotal Salary is %.2f\n",totalSalary);
fclose (indata);
return 0;
}
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.

basantCommented:
What output u get. What happens when u run the program.
Show us the File Content.

I can guess one problem. If there r
spaces after the line entry,
you feof will still return 0
if there is no more entries but spaces or empty lines.

You can check the return value of fscanf and break if it fails.

Modify ur loop like this :


   while ( !(feof(indata)) )
   if( ! fscanf(indata,"%s%d%f",person.name, &person.hours,&person.wage))
       break; // Change this line
....
}
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
basantCommented:
One more problem can happen :
Check whether you are able to open the file or not :


   indata = fopen("a:payroll.dat","r");
if( !indata)
{
    printf("\n Error opeining the file");
exit( 0);
}
0
cbrookhartAuthor Commented:
exit is not defined in the program and int exit; does not work. I created the file in notepad and saved it as payroll.dat with the extension being all files. When I looked at the file name in explorer, it was actually payroll.dat.txt I renamed it in DOS to payroll.dat. I recreated the data file in C++ and saved it as payroll.dat

The data file content is:

Joe   40  10.00
Sue   40  10.00
Mary  20   7.25
Peter 15   7.25

I added the following line:

if( ! fscanf(indata,"%s%d%f",person.name, &person.hours,&person.wage))
       break;

This compiles and links without errors, but the program crashes when it runs. The crash is a fatal exception error and the text of the message says <UNKNOWN> caused an exception c0000006H in module <unknown> at 0000:bff85a5d.
0
INTRODUCING: WatchGuard's New MFA Solution

WatchGuard is proud to announce the launch of AuthPoint, a powerful, yet simple, Cloud-based MFA service designed to eliminate the vulnerabilities that put your data, systems, and users at risk.

basantCommented:
I think you are unable to open the file.

Try this :
   indata = fopen("a:payroll.dat","r");
if( !indata)
{
    printf("\n Error opeining the file");
return 0;
}

or
include stdlib to define exit.
using
#include <stdlib.h>




-------------------------------
Also check for the File Name
"a:payroll.dat"

Change it to "a:\payroll.dat"
if it is in Root Directory of A:
0
cbrookhartAuthor Commented:
If I use "a:\payroll.dat" the compiler thinks that \p is being used as an escape sequence, which is invalid. The file is in the root of the A: drive. I left the test code in and I didn't see the message about not being able to open the file.
0
basantCommented:
Sorry Use
"A:\\payroll.dat"

In C/C++ \ charater is represented as
'\\'

Is ur file open failing or succeding.

I think your program fails because you
are trying to read a file which is not
opened successfully.


if( !indata)
{
    printf("\n Error opeining the file");
return 0;
}


YOu did not answer fot that check condition.
0
cbrookhartAuthor Commented:
I have changed the line in the fopen to a:\\payroll.dat. I have put in the test condition, and everything compiles without errors. I have executed the program from within C++, from DOS prompt, and from windows explorer. It is displaying: 157.250000108.750000

Total Salary is 108.750000

I am not seeing the test message that I added (cannot open file). Is there something that I am missing from the code that is causing this output? Is there another way to find out if the problem is a result of not being able to open the data file?
0
basantCommented:
YOu got the right salary.

It means that it has read the file.

Put the current code.
0
cbrookhartAuthor Commented:
How can the salary be right?

Joe   40  10.00 --> $400
Sue   40  10.00 --> $400
Mary  20   7.25 --> $145
Peter 15   7.25 --> $108.75

Total Salary: $1053.75

Since the program is only reading the last salary, it seems like there is something missing. I'm not sure what it is that's missing.

Current Code:
<include stdio.h>
struct employee {
     char name [8];
       int hours;
       float wage;
       };
int main (void)
{
   float pay;
   float totalSalary = 0;
   struct employee person;
   /*pay = person.hours * person.wage;*/
   FILE *indata;
   indata = fopen("a:\\payroll.dat","r");
   if( !indata)
   {
         printf("\n Error in opening file");
         return 0;
   }
   while ( !(feof(indata)) )
   fscanf(indata,"%s%d%f",person.name, &person.hours,&person.wage);
   {
      pay = person.hours * person.wage;
        /*printf("person.hours,person.wage,pay");*/
        printf("%d%f%f\n",person.hours, person.wage, pay);
        totalSalary += pay;
   }
printf("\nTotal Salary is %.2f\n",totalSalary);
fclose (indata);
printf("\n");
printf("Press<enter>to continue\n"); /*added so that program will pause when run through windows explorer*/
while (getchar()!='n');
return 0;
}

0
basantCommented:
You have done the basic error in while
loop. This is the currect one.

   while ( !(feof(indata)) )
   { /* Note the curly bracket you have
used it incorrectly */


   fscanf(indata,"%s%d%f",person.name, &person.hours,&person.wage);
      pay = person.hours * person.wage;
  /*printf("person.hours,person.wage,pay");*/
  printf("%d%f%f\n",person.hours, person.wage, pay);
  totalSalary += pay;
   }


I am quite sure that now it will work.
0
cbrookhartAuthor Commented:
The salary is now correct, however, the numbers are still printing together. The output is now:
4010.000000400.000000
4010.000000400.000000
207.250000145.000000
157.250000108.750000\

What am I missing so that the out looks like:

Name   HoursWorked   HourlyWage  Pay
Sue    40            $10.00      $400.00
..
..
..
..

It seems like the program is skipping over the name, printing hours worked and hourly pay as one number with extra numbers after the decimal point even though %.2f is being used. When I created the data file, I typed in 10.00 for the first two lines. I thought that might be causing the extra 0's to appear. I have removed them from the data file, but it still prints on the screen.
0
basantCommented:
Correct the printf


  printf("%5d %6.2f %6.2f\n",person.hours, person.wage, pay);


Adjust the Fields to left/right whatever you want. Read the printf help properly
0
cbrookhartAuthor Commented:
I got it working now. Thank you for your help.
0
basantCommented:
Though I helped u but later I thought that this site is not for this kind of education b/g questions. Anyway, I hope u had understood many concepts from this question.
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.