Thread: Can't get this to compile

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    2

    Can't get this to compile

    I'm new at this and i'm try to write a code which is a salary program. It has to show hours worked which cant execees 40 and overtime which can't exceed 72

    Another problem is that, if the hours are less than 40 and over time is indicated overtime hours have to be transfered to reg hours and the remaining is the overtime hours

    This is my code please help

    int main()
    {
    float salary, rate;
    int hours, overtime, tryagain;

    printf("Enter number of hours worked:\n " );
    scanf( "%d", &hours );

    printf("Enter number of overtime hours worked:\n");
    scanf( "%d", &overtime);

    if ( hours < 0 || hours >40)
    printf("The number of hours should not exceed 40, please try again\n");

    if (overtime <0 || overtime>72)
    printf("The number of hours should not exceed 72, please try again\n");

    printf( "Enter hourly rate of the worker ($00.00): " );
    scanf( "%d", &rate );

    salary = (overtime) * (1.5 * rate) + (hours * rate);

    printf( "Salary is %f\n", salary );

    return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Hmm... I suggest changing your program to just ask the user for how many hours, total, the employee worked.

    But, if you really want to turn overtime hours into normal hours, here's some pseudocode...
    Code:
    // Z = the number of hours we should add.
    Z = 40 - hours;
    
    // This prevents adding more hours
    // Than there are available
    if (Z < overtime)
    {
     Z = Overtime;
    }
    
    Add Z to hours
    Subtract Z from overtime
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    ....here is a short 25 liner which will do the job neatly......

    Code:
    #include <stdio.h>
    
    int main() 
    { 
    	float salary, rate; 
    	int hours, overtime,leftover;
    
    	printf("\n\tHours Worked: " ); 
    	scanf( "%d", &hours ); 
    	if (hours>40){
    		leftover=(hours-40);  
    		hours=40;		}
    	if (hours>72) { 
    	   	   printf("\t72 Hours is Max Limit per Week!\n\t");    
    		   return 0;
    		   }
    	
    	printf( "\tHourly Rate of the Worker ($0.00): $" ); 
    	scanf("%f",&rate);
    	
    	salary=(hours*rate)+(rate*1.5*leftover);
    	printf("\tSalary is $%f\n\n\t",salary);
    
    return 0; 
    }
    work? enjoy if it does.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>if (hours>40)
    {
    leftover=(hours-40);
    hours=40;
    }
    if (hours>72) {

    Should this be
    if((hours+leftover)>72)
    as previous if() has set hours == 40 if it was greater than 40. (so will never be greater than 72)

    Remember to init leftover=0 or final calc will be out.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C and C++ compile speed
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-02-2007, 02:37 PM
  2. Compile as you type
    By Rocketmagnet in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-07-2006, 01:36 PM
  3. How to compile mfc libs from platform sdk
    By tjcbs in forum Windows Programming
    Replies: 6
    Last Post: 11-19-2006, 08:20 AM
  4. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 09:05 PM
  5. How can I compile C or C++ with Visual Studio .NET?
    By Dakkon in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 02:58 PM