0

I have written the code, displaying a menu to the user so they can select a payrate, and once they have done that they need to enter amount of hours worked. From there I am trying to calculate total gross pay, taxes, and net pay before the loop ends. I need to use #defined constants, with tax being 15% of the first $300. Or at the very least how and where would I add in a simple calculation for the pay rate multiplied by hours?

#include <stdio.h>
#include <ctype.h>
int main(void)
{
int pick;
int hours;
float total;

do {

    printf_s("************************************************\n");
    printf_s("Enter the number corresponding to the desired pay rate or action:\n");
    printf_s("\n1) $8.75/hr\n");
    printf_s("\n2) $9.33/hr\n");
    printf_s("\n3) $10.00/hr\n");
    printf_s("\n4) $11.20/hr\n");
    printf_s("\n5) quit\n");
    printf_s("************************************************\n");

    scanf_s("%d", &pick);
    printf("Enter the number of hours: \n");
    scanf_s("%d", &hours);

    switch (pick)
    {
    case 1: total = hours * 8.75;
        break;

    case 2: total = hours * 9.33;
        break;

    case 3: total = hours * 10.00;
        break;

    case 4: total = hours * 11.20;
        break;

    case 5:
        break;

        return 0;


    }
} while (pick != 5);
}
Lundin
  • 195,001
  • 40
  • 254
  • 396
user3247128
  • 359
  • 1
  • 3
  • 9
  • What is your question? – DevSolar Apr 13 '16 at 07:39
  • 2
    You asked this [same question](https://stackoverflow.com/questions/36468972) about a week ago. If you couldn't figure it out in a week's time, then you should seriously consider getting a tutor. – user3386109 Apr 13 '16 at 07:40
  • Oh, and... neither `printf_s` nor `scanf_s` are standard functions. Using implementation-defined extensions when it's not really necessary to solve your problem just makes it harder to a) find documentation, b) get help. – DevSolar Apr 13 '16 at 07:42
  • @user3386109 I have not been working on this for a week. I fixed my code based on the previous answer and am only now getting back to working on this. I'm just trying to figure out how and where to add the calculation logic. – user3247128 Apr 13 '16 at 07:47
  • @DevSolar: That depends on your definition of standard. They're an optional part of C11 specified in Annex K (but seldom if ever implemented on Unix systems), and they're standard on Microsoft systems (though the interface there is a bit different from the interface in Annex K). See [Do you use the TR-24731 'safe' functions?](https://stackoverflow.com/questions/372980/do-you-use-the-tr-24731-safe-functions) for my take on their usefulness. But it is not reasonable to criticize people for using them — Microsoft firmly encourages their use. – Jonathan Leffler Apr 13 '16 at 07:47
  • @DevSolar I am trying to figure out how and where to add the simple calculation such as just multiplying the selected payrate by the chosen amount of hours. I am using printf_s because I am just going by visual basic's weird logic :) – user3247128 Apr 13 '16 at 07:48
  • @JonathanLeffler: First I'd have to check whether the OP is using the standard or the Microsoft type of the function. Then I'd have to find a compiler (-setting) not choking on them. It makes the code fail the "C" in MVCE on quite a few systems.... – DevSolar Apr 13 '16 at 07:54
  • @user3247128: You could use [`puts()`](http://en.cppreference.com/w/c/io/puts) instead since you're not doing *formatted* output in the first place. And your problem is not actually C-related, your problem is "how do I turn a monolithic problem into small subproblems I can solve programmatically". That's something not really suited for a Q&A site. I second user3386109: Find a tutor. – DevSolar Apr 13 '16 at 07:57
  • @DevSolar: You can safely assume they are not using anything other than the Microsoft versions — the other implementations are corner cases. There's a move afoot to remove them from the next C standard; I'll support it, not that my opinion will be asked. When people use them, they're using them because Microsoft forcibly pushes them to do so. Think of them as 'Windows functions' from the Windows API, because that's basically what they are. By all means add a Windows-related tag. But they're firmly within scope for C questions. (Google search 'site:msdn.microsoft.com function_s' finds them). – Jonathan Leffler Apr 13 '16 at 07:58
  • @DevSolar as I am sure you yourself were once, I am a beginner. I've been learning for 5 weeks now, I have not been struggling through this for months, I am simply looking for some guidance here. A tutor is most definitely very premature when I am still new at this. – user3247128 Apr 13 '16 at 08:02
  • @user3247128: No, actually that is *exactly the right time* to find a (good) tutor. You have nothing to unlearn yet. Once bad habits have become second nature, it takes a concentrated effort to get rid of them. Don't go for online tutorials, though. They are usually of rather questionable quality. I'd prefer [a good book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and visits to [CodeReview.SE](http://codereview.stackexchange.com/). – DevSolar Apr 13 '16 at 08:04
  • Question re-opened, it is not a duplicate. If you have different follow-up questions to an old question, it is correct to post a new question, as was done in this case. – Lundin Apr 13 '16 at 13:49

1 Answers1

2

First you need to initialize total to 0.

Also, since you will be able to add more than once you need to add the value to total instead of overwriting the value.

After the while-loop you need to add logic for calculating the tax.

Since this is clearly homework I will not initially supply the actual code. Try it yourself first!

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • Thank you for your response, I am a little shaky on where to add the logic for calculating the tax, etc. do I add it below the "break" after case 5, just before the return statement? – user3247128 Apr 13 '16 at 07:45
  • 1
    @user3247128: Yes, the tax calculation logically goes after the switch and before the end of loop. You've calculated the gross pay in the switch. You can now calculate the tax and apply the limit on the amount in a couple more statements, and then (presumably) print the details. – Jonathan Leffler Apr 13 '16 at 07:51
  • @user3247128 Yes, either there, or you can move the `return 0;` to the end of main and add the tax calculation after the `while`-loop. – Klas Lindbäck Apr 13 '16 at 07:58