Thread: New to C Programming. Help please

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

    Question New to C Programming. Help please

    Hi everyone. I'm taking a C programming class right now. It's all really new to me. The book I'm using is Programming in C by Stephen G. Kochan. I need help with a practice program. Any help is much appreciated.


    1. Read in data for employee number, hours, and rate and calculate pay (hours x rate).
    2. If hours are over 40, use 1.5 times the hourly rate for all hours over 40.
    3. Process data for 3 people.
    4. Use arrays to read and do calculations.
    5. Sum the final pay for all employees.

    Use good simple data to test your program. Each level of the above program adds some more programming commands. For example, level 1 only requires reading data, doing a calculation, and printing the output. Level 2 requires you to use an IF statement to do overtime pay. Level 3 requires a loop, etc.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So what have you done so far?

    --
    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
    Sep 2006
    Posts
    8,868
    We want to help you, not do your program for you.

    Please post up your attempt to code this problem, and then highlight it, and click on the '#' pound or hash sign, to wrap it in code tags. Good indentation will help a lot, for you and us.

    Ask any specific questions you may have or problems you or the compiler, come across. The more specific your questions are, the more specific our answers tend to be.

    To be a programmer, you must practice a good deal. It won't make you perfect, but it will certainly make you a much better programmer.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Read up on using scanf, for loops, integers and doubles. Read the sections addressing how to create/initialize an array of data, which is grouped data of like elements.

    Write down logically how this program should flow and the logic to add such numbers/salaries of your employees before writing any code.

    Then post what you have done.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    3
    I'm sorry. I didn't mean for it to sound like I wanted you to do it for me. I was at school when I posted this. My laptop had an issue with Bloodshed Dev-C++ so I couldn't get to my program. I'll be back at the school tomorrow and I should be able to use the classroom set of computers that already has the compilers running on them. I'll post what I have so far tomorrow most likely. I'm glad people are willing to help because I think I'll be needing it. =/

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    3
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <iostream.h>
    #include <fstream.h> // For file I/O
    
    void CalcPay( float, float, float& );
    
    const float MAX_HOURS = 40.0; // Maximum normal work hours
    const float OVERTIME = 1.5; // Overtime pay rate factor
    
    int main()
    {
    float payRate; // Employee's pay rate
    float hours; // Hours worked
    float wages; // Wages earned
    float total; // Total company payroll
    int empNum; // Employee ID number
    ofstream payFile; // Company payroll file
    
    payFile.open("payfile.dat"); // Open the output file
    total = 0.0; // Initialize total
    cout << "Enter employee number: "; // Prompt
    cin >> empNum; // Read employee ID no.
    while (empNum != 0) // While employee number
    { // isn't zero
    cout << "Enter pay rate: "; // Prompt
    cin >> payRate; // Read hourly pay rate
    cout << "Enter hours worked: "; // Prompt
    cin >> hours; // Read hours worked
    CalcPay(payRate, hours, wages); // Compute wages
    total = total + wages; // Add wages to total
    payFile << empNum << payRate // Put results into file
    << hours << wages;
    cout << "Enter employee number: "; // Prompt
    cin >> empNum; // Read ID number
    }
    cout << "Total payroll is " // Print total payroll
    << total << endl; // on screen
    return 0; // Indicate successful
    } // completion
    
    //************************************************** ****************
    
    void CalcPay( /* in */ float payRate, // Employee's pay rate
    /* in */ float hours, // Hours worked
    /* out */ float& wages ) // Wages earned
    
    // CalcPay computes wages from the employee's pay rate
    // and the hours worked, taking overtime into account
    
    {
    if (hours > MAX_HOURS) // Is there overtime?
    wages = (MAX_HOURS * payRate) + // Yes
    (hours - MAX_HOURS) * payRate * OVERTIME;
    else
    wages = hours * payRate; // No
    }

    here it is. For some reason it doesn't produce their pay. it just loops back to the employee number and I only need it to process data for 3 people

  7. #7
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Quote Originally Posted by New_Breed View Post
    [code]#include <stdio.h>


    here it is. For some reason it doesn't produce their pay. it just loops back to the employee
    number and I only need it to process data for 3 people
    Then change the while loop condition accordingly.(Hint: use a count variable within the loop
    and increment it every time within the loop.)
    If it's your code you should be able to do that.
    This should be in the C++ forum.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by stevesmithx
    This should be in the C++ forum.
    I have my doubts since the text mentioned appears to be a pure C text. I think New_Breed is merely finding C++ examples online and confusing them with C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    I have my doubts since the text mentioned appears to be a pure C text. I think New_Breed is merely finding C++ examples online and confusing them with C.
    Oh,I see.
    btw,interesting post number!.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

Popular pages Recent additions subscribe to a feed