12,700,376 members (35,284 online)
Rate this:
See more:
```#include <stdio.h>
void main (void)
{
float no_week =0.0;
int no_hour;
float hour_rate;
int no_item;
int salary_perweek;
float paycode_1;
float paycode_2;
float paycode_21;
float paycode_22;
float paycode_3;
float paycode_4;
float salarypaycode_1;
float salarypaycode_21 = 0.0;
float salarypaycode_22 = 0.0;
salary_perweek = 500;
hour_rate = 2.5;
printf("Enter Employee pay code: ");
scanf("%i", &paycode_1);
printf("Enter Number of Week: ");
scanf("%g", &no_week);
salarypaycode_1 = no_week * salary_perweek;
printf("Manager's Salary is \$%g\n", salarypaycode_1);
printf("Enter Employee pay code: ");
scanf("%i", &paycode_2);
printf("Enter Number of Hour: ");
scanf("%g", &no_hour);
while (no_hour >=0)
{
if (no_hour<=40)
salarypaycode_21 = no_hour * hour_rate;
{   printf("Hourly Workers Salary is %g\n", salarypaycode_21);
}
else
if (no_hour>40)
salarypaycode_22 = no_hour * hour_rate;
{   printf("Hourly Workers Salary is %g\n", salarypaycode_22);
}
}

}```

My coding is find the salary pay for the employee. So the variable dont bother it 1st because i haven finished my coding. It just part on it, and i stuck on this part.The formula cant run in the if....else and cant get the result. May i know where is the error? i cant figure it out.
Posted 16-Mar-11 20:36pm
Jayfam469
SAKryukov 17-Mar-11 2:09am

Pretty bad, pretty bad. You really need to fix your coding on a very elementary level (I understand, you're probably a beginner). Please see my Answer for some...
--SA

Rate this:

## Solution 1

You have misplaced the braces around your if else statement. I think this would work better.

```if (no_hour<=40)
{
salarypaycode_21 = no_hour * hour_rate;
printf("Hourly Workers Salary is %g\n", salarypaycode_21);
}
else ...```
v4
SAKryukov 17-Mar-11 2:08am

This isn't really big sin, easy to fix. See my Answer for more important failure.
--SA
Tarun.K.S 17-Mar-11 2:46am

That's correct, you missed one more point given by SA below. 5d!
Rate this:

## Solution 2

Bracket is a small thing, easy to find; Visual Studio will show the matching brackets (did you see it)…

But look at what are you doing!

```if (no_hour<=40)
{
}
else
if (no_hour>40) //this line is redundant! When code runs at this point it's always true!
{
}
```

Why using two conditions if one is the opposite to another. And this is not a small mistake, this is wrong way of thinking. Now, imagine you replace 40 with 42 (believe me, no constant stay a constant for long). You can easily change it only in one case and break your code. So, no immediate constants, ever! (The literal 40 in your code is called immediate constant, it goes right into the code after compilation and JIT.)

Keep all constants in the same place declared as constants explicitly.
In other cases, they should not be constants but variables, objects in resources or some configuration files.
Never assume any constant to be of the same value forever.

[EDIT]
The loop at the end is never finished. How can it? If `(no_hour >=0)` the code will get into the loop, because this is a loop condition, it remains in the loop forever, because condition remains true, because `no_hour` value is never modified thereafter.

—SA
v3
Tarun.K.S 17-Mar-11 2:47am

Right, second if is not necessary. 5d!
SAKryukov 17-Mar-11 2:49am

Who would argue..? :-)
Thank you, Tarun.
--SA
Jayfam 17-Mar-11 4:01am

can show me some example? im still not understand with the constants u mean above
SAKryukov 17-Mar-11 4:10am

The example is your code. Look at the literal "40", it is called "immediate constant". It is not supportable. Let's go with constants. You should have some place where you explicitly declare "const int Step = 40", or whatever it its, give it a name, which is itself a help to readability. Called D.R.Y. -- "Don't repeat yourself". Every time you repeat a small thing you create a source of inconsistency.
--SA

