New to C++ and I need some help. (Not new to languages)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: New to C++ and I need some help. (Not new to languages)

  1. #1
    Join Date
    Mar 2014
    Posts
    4

    New to C++ and I need some help. (Not new to languages)

    I have this homework, and yes, I've read the homework forum post about not having people here do it for me. I'm 100% okay with that because I'm a Computer Science major and just need to ask a few questions (I'm taking my Intro to Computer Programming Concepts online [seems to be a mistake])

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    int main () {
        //Declare Variables
        double hours, hourlyRate, grossPay, overTime, overPay; 
    
        //Enter hours & hours worked
        printf("Enter the amount of hours you worked here: ");
        scanf("%lf", &hours); 
        printf("Your total hours worked %.2lf\n", hours);
        printf("Enter your hourly rate here: ");
        scanf("%lf", &hourlyRate);
        printf("Your hourly rate is %.2lf\n", hourlyRate);
    
        //Calculations
        if (hours > 40) {
            overTime = (hours - 40);
            overPay = (hourlyRate * 1.5 * (overTime));
            grossPay = (40 * hourlyRate) + overPay;
        } else {
            grossPay = hours * hourlyRate;
        }
        
        
        //Print out
        printf("Your gross pay is: %.2lf\n", grossPay);
    system("pause");
    return 0;
    
    }
    In this code I wrote, no matter if I put 30 hours, or 45 hours, I get overtime for everything.
    So, I took it in a different direction, and wrote this!

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main () {
        //Declare Variables
        double hours, hourlyRate, grossPay, overTime, overPay; 
    
        //Enter hours & hours worked
        printf("Enter the amount of hours you worked here: \n");
        scanf("%lf", &hours); 
        printf("You have worked %.2lf\n", hours);
        printf("Enter your hourly rate here: \n");
        scanf("%lf", &hourlyRate);
        printf("Your hourly rate is %.2lf\n", hourlyRate);
    
        //Calculate gross pay
        if (hours <= 40);
            grossPay = hours * hourlyRate;
        if (hours > 40); 
            grossPay = (hourlyRate * 1.5) * hours;
        
        //Output gross pay
        printf("Your gross pay is: %.2lf\n", grossPay);
    
        system("pause");
    return 0;
    }
    But within this code I don't get any over time no matter if it's 45 or 30 hours. -__-

    If you want to know what it's supposed to do, then it's supposed to show the pay of a particular employee if he/she makes x amount of dollars during a period of time, where overtime happens after 40 hours and anything below 40 hours is just normal pay.

    In his exact words:
    "Develop an algorithm that will determine the gross pay of a particular employee. You need to read the hourly rate, and the amount of hours worked by the employee. You should display the hours, hourly rate, and gross pay of the employee. If the hours worked exceed 40 compute the overtime will be paid 1.5 *hourly rate. This program needs to have a selection statement that determines if the hours are overtime or not.

    Test your program by entering an amount of hours that are over 40 and under 40."

    I really appreciate any and all help anybody can give me. Thanks way ahead of time.

  2. #2
    Join Date
    Jul 2013
    Posts
    288

    Re: New to C++ and I need some help. (Not new to languages)

    if (hours <= 40);

    The semicolon shouldn't be there.

  3. #3
    Join Date
    Mar 2014
    Posts
    4

    Re: New to C++ and I need some help. (Not new to languages)

    Quote Originally Posted by razzle View Post
    if (hours <= 40);

    The semicolon shouldn't be there.
    Of course, it's a habit of mine (guess I'm going to pay attention to where I put them now) to put those after every line. After I took out that one and the one from the other if statement it works perfectly. Thank you very much!

  4. #4
    Join Date
    Jul 2013
    Posts
    288

    Re: New to C++ and I need some help. (Not new to languages)

    To avoid problems like this I always make a "one liner" out of simple if statements (lacking {}) like,

    if (hours <= 40) grossPay = hours * hourlyRate;

    Also note that 40 is an int whereas 40.0 is a double. Again to avoid problems (especially with unintended integer division), it's better to match the types (and use 40.0 here since hours is a double).

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,463

    Re: New to C++ and I need some help. (Not new to languages)

    Going back to your original program, it compiles and runs fine for me under MSVC 2012.

    Note that in your two programs you are using two different formulas for the calculation of gross pay when overtime is involved which will produce different results.

    The calculation from the first program can be simplified somewhat to
    Code:
    const double norm = 40.0;
    grossPay = (hours > norm) ? hourlyRate * (hours * 1.5 - (norm / 2.0)) : hours * hourlyRate;
    Last edited by 2kaud; March 27th, 2014 at 05:40 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,108

    Re: New to C++ and I need some help. (Not new to languages)

    Quote Originally Posted by razzle View Post
    To avoid problems like this I always make a "one liner" out of simple if statements (lacking {}) like,

    if (hours <= 40) grossPay = hours * hourlyRate;

    Also note that 40 is an int whereas 40.0 is a double. Again to avoid problems (especially with unintended integer division), it's better to match the types (and use 40.0 here since hours is a double).
    Strongly disagree. One statement per line. Code like that is hard to read, as it's easy to miss statements, and it's harder to debug as you can't tell if the condition is true just be stepping in the debugger. I would advise never combine multiple statements into one line.

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,463

    Re: New to C++ and I need some help. (Not new to languages)

    Quote Originally Posted by GCDEF View Post
    One statement per line. Code like that is hard to read, as it's easy to miss statements, and it's harder to debug as you can't tell if the condition is true just be stepping in the debugger. I would advise never combine multiple statements into one line.
    Agree
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    Join Date
    Mar 2014
    Posts
    4

    Re: New to C++ and I need some help. (Not new to languages)

    Again, I appreciate all of the help, I read through all of your replies and I have to say that I'd agree on keeping one statement to each line. I was in web design for three years and it's habit now to separate and organize everything, from indention(s) to white space.

    Also! Thanks for the protip (2kaud) on the simplifying my program, and telling me that they were different calculations. I meant for them to be different, but I was trying to use different math to produce the same result (I can't really even explain why this would work, but I figured I would give it a try). All-in-all the original ended up working perfectly after the removal of the incorrectly placed semicolons.

  9. #9
    Join Date
    Jul 2013
    Posts
    288

    Re: New to C++ and I need some help. (Not new to languages)

    Quote Originally Posted by GCDEF View Post
    Strongly disagree. One statement per line.
    Then I'm sure this is what your for-loops look like,

    Code:
    for (
       int i=0; 
       i<N;
       ++i)
    If not why not? It's what your rule implies. Or is it in fact your personal habits that are the rule?

    It's much safer to put a simple substatement on the same line as the head statement like,

    if (condition) simple-substatement;

    or

    while (condition) simple-substatement;

    It's either that or using braces. No orphan substatements. It's one of the few style rules that have safety implications. It removes a whole class of potential errors (including the one the OP couldn't find without help). Most coding standards would enforce the use of braces but what I suggest is a good alternative in a less formal setting.
    Last edited by razzle; March 29th, 2014 at 06:44 AM.

  10. #10
    Join Date
    Jul 2013
    Posts
    288

    Re: New to C++ and I need some help. (Not new to languages)

    Quote Originally Posted by Tonyp32810 View Post
    I have to say that I'd agree on keeping one statement to each line.
    Well, you came here with a bug you couldn't find. My suggestion will minimize the risk for it to happen again. Hopefully you're not an old dog already who cannot learn new tricks.

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,108

    Re: New to C++ and I need some help. (Not new to languages)

    Quote Originally Posted by razzle View Post
    Then I'm sure this is what your for-loops look like,

    Code:
    for (
       int i=0; 
       i<N;
       ++i)
    If not why not? It's what your rule implies. Or is it in fact your personal habits that are the rule?

    It's much safer to put a simple substatement on the same line as the head statement like,

    if (condition) simple-substatement;

    or

    while (condition) simple-substatement;

    It's either that or using braces. No orphan substatements. It's one of the few style rules that have safety implications. It removes a whole class of potential errors (including the one the OP couldn't find without help).
    That's a really goofy post. In the case of the for, the for, and its conditions, is the single statement, just as the if followed by an expression is a single statement.

    if (condition) simple-substatement; is not easier. Step to that statement in the debugger. Did the simple-statement get executed or not? You can't tell. It's also harder to miss multiple statements in a single line when reading code. Had the OP used the debugger, it would have been easy to spot that the statement following the if was always executed, which should have quickly led to the discovery of the ; Sure, on one line he may not have written the extra ;, but that's not a benefit that outweighs the drawbacks.

  12. #12
    Join Date
    Jul 2013
    Posts
    288

    Re: New to C++ and I need some help. (Not new to languages)

    Quote Originally Posted by GCDEF View Post
    That's a really goofy post.
    It demonstrates that you're not following your own rule.

    And even worse you don't seem to be clear about what's a statement in C++.

    You're trying to elevate your own habits to a general rule and I'm not buying that.

    Step to that statement in the debugger.
    If your debugger cannot handle ordinary C++ then get a better one. I would never use development software that forces me to write crappy code.
    Last edited by razzle; March 29th, 2014 at 07:08 PM.

  13. #13
    Join Date
    Mar 2014
    Posts
    4

    Re: New to C++ and I need some help. (Not new to languages)

    Quote Originally Posted by razzle View Post
    Well, you came here with a bug you couldn't find. My suggestion will minimize the risk for it to happen again. Hopefully you're not an old dog already who cannot learn new tricks.

    No, of course I'm open to new ideas. Especially as a student in something completely new to me (well, this is new to me, anyway). And you're right, I did come here with a bug I was unable to identify. Sorry if what I said seemed sorta rude. It wasn't intended to come across that way.

  14. #14
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,463

    Re: New to C++ and I need some help. (Not new to languages)

    Sorry if what I said seemed sorta rude. It wasn't intended to come across that way.
    You didn't.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  15. #15
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,990

    Re: New to C++ and I need some help. (Not new to languages)

    Quote Originally Posted by razzle View Post
    My suggestion will minimize the risk for it to happen again.
    Sorry for intervention, but this your suggestion is nothing more than urging to develop a particular habit. It's no better than other habit of never putting semicolon right after condition part. Or say, adopting some coding style, including indent style, a well known one preferably, and following it unexceptionally.
    Best regards,
    Igor

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center