1. ## 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:
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..

2. Originally Posted by Innos
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)

3. Put it in a compiler and see how it works man.. Omg, maybe you would like some tea too sir?

4. 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.

5. 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. 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. 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. Originally Posted by Innos
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. Originally Posted by Innos
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:

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.

10. 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. 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:
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.

12. 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:
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. Originally Posted by Innos
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.

14. 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:
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```

15. Originally Posted by tabstop
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:
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;
}```