Thread: Help!!

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    7

    Help!!

    I want to make a c-program which enables the user to input an hourly rate, then an amount of hours worked,

    the output needs to be what the person would earn, but for every hour after 40 hours the person earns 1.5 times the hourly rate per hour

    so if someone was on 10 per hour, 40 hours would be 400, 41 hours would be 415


    im really really stuck, thanks for any help

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So how far have you got? Post your code, and we will help.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    7
    this is what have so far, it was used on a similar programme, also the programme needs to ask how many employee's there are first, and loop it so it can ask, but im starting to think that may be a bit too complicated, and i would be better off just coding the calculator


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main ()
    
    {
    	char status;
    	int count, years, num;
    	
    	void calculate (char, int);
    	
    	printf("Enter the number of emplyee's \n");
    	scanf("%d", &num);
    	fflush(stdin);
    	
    	count=1;
    	
    	for(count; count<= num;count++)
    	{
    		printf("Enter your pay rate>");
    		scanf("%c", &status);
    		fflush(stdin);
    		
    		printf("Enter the number of hours you have worked here this week>");
    		scanf("%d", &years);
    		fflush(stdin);
    		
    		calculate(status, years);
    	}
    	
    }
    
    void calculate (char stat, int yrs)

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Main returns int.
    Function prototypes should be at the start of the file or in a header.
    fflush(studin) is undefined, see FAQ.
    And I don't think "years" is an appropriate name for number of hours worked in a week.
    The rest of the calculate function should just be pure math.

    And I must stress that you shouldn't just put the types in the function prototypes. Copy the exact line of your function and use it as a prototype. It's much easier to see what the function does when looking at the declaration if they have appropriate argument names! Plus some IntelliSense tools reports the information from the prototypes and "int, char" is just not informative.
    Last edited by Elysia; 02-13-2008 at 09:13 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    7
    thanks for that, im gonna have a read through that

    the man thing im struggling with is the multiply thing, i can get it to display a set times table like below

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    main()
    {
    	int count;
            int num;
    	
    	printf ("Please enter a whole number, i will then disply the 12 times table for that number\n");
    	scanf ("&#37;d", &num);
    	fflush (stdin);
    	
    	
    	for ( count = 1; count <= 12; count++ )
    	
    	{ 
    	
    		printf( "%2d * %2d = %3d", count, num, count * num);
    	}
    	
    	
    	
    	
    }
    but i cant figure out how to make it multiply 2 given numbers

    please be kind im very new to programming

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Please take some time to fix the above first. Multiplying is a very basic task. You don't have a book on C or something?
    Your code sample actually successfully multiplies the count and num. But printf is called wrong.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    7
    no i dont have a book, yes that code works, but im still struggling to multiply, although i think i may be on the edge of cracking it

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    7
    thanks for your help so far!!!

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then I suggest you get a book or some tutorials. C isn't some language you can just learn by yourself.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    And I don't think "years" is an appropriate name for number of hours worked in a week.
    Nor is "status" a good name for hourly pay-rate, or the type char to represent this type [unless you only have whole units of money - works well in (now deprecated) Italian Lire, but for example Euros, Dollars or British pounds may be a bit too big for an employer to give a pay-rise of whole units each time].

    So what is different between your calculation of the times-table and the calculation of hours * payrate?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    but i cant figure out how to make it multiply 2 given numbers
    You are multiplying the numbers correctly, you just arent formatting your output to be readable. Try this:
    Code:
    printf( "%2d * %2d = %3d\n", count, num, count * num);
    It should look better with a new line after each multiplication.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    7
    Code:
    	if(hours_worked>40) 
    	{
    		overtime = hours_worked-40;
    		overtime_extra = (wage*1.5)*overtime;
    		total = overtime_extra + (wage*40);
    	}
    
    
    else 
    	{
    		total = hours_worked * wage;
    	}
    this is what i have for the calculation part, but i cant seem to get it to work with the rest of my programme

  13. #13
    Registered User
    Join Date
    Feb 2008
    Posts
    7
    this is what i have, if anyone can help that wil be absoloutely great

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ()
    {
    	char wage;
    	int count, hours_worked, num, overtime, overtime_extra, total ;
    	
    	
    	void calculate (char, int);
    	
    	printf("Enter the number of employee's \n");
    	scanf("&#37;d", &num);
    	fflush(stdin);
    	
    	count=1;
    	
    	for(count; count<= num;count++)
    	{
    		printf("Enter your hourly pay>");
    		scanf("%d", &wage);
    		fflush(stdin);
    		
    		printf("Enter the number of hours you have worked here>");
    		scanf("%d", &hours_worked);
    		fflush(stdin);
    		
    		calculate (wage, hours_worked);
    	}
    	
    
    	if(hours_worked>40) 
    	{
    		overtime = hours_worked-40;
    		overtime_extra = (wage*1.5)*overtime;
    		total = overtime_extra + (wage*40);
    	}
    
    
    else 
    	{
    		total = hours_worked * wage;
    	}
    }

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Come on. Is it so hard to know where to place it?
    Read your code to yourself, explain to yourself what it does and you should be able to see where it fits.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Thumbs up

    First off you have a function defined in main called calculate that takes one argument and returns void (IE: nothing). You should declare calculate before main and make it return a number:
    Code:
    int calculate(int wage, int hours_worked);
    
    int main()
    {
          //code in main
    }
    
    int calculate(int wage, int hours_worked)
    {
        int total, overtime, overtime_extra;
    
        // Calculation code goes here
    
        return total;  // return the total to calling function (in this case main)
    }
    Now you just need to call your function within your for loop to get the total for each iteration.

Popular pages Recent additions subscribe to a feed