Thread: please check my homework

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Smile please check my homework

    I just need someone to check my homework. The instructions are as follows:

    Create a software solution to the following problem: A simple payroll application system is needed with the following specifications:
    The sequential file has already been created for you so you will not have to create it. I will send this to you via e-mail. The data file is called payroll.dat It has the following test data:
    Doe 1234 40 10.00
    Smith 4567 35 25.00
    Jones 9345 20 60.00

    Write a c program that will allow the user a number of choices to process and view the information.
    The main program should allow the user to enter one of four choices
    Choice One: Show all of the records, including name, employee number, hours worked, rate of pay, and gross pay (hours worked * rate of pay)
    Choice Two: Show all of the records in which the gross pay is equal to or less than 400.00.
    Choice Three: Show all of the records in which gross pay is greater than 400.00
    Choice Four: End Program Run
    Be sure that your program rewinds the file for each choice made (excluding choice four (ending the programming run)

    this is my code:
    Code:
    #include <stdio.h>
    
    int main( void )
    { 
       int request;     
       int employee;    
       double payrate; 
       double hours; 
       char name[ 5 ];
       FILE *cfPtr;   
    
       if ( ( cfPtr = fopen( "payroll.dat", "r" ) ) == NULL ) {
          printf( "File could not be opened\n" );
                         system("pause");
       }
       else {
          
        
          printf( "Enter request\n"
             " 1 - Show all records\n"
             " 2 - Show all of the records in which the gross pay is equal to or less than 400.00\n"
             " 3 - Show all of the records in which gross pay is greater than 400.00\n"
             " 4 - End of run\n? " );
          scanf( "%d", &request );
    
         while ( request != 4 ) { 
    
             fscanf( cfPtr, "%s %d %4lf %4lf", name, &employee, &hours, &payrate );
    
             switch ( request ) { 
    
                case 1:
                   printf( "\nShow all of the records:\n" );
                   printf("%-9s %-15s %-17s %-17s %s\n", 
                      "Name", "employee #", "hours worked", "rate of pay", "gross pay");
     
                   while ( !feof( cfPtr ) ) { 
                      printf( "%-5s %11d %16.2f %17.2f %17.2f\n",
                         name, employee, hours, payrate, payrate * hours );
                    
                      fscanf( cfPtr, "%s%d%lf%lf", name, &employee, &hours, &payrate ); 
                        
                   } 
    
                   break;
    
                case 2:
                   printf( "\nShow all of the records in which the gross pay is equal to or less than 400.00\n" );
                   printf("%-9s %-15s %-17s %-17s %s\n", 
                      "Name", "employee #", "hours worked", "rate of pay", "gross pay");
                   
                   while ( !feof( cfPtr ) ) { 
                         
                      if ( payrate * hours <= 400.00 ) {              
                         printf( "%-5s %11d %16.2f %17.2f %17.2f\n", 
                            name, employee, hours, payrate, payrate * hours );
                      } 
                      
                      fscanf( cfPtr, "%s%d%lf%lf", name, &employee, &hours, &payrate ); 
                        
                   }
    
                   break;
    
                case 3:
                   printf( "\nShow all of the records in which gross pay is greater than 400.00\n" );
                   printf("%-9s %-15s %-17s %-17s %s\n", 
                      "Name", "employee #", "hours worked", "rate of pay", "gross pay");
                   
                   while ( !feof( cfPtr ) ) { 
    
                      if ( payrate * hours > 400.00 ) {
                         printf( "%-5s %11d %16.2f %17.2f %17.2f\n", 
                            name, employee, hours, payrate, payrate * hours );
                      }
    
                      fscanf( cfPtr, "%s%d%lf%lf", name, &employee, &hours, &payrate );
                   } 
    
                   break;           
                
             }
    
             rewind( cfPtr ); 
    
             printf( "\n? " );
             scanf( "%d", &request );
          } 
    
          printf( "End of run.\n" );
          fclose( cfPtr );
       } 
     
       return 0;
    
    }
    i can't figure out if i'm doing something wrong but according to the instructions, the test data in the data file should appear as:
    Doe 1234 40 10.00
    Smith 4567 35 25.00
    Jones 9345 20 60.00

    which it does when i open the file, but when i run my program it appears as :
    Doe 1234 40 10.00
    0 1234 40 10.00
    Smith 4567 35 25.00
    Jones 9345 20 60.00

    i don't know if there's a problem with my code or if it's the file. any feedback you can give me to resolve this or improve my code is appreciated. thanks in advance

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    See this FAQ article explaining why it's a bad idea to use feof() as the loop terminal condition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. How can i check a directory for certain files?
    By patrioticpar883 in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 05:27 PM
  3. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  4. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM