Thread: Need help with looping

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    11

    Need help with looping

    In the interest of full disclosure, this is a lab assignment that is due tomorrow. We are allowed to work with classmates in/out of class on it, so I am not cheating by asking for help. i am much older than most of my classmates, so we don't associate outside of class. (kinda creepy for a 40 year old dude hanging out with teenagers no matter what the reason if you ask me)

    I have to modify a program that I have previously written to include a loop. When I first wrote the program I was aggravated that it terminated after each execution even though looping was not a requirement. I researched how to get it to loop (started a thread about it on here for help). I wasn't aware at the time that I would have to make the same program loop later. I finally managed, with some help from my tutor, to figure out how to get it to loop by prompting the user to answer if they wanted to continue. This assignment is a bit different than what I had accomplished.

    We are required to have it loop until the years of service entered are <=0, or >=99. I am pretty sure that it requires a pre-test condition, but have tried both pre and post-test conditions while trying to get it to work. It will loop, but will not exit when any of the exit numbers are entered. if you can spot my mistake, please offer some help or a hint. It would be greatly appreciated. Thanks ahead of time for any help you may offer.
    Code:
    //Travis 
    //CPT-168-A01
    //2nd Program
    #include
    <iostream>
    using
    namespace std;
    int
     main()
    {
    	
    	system(
    "color f0");
    	cout<<
    "\t\t\t***********************************"<<endl;
    	cout<<
    "\t\t\t*          Travis                 *"<<endl;
    	cout<<
    "\t\t\t*         Second Program          *"<<endl;
    	cout<<
    "\t\t\t*           CPT-168-A01           *"<<endl;
    	cout<<
    "\t\t\t***********************************"<<endl;
    	
    		
    		
    int yrswrkd = 0;
    		
    double hrswrkd = 0.0, hrlyrat = 0.0,notpay = 0.0, otpay = 0.0, bnspay = 0.0, pay1 = 0.0, totpay = 0.0;
    		
    do
    		{
    		cout<<
    "Please Enter Years of Service(Less than 1 or Greater than 98 to Exit): ";
    		cin>>yrswrkd;
    		cout<<
    "Please Enter Hours Worked: ";
    		cin>>hrswrkd;
    		cout<<
    "Please Enter Hourly Pay Rate: ";
    		cin>>hrlyrat;
    		
    if (hrswrkd > 40)
    			notpay = hrlyrat * 40, otpay = (hrswrkd - 40) * 1.5 * hrlyrat, pay1 = otpay + notpay;
    		
    else
    			pay1 = hrswrkd * hrlyrat;
    		cout<<
    "Your Gross Pay Is: $"<<pay1<<endl;
    		
    if (yrswrkd >= 1 && yrswrkd <= 5)
    			bnspay = pay1 * .05;
    		
    else
    			
    if (yrswrkd >= 6 && yrswrkd <= 9)
    				bnspay = pay1 * .10;
    			
    else
    				
    if (yrswrkd == 10)
    					bnspay = pay1 * .15;
    				
    else
    					
    if (yrswrkd > 10)
    						bnspay = pay1 * .20;
    					
    //endif
    				
    //endif
    			
    //endif
    		
    //endif
    		cout<<
    "Your Employee Longevity Bonus is: $"<<bnspay<<endl;
    		totpay = pay1 + bnspay;
    		cout<<
    "Your Total Pay Is: $"<<totpay<<endl<<endl;
    		cout<<
    "Thank You For Your Service!"<<endl<<endl;
    		}
    while (yrswrkd >= 1 || yrswrkd <= 98);
    	
    		cout<<
    "HAVE A NICE DAY!!!"<<endl<<endl;
    		system(
    "pause");
    		
    return 0;
    }
     
     
    

  2. #2
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    Coincidentally, I used the "code" brackets. It came out better than the last code that I posted, but is still not correctly indented, etc. I apologize. I am working on it, though.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    while (yrswrkd >= 1 || yrswrkd <= 98);
    Speak this line of code out loud to yourself. Hint: It's always going to be true.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    Get Condition
    If Condition Met Loop
        Do This
        Get Condition 
    End Loop

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    Quote Originally Posted by Matticus View Post
    Code:
    while (yrswrkd >= 1 || yrswrkd <= 98);
    Speak this line of code out loud to yourself. Hint: It's always going to be true.
    In actual years of service it should be, but he wanted it to be set so that the user could enter 0, 99 or over, or a negative number to exit. Unless I am having a serious brain fart, the while condition i used sets the parameter between 1 and 99, doesn't it? I will read it a few more times. Sometimes it takes me 20 or so readings, lol. Thanks for the hint.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Just remember:

    Code:
    false OR false = false
    false OR true  = true
    true  OR false = true
    true  OR true  = true
    Now check the result of each of those evaluations for "yrswrkd" equal to: -5, 50, and 100

    (FYI - I didn't check the rest of your code, I just homed in on that one line - so I can't say whether anything else might be awry in your program.)

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    Quote Originally Posted by Matticus View Post
    Code:
    while (yrswrkd >= 1 || yrswrkd <= 98);
    Speak this line of code out loud to yourself. Hint: It's always going to be true.
    After further reading... I GET IT!!! It should be && to keep it between 1 and 99. THANK YOU!

  8. #8
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    Quote Originally Posted by Matticus View Post
    Just remember:

    Code:
    false OR false = false
    false OR true  = true
    true  OR false = true
    true  OR true  = true
    Now check the result of each of those evaluations for "yrswrkd" equal to: -5, 50, and 100

    (FYI - I didn't check the rest of your code, I just homed in on that one line - so I can't say whether anything else might be awry in your program.)
    That solves the never-ending loop problem. I now have to figure out how to get it to pre-test the condition, but still have a prompt inside the loop for years of service. Thanks again for pointing that out. I really have been going over this for about 4 hours and that never popped out at me.

  9. #9
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    Ok, I have it. Thank you guys so much for the help and suggestions. I know I have to work on making my code prettier, but I am hoping that will come with time.

    Code:
    //Travis Bryant
    //CPT-168-A01
    //Payroll Prog with Loop Based on Years of Service
    #include
    <iostream>
    #include
    <iomanip>
    using
    namespace std;
    int
     main()
    {
    	
    	system(
    "color f0");
    	cout<<
    "\t\t\t***********************************"<<endl;
    	cout<<
    "\t\t\t*          Travis Bryant          *"<<endl;
    	cout<<
    "\t\t\t*         Second Program          *"<<endl;
    	cout<<
    "\t\t\t*           CPT-168-A01           *"<<endl;
    	cout<<
    "\t\t\t***********************************"<<endl;
    	
    		
    		
    int yrswrkd = 0;
    		
    double hrswrkd = 0.0, hrlyrat = 0.0,notpay = 0.0, otpay = 0.0, bnspay = 0.0, pay1 = 0.0, totpay = 0.0;
    		cout<<
    "Please Enter Years of Service(Less than 1 or Greater than 98 to Exit): ";
    		cin>>yrswrkd;
    		
    while (yrswrkd >= 1 && yrswrkd <= 98) 
    		{
    		cout<<
    "Please Enter Hours Worked: ";
    		cin>>hrswrkd;
    		cout<<
    "Please Enter Hourly Pay Rate: ";
    		cin>>hrlyrat;
    		
    if (hrswrkd > 40)
    			notpay = hrlyrat * 40, otpay = (hrswrkd - 40) * 1.5 * hrlyrat, pay1 = otpay + notpay;
    		
    else
    			pay1 = hrswrkd * hrlyrat;
    		cout<<fixed<<setprecision(2);
    		cout<<
    "Your Gross Pay Is: $"<<pay1<<endl;
    		
    if (yrswrkd >= 1 && yrswrkd <= 5)
    			bnspay = pay1 * .05;
    		
    else
    			
    if (yrswrkd >= 6 && yrswrkd <= 9)
    				bnspay = pay1 * .10;
    			
    else
    				
    if (yrswrkd == 10)
    					bnspay = pay1 * .15;
    				
    else
    					
    if (yrswrkd > 10)
    						bnspay = pay1 * .20;
    					
    //endif
    				
    //endif
    			
    //endif
    		
    //endif
    		cout<<
    "Your Employee Longevity Bonus is: $"<<bnspay<<endl;
    		totpay = pay1 + bnspay;
    		cout<<
    "Your Total Pay Is: $"<<totpay<<endl<<endl;
    		cout<<
    "Thank You For Your Service!"<<endl<<endl;
    		cout<<
    "Please Enter Years of Service(less than 1 or Greater than 98 to Exit): ";
    		cin>>yrswrkd;
    		}
    	
    		cout<<
    "HAVE A NICE DAY!!!"<<endl<<endl;
    		system(
    "pause");
    		
    return 0;
    }
     
     
    

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. looping help
    By Titanium in forum C++ Programming
    Replies: 4
    Last Post: 08-31-2008, 11:47 AM
  2. Why isn't this looping?
    By mauhdeeb in forum C Programming
    Replies: 4
    Last Post: 02-05-2008, 01:10 AM
  3. I need help with looping
    By ben2000 in forum C Programming
    Replies: 3
    Last Post: 07-30-2007, 04:47 PM
  4. Looping once a second...
    By xconspirisist in forum C Programming
    Replies: 18
    Last Post: 12-02-2004, 01:10 AM
  5. Looping???
    By romeoz in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2003, 08:32 PM