finally my salary program but...........

This is a discussion on finally my salary program but........... within the C Programming forums, part of the General Programming Boards category; #include <stdio.h> int main() { float salary; int rate, hours; rate = 0; hours = 0; printf( "Enter # of ...

  1. #1
    pancho
    Guest

    finally my salary program but...........

    #include <stdio.h>

    int main()
    {
    float salary;
    int rate, hours;
    rate = 0;
    hours = 0;


    printf( "Enter # of hours worked (-1 to end): " );
    scanf( "%d", &hours );


    while ( hours != -1 ){
    printf( "Enter hourly rate of the worker ($00.00): " );
    scanf( "%d", &rate );

    if ( hours <= 40 )
    salary = hours * rate;

    if ( hours > 40 )
    salary = ( float ) (hours - 40) * (1.50 * rate) + (40 * rate);

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

    printf( "Enter # of hours worked (-1 to end): " );
    scanf( "%d", &hours );
    }


    return 0;
    }



    Almost done guys!!! i just want to know how to input decimal values like 9.56 in the rate field without causing a infinite loop...thaaaaanks!

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    make rate a float instead of an int.
    make sure you use the correct format specifier (%blah blah) in the rate scanf statement.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Also do yourself a favour and after every scanf statement add this line....

    while (getchar() != '\n') continue;

    This will effectively clean up any crap left in the input stream by scanf()
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    pancho
    Guest

    floating

    like this??????

    #include <stdio.h>

    int main()
    {
    float salary;
    float rate;
    int hours;
    rate = 0;
    hours = 0;


    printf( "Enter # of hours worked (-1 to end): " );
    scanf( "%d", &hours );


    while ( hours != -1 ){
    printf( "Enter hourly rate of the worker ($00.00): " );
    scanf( "%.2f", &rate );
    .............

    if not how?
    plz help me because im almost sleeping after 5 hours trying to figure out this one...thanks

  5. #5
    pancho
    Guest
    thanks for the cleaning code buuuut we are not supposed to know that yet. its an assignment and if I put that, the teacher will think that I stole the code or sumthing like that.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    try it.... does it compile? if so does it work as expected?
    Looks ok to me but i'm sleepy and cannot remember the format specifiers for scanf() off the top of my head!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    pancho
    Guest

    Unhappy compiles but doesnt work...argggg

    heelp me plzzz....compiles but doesnt work...

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Do not worry about your teacher. There is nothing wrong in seeking help from others. Better that than you spend another 5 hours chewing your fingernails away lol

    for your information....

    while ( boolean condition) statement;

    in this case statement will be executed if the boolean condition is TRUE. This will keep looping continuously until the boolean condition becomes FALSE. Anywhere you see a statement you can replace it with multiple statements by enclosing in chicken lips {
    statements;
    go;
    here;
    }

    getchar() returns the next char from stdin (the input stream)
    If scanf() left no bad input there will be a newline char in the stream ('\n') otherwise there could be anything. So the loop will continue extracting chars from stdin until it hits the newline char and then the loop exits.
    continue is a keyword that just makes the loop go to its next iteration and can be safely removed in this example but it helps make the code more readable.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    post full code and i'll give it a once over before hitting the sack.....
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    pancho
    Guest

    go 2 sleep

    thanks man....u better go to sleep....i will do it and try it tomorrow...can not think right know


    thanks again

  11. #11
    Unregistered
    Guest

    Thumbs up Here you go dude

    Here dude, it's 1.5 * salary, not 3/2 because 3/2=1.
    Also if you want -1 to exit program right away,
    it has to be the last statement in your while loop,
    but you also need it before the loop too, for the first time through.
    Here you go:

    #include <stdio.h>

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

    printf( "Enter # of hours worked (-1 to end): " );
    scanf( "%d", &hours );

    while ( hours != -1 ){

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

    if ( hours <= 40 )
    salary = hours * rate;

    else
    salary = ( float ) (hours - 40) * (1.5 * rate) + (40 * rate);

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

    printf( "Enter # of hours worked (-1 to end): " );
    scanf( "%d", &hours );

    }


    return 0;
    }

  12. #12
    Unregistered
    Guest

    1.5 * rate I mean. look at the code

    1.5 * rate I mean. look at the code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. salary program heeeeeeeelp....
    By juan in forum C Programming
    Replies: 1
    Last Post: 02-09-2002, 12:55 PM
  5. salary program heeeelp!
    By pancho in forum C Programming
    Replies: 6
    Last Post: 02-02-2002, 08:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21