--SA
Jayfam 17-Mar-11 4:18am

I understand what u mean ady, set the 40 into constant. But why the output still unstoppable showing out?

#include <stdio.h>

void main (void)
{
int no_item;
int salary_perweek;
float no_week =0.0;
float hour_rate;
float paycode_1;
float paycode_2;
float paycode_21;
float paycode_22;
float paycode_3;
float paycode_4;
float salarypaycode_1;
float salarypaycode_21 = 0.0;
float salarypaycode_22 = 0.0;
const int no_hour = 40;

salary_perweek = 500;
hour_rate = 2.5;

printf("Enter Employee pay code: ");
scanf("%i", &paycode_1);
printf("Enter Number of Week: ");
scanf("%g", &no_week);
salarypaycode_1 = no_week * salary_perweek;
printf("Manager's Salary is \$%g\n", salarypaycode_1);

printf("Enter Employee pay code: ");
scanf("%i", &paycode_2);
printf("Enter Number of Hour: ");
scanf("%g", &no_hour);
while (no_hour >=0)
{
if (no_hour<=40)
{
salarypaycode_21 = no_hour * hour_rate;
printf("Hourly Workers Salary is %g\n", salarypaycode_21);
}
else
if (no_hour>40)
{
salarypaycode_22 = no_hour * hour_rate;
printf("Hourly Workers Salary is %g\n", salarypaycode_22);
}
}

}
SAKryukov 17-Mar-11 4:29am

I answered by updating the Answer. Frankly, I could not even ever imagine that it can be a question...
Please, edit the above comment above and remove the code. You repeat yourself...

--SA
Espen Harlinn 17-Mar-11 15:36pm

Nice effort - 5ed!
SAKryukov 17-Mar-11 15:56pm

Thank you, Espen.
--SA
Rate this:

## Solution 3

Bold lines are the problems:
```while (no_hour >=0)// an infinite loop
{
if (no_hour<=40)
salarypaycode_21 = no_hour * hour_rate; //if ends here, so else is an error
{   printf("Hourly Workers Salary is %g\n", salarypaycode_21);
}
else
if (no_hour>40)
salarypaycode_22 = no_hour * hour_rate;
{   printf("Hourly Workers Salary is %g\n", salarypaycode_22);
}
}```

Solution:
```//while (no_hour >=0) //not needed
//  {
if (no_hour<=40)
{// here
salarypaycode_21 = no_hour * hour_rate;
//{   // not here
printf("Hourly Workers Salary is %g\n", salarypaycode_21);
}
else
//if (no_hour>40) not needed
{ //here
salarypaycode_22 = no_hour * hour_rate;
//{   not here
printf("Hourly Workers Salary is %g\n", salarypaycode_22);
}
//    }end while
```

This is not the exact solution. It solves only structural problem. you have some semantic/logical error which depend on your need.
v2
Jayfam 17-Mar-11 6:45am

may i know why //while (no_hour >=0) // no needed?
Аslam Iqbal 17-Mar-11 7:15am

because its a infinite loop and it does nothing. If you add this inside while{}: scanf("%g", &no_hour); then it is useful. You used many more variables all of those are not in use. maybe you can use those later.
Jayfam 17-Mar-11 7:21am

ya, the variable i will use it later. The coding now got the logical error, is it the formula and the variable got problem?
Аslam Iqbal 17-Mar-11 7:33am

It depends on you and your need.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 ppolymorphe 125 OriginalGriff 120 Dave Kreskowiak 120 Peter Leow 55 Homero Rivera 55
 OriginalGriff 3,936 Peter Leow 2,323 ppolymorphe 1,882 Mika Wendelius 1,753 Jochen Arndt 1,479

Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 17 Mar 2011