Trouble with the While Statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Villanmac
    New Member
    • Nov 2007
    • 12

    Trouble with the While Statement

    First up thanks to the guys who pointed out my error last week.

    Now I was wondering If I could get some help with this problem because again I have fallen into trouble. Basically im using the while statement to loop a program but dont know how to loop back to the request of Enter hours worked or (-1 to end) : 39. I cant put it in the while statement as it contains the condition that activates the while statement, if you see what I mean?

    Im trying to get a process like so

    Sample screen input/ouput

    Enter hours worked or (-1 to end) : 39
    Enter hourly rate of worker (£00.00) : 10.00
    Salary is £390.00

    Enter hours worked or (-1 to end) : 40
    Enter hourly rate of worker (£00.00) : 10.00
    Salary is £400.00

    Enter hours worked or (-1 to end) : 41
    Enter hourly rate of worker (£00.00) : 10.00
    Salary is £415.00

    Enter hours worked or (-1 to end) : -1

    But im getting this....

    Enter hours worked or (-1 to end) : 39
    Enter hourly rate of worker (£00.00) : 10.00
    Salary is £390.00

    Enter hourly rate of worker : 10.00
    Salary is 390.00 (it uses my original value of hours and i dont know how to allow the user to input a new value of hours worked for a new worker.

    Heres my program...



    [code=cpp]
    #include <iostream>
    #include <cmath>
    using std::cout;
    using std::cin;
    using std::endl;


    int main ()
    {
    double hours;
    double Pay_per_hour;
    double Total_Pay;

    cout << "Enter hours worked or -1 to end: ";
    cin >> hours;

    while (hours != -1)
    {
    cout << "Enter hourly rate of the worker ";
    cin >> Pay_per_hour;

    if (hours <= 40)
    Total_Pay = hours * Pay_per_hour;
    else
    Total_Pay = ( (hours - 40) * 15 ) + 400;

    cout << "Salary is " << Total_Pay << endl;

    }


    return 0;

    } // end main[/code]
    Last edited by sicarie; Nov 27 '07, 02:54 PM. Reason: Code tags (and excess use of bolding annoys the crap outta me)
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Your hours prompt and input need to be inside your while loop as well.

    Comment

    • mohammadazim
      New Member
      • Aug 2007
      • 28

      #3
      You can move your prompt to hours in the loop and use a break statement to come out of loop. Like this...

      Code:
      //cout << "Enter hours worked or -1 to end: ";
      //cin >> hours;
      // move the above two inside the loop.
       
      while (1) // make loop unconditional.
      {
         cout << "Enter hours worked or -1 to end: ";
         cin >> hours;
      //  ... and put a check for hours
         if (hours < 0)
            break;// break will take you out of loop.
         cout << "Enter hourly rate of the worker ";
         cin >> Pay_per_hour;
       
         if (hours <= 40)
            Total_Pay = hours * Pay_per_hour;
         else
            Total_Pay = ( (hours - 40) * 15 ) + 400;
       
         cout << "Salary is  " << Total_Pay << endl;
      
      }

      Comment

      • Villanmac
        New Member
        • Nov 2007
        • 12

        #4
        Originally posted by sicarie
        Your hours prompt and input need to be inside your while loop as well.
        Thanks, but the problem I then have is when I click -1, the program does not terminate but still asks me the hourly rate of the worker and then works out his/her wage. When the user clicks -1 I want the program to terminate straight away but I cant do it.

        Comment

        • Villanmac
          New Member
          • Nov 2007
          • 12

          #5
          Originally posted by mohammadazim
          You can move your prompt to hours in the loop and use a break statement to come out of loop. Like this...

          Code:
          //cout << "Enter hours worked or -1 to end: ";
          //cin >> hours;
          // move the above two inside the loop.
           
          while (1) // make loop unconditional.
          {
             cout << "Enter hours worked or -1 to end: ";
             cin >> hours;
          //  ... and put a check for hours
             if (hours < 0)
                break;// break will take you out of loop.
             cout << "Enter hourly rate of the worker ";
             cin >> Pay_per_hour;
           
             if (hours <= 40)
                Total_Pay = hours * Pay_per_hour;
             else
                Total_Pay = ( (hours - 40) * 15 ) + 400;
           
             cout << "Salary is  " << Total_Pay << endl;
          
          }
          Cheers mohammad, that was what I was struggling with. Thanks again

          I'd never come across the while(1) statement, so hadnt a chance

          Comment

          Working...