Thread: Code compiles but doesn't run

  1. #1
    Registered User dbyte's Avatar
    Join Date
    Jul 2003
    Posts
    15

    Question Code compiles but doesn't run

    Here is my code:
    Code:
    #include <stdio.h>
    
    /*Structure declaration*/
    struct employee
    {
         long id_number;
         float wage;
         float hours;
         float overtime;
         float gross;
    };
    
    /*Variable declaration (global)*/
    struct employee data[5] = { {98401, 10.60}, {526488, 9.75}, {765349, 10.50},
    {34645, 12.25}, {127615, 8.35} };
    
    struct employee get_hours (struct employee data[])
    {
         int temp, i = temp;
    
         for (temp=1; temp<6; ++temp)
         {
             printf ("Enter employee #%06d hours worked: ", data[temp-1].id_number);
             scanf ("%f", &data[temp-1].hours);
         }
         return (data[i]);
    }
    
    struct employee calculate_overtime (struct employee data[])
    {
         int temp, i = temp;
    
         for (temp=1; temp<6; ++temp)
         {
             if (data[temp-1].hours > 40)
                 data[temp-1].overtime = data[temp-1].hours - 40;
             else
                 data[temp-1].overtime = 0;
         }
         return (data[i]);
    }
    
    struct employee calculate_gross (struct employee data[])
    {
         int temp, i = temp;
    
         for (temp=1; temp<6; ++temp)
         {
             if  (data[temp-1].overtime > 0)
                 data[temp-1].gross = (data[temp-1].wage * 40) +
                 ((data[temp-1].wage * 1.5) * data[temp-1].overtime);
             else
                 data[temp-1].gross = (data[temp-1].wage * data[temp-1].hours) +
                 ((data[temp-1].wage * 1.5) * data[temp-1].overtime);
         }
         return (data[i]);
    }
    
    void output (struct employee data[])
    {
         int temp;
    
         printf ("\n\n");
         printf ("--------------------------------------------------------\n");
         printf ("Clock #    Pay Rate     Hours      OT      Gross Pay\n");
         printf ("--------------------------------------------------------\n");
    
         for (temp=1; temp<6; ++temp)
         {
              printf ("%06d     $%5.2f      %5.1f     %5.1f     $%6.2f\n",
              data[temp-1].id_number, data[temp-1].wage, data[temp-1].hours,
              data[temp-1].overtime, data[temp-1].gross);
         }
    }
    
    
    main ()
    {
         int i;  
    
         /*Execute functions*/
         for (i=1; i<6; ++i)
         {
              get_hours (data);
              calculate_overtime (data);
              calculate_gross (data);
         }
         
         output (data);
    
         printf ("\n");
         system("PAUSE");
    }
    This code compiles with no errors, but crashes during the 'output' function. Can anyone assist me in figuring out why this is happening?

    BTW: yes, I am a student.

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    I changed main to int main(), although it really shouldn't have a big impact on the output but lets just use int main() for the sake that int main() is how the standard wants it.

    I added
    #include <stdlib.h>
    to the code because system() comes from stdlib.h
    I find it odd that your compiler compiled it without including stdlib.h

    If problems persist it may have something to do with the way your compiler links perhaps.


    here is the code, (modified)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Structure declaration*/
    struct employee
    {
         long id_number;
         float wage;
         float hours;
         float overtime;
         float gross;
    };
    
    /*Variable declaration (global)*/
    struct employee data[5] = { {98401, 10.60}, {526488, 9.75}, {765349, 10.50},
    {34645, 12.25}, {127615, 8.35} };
    
    struct employee get_hours (struct employee data[])
    {
         int temp, i = temp;
    
         for (temp=1; temp<6; ++temp)
         {
             printf ("Enter employee #%06d hours worked: ", data[temp-1].id_number);
             scanf ("%f", &data[temp-1].hours);
         }
         return (data[i]);
    }
    
    struct employee calculate_overtime (struct employee data[])
    {
         int temp, i = temp;
    
         for (temp=1; temp<6; ++temp)
         {
             if (data[temp-1].hours > 40)
                 data[temp-1].overtime = data[temp-1].hours - 40;
             else
                 data[temp-1].overtime = 0;
         }
         return (data[i]);
    }
    
    struct employee calculate_gross (struct employee data[])
    {
         int temp, i = temp;
    
         for (temp=1; temp<6; ++temp)
         {
             if  (data[temp-1].overtime > 0)
                 data[temp-1].gross = (data[temp-1].wage * 40) +
                 ((data[temp-1].wage * 1.5) * data[temp-1].overtime);
             else
                 data[temp-1].gross = (data[temp-1].wage * data[temp-1].hours) +
                 ((data[temp-1].wage * 1.5) * data[temp-1].overtime);
         }
         return (data[i]);
    }
    
    void output (struct employee data[])
    {
         int temp;
    
         printf ("\n\n");
         printf ("--------------------------------------------------------\n");
         printf ("Clock #    Pay Rate     Hours      OT      Gross Pay\n");
         printf ("--------------------------------------------------------\n");
    
         for (temp=1; temp<6; ++temp)
         {
              printf ("%06d     $%5.2f      %5.1f     %5.1f     $%6.2f\n",
              data[temp-1].id_number, data[temp-1].wage, data[temp-1].hours,
              data[temp-1].overtime, data[temp-1].gross);
         }
    }
    
    
    int main ()
    {
         int i;  
    
         /*Execute functions*/
         for (i=1; i<6; ++i)
         {
              get_hours (data);
              calculate_overtime (data);
              calculate_gross (data);
         }
         
         output (data);
    
         printf ("\n");
         system("PAUSE");
    	 return 0;
    }
    here is the output I got, if this is not correct then check the code...
    Code:
    Enter employee #098401 hours worked: 4
    Enter employee #526488 hours worked: 12
    Enter employee #765349 hours worked: 13
    Enter employee #034645 hours worked: 15
    Enter employee #127615 hours worked: 10
    Enter employee #098401 hours worked: 8
    Enter employee #526488 hours worked: 8
    Enter employee #765349 hours worked: 8
    Enter employee #034645 hours worked: 8
    Enter employee #127615 hours worked: 8
    Enter employee #098401 hours worked: 8
    Enter employee #526488 hours worked: 8
    Enter employee #765349 hours worked: 8
    Enter employee #034645 hours worked: 8
    Enter employee #127615 hours worked: 8
    Enter employee #098401 hours worked: 8
    Enter employee #526488 hours worked: 8
    Enter employee #765349 hours worked: 8
    Enter employee #034645 hours worked: 8
    Enter employee #127615 hours worked: 8
    Enter employee #098401 hours worked: 8
    Enter employee #526488 hours worked: 8
    Enter employee #765349 hours worked: 8
    Enter employee #034645 hours worked: 8
    Enter employee #127615 hours worked: 8
    
    
    --------------------------------------------------------
    Clock #    Pay Rate     Hours      OT      Gross Pay
    --------------------------------------------------------
    098401     $10.60        8.0       0.0     $ 84.80
    526488     $ 9.75        8.0       0.0     $ 78.00
    765349     $10.50        8.0       0.0     $ 84.00
    034645     $12.25        8.0       0.0     $ 98.00
    127615     $ 8.35        8.0       0.0     $ 66.80
    
    Press any key to continue . . .
    Last edited by Lynux-Penguin; 07-21-2003 at 08:01 PM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    Registered User dbyte's Avatar
    Join Date
    Jul 2003
    Posts
    15
    LP,
    Thanks for your quick reply. I'm using the Dev-C++ 4.9.8.0 IDE on a Windows XP laptop. I have used the system("pause") bit in all of my previous code without it causing any problems. Maybe it doesn't require that library to work on Windows machines? At any rate I don't think that's the cause of my problems. I did make the 'int main ()' change though - thanks for the heads-up on that 1.

    The program should ask you to enter the hours worked for each of 5 employees (indicated by their id numbers) only 1 time. After that it should output the data onto your screen. There shouldn't be any repetition of input after you enter hours for the 5, as all calculations are done immediately following each entry of hours worked. Maybe this is a looping issue? Your output makes me think so. Any other ideas?

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Yup, looping problem. Look at this:

    Code:
    //in int main
         for (i=1; i<6; ++i)
         {
              get_hours (data);
              calculate_overtime (data);
              calculate_gross (data);
         }
    
    //in get hours
         for (temp=1; temp<6; ++temp)
         {
             printf ("Enter employee #%06d hours worked: ", data[temp-1].id_number);
             scanf ("%f", &data[temp-1].hours);
         }
    Away.

  5. #5
    Registered User dbyte's Avatar
    Join Date
    Jul 2003
    Posts
    15
    Blackrat,
    Okay, I removed the loop from main(). Code still compiles fine but crashes during running, @ the same point as before. Here is my updated code for main:
    Code:
    int main ()
    {
         /*Execute functions*/
         get_hours (data);
         calculate_overtime (data);
         calculate_gross (data);
         output (data);
    
         printf ("\n");
         /*Keep window open*/
         system("PAUSE");
    }
    All the other code is exactly the same. Were you referring to something else, that maybe I am still not seeing? Thanks for you help.

  6. #6
    Registered User dbyte's Avatar
    Join Date
    Jul 2003
    Posts
    15
    Salem,
    You rule!

    Thanks for you help. More importantly, thanks for your clear explanations. I like it a lot better when I can learn why things are done instead of merely knowing that they are done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My program compiles, but does not run
    By Surfer_Rosa in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 10:25 AM
  2. Replies: 5
    Last Post: 04-17-2003, 07:07 AM
  3. this code COMPILES, but DOESNT WORK
    By Leeman_s in forum C++ Programming
    Replies: 14
    Last Post: 11-01-2002, 06:54 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM