Like Tree3Likes

Payment program

This is a discussion on Payment program within the C++ Programming forums, part of the General Programming Boards category; Here's the deal: -Payment rate is 2.55 per hour -Night hours get paid 20% bonus -Worked holidays get paid 75% ...

  1. #1
    Registered User
    Join Date
    Oct 2013
    Location
    Hellas
    Posts
    70

    Payment program

    Here's the deal:
    -Payment rate is 2.55 per hour
    -Night hours get paid 20% bonus
    -Worked holidays get paid 75% bonus
    Overtime is being paid: for the first 20 hours you get 20% bonus and from the 21 overtime hour and over, you get paid 25% bonus.
    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    int main()
    {
        int pass=123; int x; int overtime; int salary;
        int hours; int hours_day; int night_hours; int holiday_hours;
        cout<<"Program for payment (age under 25)\n";
        Loop:
        cout<<"Give password\n";
        cin>>x;
        if (x==pass){
            cout<<"Give the hours you worked all month\n";
            cin>>hours;
            if (hours>176){
                overtime=hours-176;
                if (overtime<20)
                   overtime=((overtime*2.55)*0.20);
                else if (overtime>20)
                    overtime=(((overtime-20)*2.55)*0.25)+((20*2.55)*0.20);
                        }
            hours_day=(hours*2.55);
            cout<<"Give how many night hours you worked\n";
            cin>>night_hours;
            cout<<"How many holiday hours you worked?\n";
            cin>>holiday_hours;
            salary=(hours_day+((night_hours*2.55)*0.25)+((holiday_hours*2.55)*0.75)+overtime);
            cout<<"They must pay you "<<salary<<" euros"<<endl;
    
        }
        else{
            cout<<"Wrong pass, try again \n";
            goto Loop;}
    getch();
    return 0;
    }
    The problem is that it doesn't work as it should. I get wrong result no matter what I input..
    Last edited by Innos; 10-25-2013 at 12:30 PM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by Innos View Post
    The problem is that it doesn't work as it should. I get wrong result no matter what I input..
    Not good enough for problem description

    Show input values
    Show desired result based on which formulas it should be received
    Show program output (best copy past from Command window)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Location
    Hellas
    Posts
    70
    Put it in a compiler and see how it works man.. Omg, maybe you would like some tea too sir?

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    I think the overtime formula is wrong. You said that workers are paid 20% more for the first 20 hours of overtime. There are two potential calculations for that, depending on how long they worked. overtime * 2.55 * 1.20 for less than 21 hours, 20 * 2.55 * 1.20 + (overtime - 20) * 2.55 * 1.25 for 21 or more.

    I'm not sure if overtime and night hours are actually different things. It doesn't sound like it is actually different though. The distinction you make in your program might be pointless.

    On holidays, you must pay 1.75 times the rate, because it's the regular rate plus 75 percent. You should find out how many hours are holiday hours before you actually do other calculations.
    Last edited by whiteflags; 10-25-2013 at 01:58 PM.

  5. #5
    Registered User
    Join Date
    Oct 2013
    Location
    Hellas
    Posts
    70
    The worker works 22 days on each month, so 22*8=176 . Everything that is over 176 is considered overtime. For example, the user gives 186 hours so 10 hours are overtime. My calculations are the same as yours.
    Night hours are the hours that he had worked at night shift and it is paid with a 20% bonus up to his hour. The night hours are given by the user too
    The hours for holiday hours are given by the user

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you put in, say, zero hours worked, what value does the variable "overtime" have in your code?

    (Also: should all these values be int? What about all the hundredths we've got running around?)

  7. #7
    Registered User
    Join Date
    Oct 2013
    Location
    Hellas
    Posts
    70
    If I put on all couts zero I get 1977749858 as a result... and if I put 1 on all couts I get 1977749687..

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Innos View Post
    If I put on all couts zero I get 1977749858 as a result... and if I put 1 on all couts I get 1977749687..
    I wasn't asking because I cared about the answer; I asked so that you would look at your code and trace through it, which would allow you to spot an error.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Quote Originally Posted by Innos View Post
    Put it in a compiler and see how it works man.. Omg, maybe you would like some tea too sir?
    People are lazy. Remember: you are the one asking questions

    That said, there are more issues than just a non-working program. You shouldn't use goto. At all. Learn to use loops.
    Also, you should focus on preventing erroneous conditions from propagating, instead of trying to use if statements to proceed if the input is correct. Let me show you an example:

    Instead of:
    Code:
    std::string Pass;
    std::getline(std::cin, Pass);
    if (Pass == CorrectPass)
    	// Do stuff
    else
    	std::cout << "Incorrect password. Try again.\n";
    Do:
    Code:
    std::string Pass;
    std::getline(std::cin, Pass);
    while (Pass != CorrectPass)
    {
    	std::cout << "Incorrect password. Try again.\n";
    	std::getline(std::cin, Pass);
    }
    
    // Do stuff here because we know password is correct.
    This avoids nesting tons of if statements, making for readable code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Oct 2013
    Location
    Hellas
    Posts
    70
    All these are nice but the solution?? What is wrong with the program? Calculations are correct so why it doesn't give a correct answer?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,454
    Quote Originally Posted by Innos
    All these are nice but the solution?? What is wrong with the program? Calculations are correct so why it doesn't give a correct answer?
    There was an interesting observation in post #6:
    Quote Originally Posted by tabstop
    Also: should all these values be int? What about all the hundredths we've got running around?
    I suggest that you incorporate the advice given in this thread, particularly keeping in mind that you are not only dealing with integers here.

    If you still find that the results are not what you expected, post your updated program along with your input, expected output and actual output. No, "put it in a compiler and see how it works" is a poor demand coming from you: you observed something that tells you that your program doesn't work, so you should state that clearly. Otherwise, we can just say "no, it works", and go on to help someone else who is more forthcoming.
    Last edited by laserlight; 10-26-2013 at 11:35 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Oct 2013
    Location
    Hellas
    Posts
    70
    Alright, from int I made it into double. Here is the input:
    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    int main()
    {
        int pass=123; int x; double overtime; double salary;
        int hours; double hours_day; double night_hours; double holiday_hours;
        cout<<"Program for payment (age under 25)\n";
        Loop:
        cout<<"Give password\n";
        cin>>x;
        if (x==pass){
            cout<<"Give the hours you worked all month\n";
            cin>>hours;//give 10
            if (hours>176){
                overtime=hours-176;
                if (overtime<20)
                   overtime=((overtime*2.55)*0.20);
                else if (overtime>20)
                    overtime=(((overtime-20)*2.55)*0.25)+((20*2.55)*0.20);
                        }
            hours_day=(hours*2.55);
            cout<<"Give how many night hours you worked\n";
            cin>>night_hours;//give 0
            cout<<"How many holiday hours you worked?\n";
            cin>>holiday_hours;//give 0
            salary=(hours_day+((night_hours*2.55)*0.25)+((holiday_hours*2.55)*0.75)+overtime);
                    cout<<"They must pay you "<<salary<<" euros"<<endl;
    
        }
        else{
            cout<<"Wrong pass, try again \n";
            goto Loop;}
    getch();
    return 0;
    }
    The result I should have would be 10*2.55=25.5 . Instead I get 2.47137e+255 euros
    Using double I made a progress and now it gives me a decimal number. Before I was only getting integers..

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Innos View Post
    Calculations are correct so why it doesn't give a correct answer?
    That's an assertion that can be easily disproven by looking at the code. I have pointed out that your calculation of overtime is in fact incorrect -- specifically that you set it to a random amount when hours is less than 176 instead of the amount it is supposed to be. If you don't actually do the correct calculations you will not get the correct answer.
    SirPrattlepod likes this.

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    before running program - fix warnings (and don't expect me to compile for you every time you make a small fix - this is one time offer since I'm really tired to see the same unfixed code again and again)
    Code:
    #include <iostream>
    
    using namespace std;
    int main()
    {
        int pass=123; int x; double overtime; double salary;
        int hours; double hours_day; double night_hours; double holiday_hours;
        cout<<"Program for payment (age under 25)\n";
    Loop:
        cout<<"Give password\n";
        cin>>x;
        if (x==pass){
            cout<<"Give the hours you worked all month\n";
            cin>>hours;//give 10
            if (hours>176){
                overtime=hours-176;
                if (overtime<20)
                    overtime=((overtime*2.55)*0.20);
                else if (overtime>20)
                    overtime=(((overtime-20)*2.55)*0.25)+((20*2.55)*0.20);
            }
            hours_day=(hours*2.55);
            cout<<"Give how many night hours you worked\n";
            cin>>night_hours;//give 0
            cout<<"How many holiday hours you worked?\n";
            cin>>holiday_hours;//give 0
            salary=(hours_day+((night_hours*2.55)*0.25)+((holiday_hours*2.55)*0.75)+overtime);
            cout<<"They must pay you "<<salary<<" euros"<<endl;
    
        }
        else{
            cout<<"Wrong pass, try again \n";
            goto Loop;
        }
        return 0;
    }
    Code:
    g++ -c -o ../obj/test.o test.cpp -Wall -pedantic -std=c++0x -march=core2 -O2 -I../include
    test.cpp: In function ‘int main()’:
    test.cpp:27:90: warning: ‘overtime’ may be used uninitialized in this function [-Wuninitialized]
    g++ -o test ../obj/test.o -Wall -pedantic -std=c++0x -march=core2 -O2 -I../include -lm
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    Registered User
    Join Date
    Oct 2013
    Location
    Hellas
    Posts
    70
    Quote Originally Posted by tabstop View Post
    That's an assertion that can be easily disproven by looking at the code. I have pointed out that your calculation of overtime is in fact incorrect -- specifically that you set it to a random amount when hours is less than 176 instead of the amount it is supposed to be. If you don't actually do the correct calculations you will not get the correct answer.
    God damn! Such a simple solution and I didn't even notice! I stand corrected my friend!!
    Here is the working code:
    Code:
    #include <iostream>
    
    using namespace std;
    int main()
    {
        int pass=123; int x; double overtime=0.0; double salary;
        int hours; double hours_day; double night_hours; double holiday_hours;
        cout<<"Program for payment (age under 25)\n";
        Loop:
        cout<<"Give password\n";
        cin>>x;
        if (x==pass){
            cout<<"Give the hours you worked all month\n";
            cin>>hours;
            if (hours>176){
                overtime=hours-176;
                if (overtime<20.0)
                   overtime=((overtime*2.55)*0.20);
                else if (overtime>20.0)
                    overtime=(((overtime-20.0)*2.55)*0.25)+((20*2.55)*0.20);
                        }
            hours_day=(hours*2.55);
            cout<<"Give how many night hours you worked\n";
            cin>>night_hours;
            cout<<"How many holiday hours you worked?\n";
            cin>>holiday_hours;
            salary=(hours_day+((night_hours*2.55)*0.25)+((holiday_hours*2.55)*0.75)+overtime);
            cout<<"They must pay you "<<salary<<" euros"<<endl;
    
        }
        else{
            cout<<"Wrong pass, try again \n";
            goto Loop;}
    cin.get();
    return 0;
    }

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I do a gross payment program with overtime pay in C?
    By lquidplastic in forum C Programming
    Replies: 4
    Last Post: 03-11-2012, 11:36 AM
  2. Monthly Payment Program Help
    By sharingan_gv in forum C Programming
    Replies: 7
    Last Post: 07-14-2011, 09:07 AM
  3. calculate monthly payment
    By yacek in forum C Programming
    Replies: 2
    Last Post: 10-04-2010, 07:59 PM
  4. Bet Payment: Kermi
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-20-2003, 07:00 PM

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