Thread: Complete Newb, have a loop that won't exit

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    3

    Complete Newb, have a loop that won't exit

    Hi, I'm Completely new to this. I'm taking a C++ class where I have to write a payroll program, but somehow the loop I wrote won't exit even when I enter the appropriate variable. Can anyone tell me what I'm doing wrong? Thanks!

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    
    int main()
    {
    	//Declare Variables
    	char H, h, S, s, P, p;
    	char payCode;
    	double hrsWorked;
    	double grossPay;
    	double otPay;
    	double payRate;
    	double regPay;
    	double totalPay;
    	int exempt;
    
    
    	//Set decimal points
    	cout.setf(ios::fixed);
    	cout.setf(ios::showpoint);
    	cout.precision(2);
    
    	//Input pay code
    	cout << "Enter pay code (H = hourly, S = Salary, P = Piece Work): ";
    	cin >> payCode;
    
    	//Loop if payCode not equal to: H,h,S,s,P,p
    	//This part doesn't work, it won't exit loop if H,h,S,s,P,or p are entered
    	while ( (payCode !=H) || (payCode !=h) || 
    		(payCode !=S) || (payCode !=s) || 
    		(payCode !=P) || (payCode !=p)    )
    	{
    		cout << "Invalid pay type, please try agin. ";
    		cout << endl;
    		cout << "Enter pay code (H = hourly, S = Salary, P = Piece Work): ";
    		cin >> payCode;
    	}
    
            //Here it should exit the loop to continue with the rest of the program but it doesn't :(

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    you never assign H, h, S, s, P, or p to a value, so how is it suppose to compare payCode to that value?

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    3
    I'm lost, what do you mean never assign those characters as a value??
    this is one of many many many things my teacher never cared to tell us about

    how should it look? what should i do instead??

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    You need to surround the character constants with a pair of ' like for example 'H', 'S'. Otherwise they are interpreted as variables. Like this:

    > (payCode !='H') || (payCode !='h')

    And this won't work because if payCode is equal to 'H', naturally it is not equal to 'h', so it always evaluates to "true" in the end. What you want to do is "repeat while payCode is not 'H' AND payCode is not 'h' AND payCode is not ...". So:

    while ((payCode !='H') && (payCode !='h') && ....

  5. #5
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    you declared your variables H h S s P p but never assigned a value to them..
    ex char H = 'H';

    just naming it H does not make it mean 'H' that is why they are variables..

    also this logic says that while paycode is not equal to all those variables.. loop..
    Code:
    while ( (payCode !=H) || (payCode !=h) || 
    		(payCode !=S) || (payCode !=s) || 
    		(payCode !=P) || (payCode !=p)    )
    replacing all the ||'s with &&'s would say if paycode is not equal to any of then loop.. which is what you want..

    so if you initialize all and change that loop it should *work* as expected..

    another way to do this would be to just compare payCode without all the variables..
    paycode != 'H' ..

    and yet another way to do this would be to make a numbered menu and then in your loop say.. while(payCode<0 || paycode>3) { ... } or somthing to that effect..

    hth

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    3
    ahhh i see!!

    thank you both very much!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  2. Stopping an Infinite Loop
    By linuxpyro in forum C Programming
    Replies: 4
    Last Post: 11-30-2006, 12:21 PM
  3. Loop will not complete
    By jms318 in forum C Programming
    Replies: 7
    Last Post: 11-04-2006, 07:45 PM
  4. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM
  5. I can't figure out why this doesn't exit the loop, MAN!
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2002, 08:47 AM