?
Solved

Program output

Posted on 1999-11-30
14
Medium Priority
?
250 Views
Last Modified: 2010-04-15
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;
}
0
Comment
Question by:cbrookhart
  • 8
  • 6
14 Comments
 
LVL 2

Accepted Solution

by:
basant earned 400 total points
ID: 2245234
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
 
LVL 2

Expert Comment

by:basant
ID: 2245237
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
 

Author Comment

by:cbrookhart
ID: 2245342
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
Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

 
LVL 2

Expert Comment

by:basant
ID: 2245373
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
 

Author Comment

by:cbrookhart
ID: 2245540
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
 
LVL 2

Expert Comment

by:basant
ID: 2245764
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
 

Author Comment

by:cbrookhart
ID: 2248226
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
 
LVL 2

Expert Comment

by:basant
ID: 2248381
YOu got the right salary.

It means that it has read the file.

Put the current code.
0
 

Author Comment

by:cbrookhart
ID: 2248403
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
 
LVL 2

Expert Comment

by:basant
ID: 2250324
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
 

Author Comment

by:cbrookhart
ID: 2250590
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
 
LVL 2

Expert Comment

by:basant
ID: 2250666
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
 

Author Comment

by:cbrookhart
ID: 2251592
I got it working now. Thank you for your help.
0
 
LVL 2

Expert Comment

by:basant
ID: 2253179
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
There's never been a better time to become a computer scientist. Employment growth in the field is expected to reach 22% overall by 2020, and if you want to get in on the action, it’s a good idea to think about at least minoring in computer science …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question