Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
#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
Comments
SAKryukov at 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: bad
good
Please Sign up or sign in to vote.

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 ...
  Permalink  
v4
Comments
SAKryukov at 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 at 17-Mar-11 2:46am
   
That's correct, you missed one more point given by SA below. 5d!
Rate this: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
v3
Comments
Tarun.K.S at 17-Mar-11 2:47am
   
Right, second if is not necessary. 5d!
SAKryukov at 17-Mar-11 2:49am
   
Who would argue..? :-)
Thank you, Tarun.
--SA
Jayfam at 17-Mar-11 4:01am
   
can show me some example? im still not understand with the constants u mean above
SAKryukov at 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 at 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
 
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 at 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 at 17-Mar-11 15:36pm
   
Nice effort - 5ed!
SAKryukov at 17-Mar-11 15:56pm
   
Thank you, Espen.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

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.
  Permalink  
v2
Comments
Jayfam at 17-Mar-11 6:45am
   
may i know why //while (no_hour >=0) // no needed?
Аslam Iqbal at 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 at 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 at 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)



Advertise | Privacy | Mobile
Web04 | 2.8.150302.1 | Last Updated 17 Mar 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100