Question From"While" Help

This is a discussion on Question From"While" Help within the C Programming forums, part of the General Programming Boards category; Freinds after a long thinking , I m not able to start with this question. Even i do not know ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    83

    Thumbs up Question From"While" Help

    Freinds after a long thinking , I m not able to start with this question. Even i do not know how take off with question? what to put in integer or float????? I can solve it using if else but not getting how can i solve it using while.Sometimes i feel the author has missed somthing to mention or its is twisted or something else which side i m not able to think well.

    Plese guide me. But the condition is :- Use " While Statement Only "

    Here is the question.

    Quesion:- Write a Pgm to calculate overtime pay of 10 employees.Over time is paid at the Rate of Rs.12.00 Per Hour for every hour worked above 40 hours. Assusme that employees do not work for fractional part of an hour?

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    83
    Will You Believe This???? I have just wrote this much pgm for this

    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <ctype.h>
    void main()
    {
    int

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    have u ever heard of flow charts..or pseudocode???Try making a rough flowchart before starting with the code...It helps when you are new to it..and having such kind of practice will also help when u want to write big codes..

    have written a pseudo code ,now try developing it in c
    input employee no::
    while(employeeno<=10)
    Input no of hours
    if hours-40>0:calculate overtime(12*(40-hours))
    else no overtime for this employee
    next employee
    }

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    you don't need to include so many libraries for this function...

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    void main is undefined; use int main.
    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.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    83
    Quote Originally Posted by edesign View Post
    have u ever heard of flow charts..or pseudocode???Try making a rough flowchart before starting with the code...It helps when you are new to it..and having such kind of practice will also help when u want to write big codes..

    have written a pseudo code ,now try developing it in c
    input employee no::
    while(employeeno<=10)
    Input no of hours
    if hours-40>0:calculate overtime(12*(40-hours))
    else no overtime for this employee
    next employee
    }

    I feel it will help me alot.Now i can see a path to walk over.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    83
    [QUOTE=Elysia;718256]void main is undefined; use int main.[/QUOTI]
    Ok i will do that.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by edesign View Post
    have u ever heard of flow charts..or pseudocode???Try making a rough flowchart before starting with the code...It helps when you are new to it..and having such kind of practice will also help when u want to write big codes..

    have written a pseudo code ,now try developing it in c
    input employee no::
    while(employeeno<=10)
    Input no of hours
    if hours-40>0:calculate overtime(12*(40-hours))
    else no overtime for this employee
    next employee
    }
    OK, there's a start.

    Now imagine you're the paymaster at the small company of ten employee's, and you are figuring this out, by hand.

    Since each employee will have an employee number > 0, I would use that as a test condition for your first while loop:

    Code:
    employee_no = 1         /* otherwise the while loop is entirely skipped, perhaps */
    while(employee_no > 0)  
    
       set values:
       ot = 0, work hours = 0, regular pay = 0, ot_pay = 0, full pay = 0, employee = 0
    
       print a prompt for what to enter, and then input their employee number
    
       if(employee number == 0)
          then continue
    
       Without the continue (or break), you'll be asked to fill in all the data for the employee with  
       the zero employee number, which you don't want. The "continue" just moves control to 
       the very bottom of the loop, which will then bring control right to the test at the top of the
       while loop, which employee number 0 will fail, and then break out of the loop, completely.
    
       print a prompt for no. of hours, and input that integer since no fractions of hours allowed.
       
       calculate overtime hours: 
       if work hours > 40
            then ot = work hours - 40
    
       calculate pay:
       regular pay = 8 * work hours    
       if(ot > 0)
          ot_pay = 12 * OT  (12 * each hour of overtime)
    
       full pay = regular pay + ot_pay
    
       print employee no., regular pay, ot_pay, and full pay
       
    
    And loop back up.
    If you break the pay down, into small parts (regular, ot_pay, full pay), then it's just easier
    to make up the logic for it, and it's easier to troubleshoot it when there's a problem.

    <Apologies, I got into the logic, and didn't notice the "the overtime" part of the problem.>


    All those sets at the top of the loop may seem unneeded, but variables like that in a loop, can give you the craziest errors.

    Code this up, and step through it several times. See if I've made any errors!
    Change it around a bit, as you like. See if that new version's logic is good or not.

    Most of us learn best by doing, not by just reading about it.
    Last edited by Adak; 02-15-2008 at 07:50 AM.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    Code:
       if(employee number == 0)
          then continue
    Is that really the best? Essentially, what you're doing is breaking the loop if employee_number == 0. Then you should either use break or use an if to execute the rest of the loop if employee_number != 0.
    Using continue to break a loop may just be confusing.
    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
    Apr 2006
    Posts
    83
    I tried this pgm but it goes on...Infinite loop !!!!!! and never gets to result..

    M I going right way?

    pLesae have look at the pgm and advice .....

    Code:
    #include <stdlib.h>
    #include <conio.h>
    #include <ctype.h>
    int main()
    {
    clrscr();
    int e,h,ovp;
         printf("Enter the Emplyee");
         scanf("&#37;d",&e);
    
         while(e<=10)
         {
         printf("Enter The Value");
         scanf("%d%",&h);
         }
    	if(h-40>0)
    	{
    	  ovp=12*(40-h);
    	  printf("%d",ovp);
    	}
    	else
    	  printf("NO Overtime Done by this employ");
    	e=e+1;    /* I REMOVED IT ALSO BUT...NOTHING CHANGED
    getch();
    }

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    First up, fix your indentation a bit.
    As you'll see, the condition for the while is using the variable e, but you're only reading into variable h, so the value of e never changes and the loop is infinite.
    And use better names for the variables rather than e, h and ovp.

    And btw, it's better to use getchar() instead of getch().
    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.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Using the "continue" highlights the test condition for the while loop, in this case. Using a "break" highlights using a break, instead.

    If that's your biggest concern or gripe for the day, you're in good shape!

    =========================

    Code:
         while(e<=10)
         {
         printf("Enter The Value");
         scanf("%d%",&h);
         }
    Your while loop has a loop test on the variable "e", but "e" never changes, so the loop goes on forever, once it starts.

    Also, you probably want this while loop to include the code for handling every overtime pay amount, for every employee. As it is now, it's too small to do that.

    Look over my pseudo code, and study the difference in the size of the while loops.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    Quote Originally Posted by Adak View Post
    Using the "continue" highlights the test condition for the while loop, in this case. Using a "break" highlights using a break, instead.
    But in this case, you want to break the loop, so it's better to write it explicitly rather than using a continue.
    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.

  14. #14
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by RahulDhanpat View Post
    I tried this pgm but it goes on...Infinite loop !!!!!! and never gets to result..

    M I going right way?

    pLesae have look at the pgm and advice .....
    First: A Loop is the reason humans made computers. Avoiding to do by hand things that need to be done many many times.
    Second: The loops in C are repeating the statement or statements they refer to. If you can see, your programs loop, which is the while operates on two things.
    Code:
    while(e<=10)
    {          /* <---- The { notes that the 'while' will do again and again everything up to } */
         printf("Enter The Value");
         scanf("%d%",&h);
    }    /* <--- This means the end of the things 'while' will repeat */
    But you need it to work on the input, and end sometime. Since in this snippet the variable 'e' doesn't change, it will hang forever. To solve your problem, you need to move between the '{' and the '}' i have marked all the statements that need to be repeated. That of course includes the calculation of the wage. I hope it is understandable. You must also care NOT to move inside a 'while' loop anything unnecessary or unfit to go there. Try also to understand the meaning of a 'block'. A block is anything between '{' '}', and we are all used to see code withing blocks get a little pushed ahead with spaces or tabs so that it sticks out and makes blocks easier to read. That's called indentation.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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