C: Program That Calculates Pay Based On Salary And Hours.

 https://github.com/pereiradaniel/beginning_c/blob/master/ch2/exercises/2_4/exercise2_4.c

// Exercise 2-4

// Write a program that prompts for the user’s weekly pay in dollars and the
// hours worked to be entered through the keyboard as floating-point values. The program
// should then calculate and output the average pay per hour in the following form:

//     Your average hourly pay rate is 7 dollars and 54 cents.

#include <stdio.h>
double checkInput(double input, double min, double max);

int main(int argc, char* argv[])
{
    double hours = 0, rate = 0;

    // Get input for hours:
    printf("Enter number of hours worked: ");
    while(hours == 0) {
        scanf(" %lf", &hours);
        hours = checkInput(hours, 1, 10000);
    }
    
    // Get input for salary:
    printf("Enter hourly rate: ");
    while(rate == 0)
    {
        scanf(" %lf", &rate);
        rate = checkInput(rate, 1, 100);
    }
    
    // Calculate pay:
    printf("Pay: $%.2f\n", hours * rate);

    return 0;
}

double checkInput(double input, double min, double max)
{
    double result = 0;

    if (input < min || input > max) // out of bounds, invalid!
        printf("Enter a number between %.f or %.f!\n", min, max);
    else // inbounds, validate!
        result = input;
    
    return result;
}
https://github.com/pereiradaniel/beginning_c/blob/master/ch2/exercises/2_4/exercise2_4.txt
==808== Memcheck, a memory error detector
==808== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==808== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==808== Command: ./exercise2_4
==808==
Enter number of hours worked: 0 
Enter a number between 1 or 10000!
10001
Enter a number between 1 or 10000!
300
Enter hourly rate: 0
Enter a number between 1 or 100!
101
Enter a number between 1 or 100!
50
Pay: $15000.00
==808== 
==808== HEAP SUMMARY:
==808==     in use at exit: 0 bytes in 0 blocks
==808==   total heap usage: 2 allocs, 2 frees, 2,048 bytes allocated
==808==
==808== All heap blocks were freed -- no leaks are possible
==808==
==808== For lists of detected and suppressed errors, rerun with: -s
==808== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Popular posts from this blog

C programming and relevancy

Shakespeare AI: My lady is more beauteous than a rose.

C: Temperature Conversion With Main Repeat