Do While Loop Not Exiting

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 4074 Views - Last Post: 13 July 2012 - 11:53 AM Rate Topic: -----

#1 Calamity-1988  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 27-June 12

Do While Loop Not Exiting

Posted 12 July 2012 - 09:50 PM

I'm just trying to get my program to exit when it asks if the user would like to quit. For some reason, the do while loop in my main function isn't properly functioning, and I'm not sure why. Program's below.

#include <iostream>
#include <cmath>
using namespace std;

void explanation();
void employeeType(int & vac, int & sick);
int vacation( int vacationDays);
int sick(int sickDays);
void calculate(int usedVacation, int usedSick);

int main() // The code is limited here to rely mainly on functions.
{
	int vacationDays, sickDays, usedVacation, usedSick;
	char quit;
	do
	{
		explanation();
		employeeType(vacationDays, sickDays);
		usedVacation = vacation(vacationDays);
		usedSick = sick(sickDays);
		calculate(usedVacation, usedSick);
		cout << "Would you like to quit? Selecting 'N' will repeat the program. <Y/N>: ";
		cin >> quit;
		cout << endl;
	} while(quit != 'y' || 'Y');
	cout << "Press any key to terminate the program." << endl;
	cin.get();
	cin.get();
	return 0;
};

void explanation()
{
	cout << "This program serves to calculate the number of miles employees travel annually\nbased on three variables. It's Monday, January 1, 2007. As a new employee of\nWells Fargo, you only work weekdays and receive six federal holidays off.\nWith 255 working days in the year, you receive a differing number of vacation\nand sick days depending on whether you are a manager or a worker." << endl;
	cout << endl;
}

void employeeType(int & vac, int & sick)
{
	char type;
	do
	{	
		cout << "Are you a [M]anager or a [W]orker? ";
		cin >> type;
		if (type == 'm' || type == 'M')
		{
			vac = 20;
			sick = 10;
			break;
		}
		if (type == 'w' || type == 'W')
		{
			vac = 15;
			sick = 15;
			break;
		}
		else
		{
			cout << endl;
			cout << "That is not a valid entry. Please enter 'm', 'M', 'w', or 'W'." << endl;
			cout << endl;
			continue;
		}
	} while (type != 'm' || 'M' || 'w' || 'W');
	cout << "You are allotted " << vac << " vacation days and " << sick << " sick days for the year." << endl;
	cout << endl;
}

int vacation(int vacationDays)
{
	int ndays;
	do
	{
		cout << "Please enter the number of vacation days you plan to use: ";
		cin >> ndays;
		if (ndays > vacationDays || ndays < 0)
		{
			cout << endl;
			cout << "You must enter a valid number within the range of allotted vacation days." << endl;
		}
		else
		{
			cout << endl;
			return ndays;
		}
	}while(ndays > vacationDays || ndays < 0);
}

int sick(int sickDays)
{
	int ndays;
	do
	{
		cout << "Please enter the number of sick days you plan to use: ";
		cin >> ndays;
		if (ndays > sickDays || ndays < 0)
		{
			cout << "You must enter a valid number within the range of allotted sick days." << endl;
			cout << endl;
		}
		else
		{
			cout << endl;
			return ndays;
		}
	}while(ndays > sickDays || ndays < 0);
}

void calculate(int usedVacation, int usedSick)
{
	int daysWork = (255 - usedVacation - usedSick);	// Calculates the number of days worked by subtracting the number of claimed vacation and sick days from the total working days.
	int milesTraveled = daysWork * 530; // Each workday is multipled by a value of 530 miles to account for the daily commute back-and-forth between CityA and CityB.
	cout << "Based on your input, you plan to work " << daysWork << " days this year. Each workday, you\nwill commute 265 miles to and from work for a total of 530 miles. Multiplying\n530 miles by " << daysWork << " workdays, it is estimated that you will travel a total\nof " << milesTraveled << " miles this year. I hope you have a comfortable, properly-insured car!" << endl;
	cout << endl;
}



Any thoughts? I just want the program to break from the loop and exit when the user enters 'quit' as 'y' or 'Y'. Any help would be appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Do While Loop Not Exiting

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1708
  • View blog
  • Posts: 3,283
  • Joined: 30-May 10

Re: Do While Loop Not Exiting

Posted 12 July 2012 - 09:57 PM

051 if (type == 'w' || type == 'W')
Is good.

064 } while (type != 'm' || 'M' || 'w' || 'W');
Is NOT good.
Was This Post Helpful? 0
  • +
  • -

#3 Calamity-1988  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 27-June 12

Re: Do While Loop Not Exiting

Posted 12 July 2012 - 09:59 PM

View PostSalem_c, on 12 July 2012 - 09:57 PM, said:

051 if (type == 'w' || type == 'W')
Is good.

064 } while (type != 'm' || 'M' || 'w' || 'W');
Is NOT good.


Why? It works. Also, I'm talking about the do while loop in my main function. Everything else works fine in debugging.
Was This Post Helpful? 0
  • +
  • -

#4 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,078
  • Joined: 01-December 05

Re: Do While Loop Not Exiting

Posted 12 July 2012 - 10:00 PM

while (type != 'm' || 'M' || 'w' || 'W');

That code does not compare type against the various character literals. It checks type against 'm' and then if type is equal to 'm' it checks if 'M' is not false (0.) Since 'M' is certainly not false the while loop will never end.
Was This Post Helpful? 0
  • +
  • -

#5 totgeburt  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 227
  • Joined: 03-August 11

Re: Do While Loop Not Exiting

Posted 12 July 2012 - 11:23 PM

View PostGWatt, on 13 July 2012 - 05:00 AM, said:

while (type != 'm' || 'M' || 'w' || 'W');

That code does not compare type against the various character literals. It checks type against 'm' and then if type is equal to 'm' it checks if 'M' is not false (0.) Since 'M' is certainly not false the while loop will never end.


he's trying to say it should look like
if (type == 'w' || type == 'W')



instead of type != the following various character literals

so it compares each literal correctly
Was This Post Helpful? 0
  • +
  • -

#6 Calamity-1988  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 27-June 12

Re: Do While Loop Not Exiting

Posted 13 July 2012 - 07:06 AM

That doesn't even address what I'm talking about.

int main() // The code is limited here to rely mainly on functions.
{
	int vacationDays, sickDays, usedVacation, usedSick;
	char quit;
	do
	{
		explanation();
		employeeType(vacationDays, sickDays);
		usedVacation = vacation(vacationDays);
		usedSick = sick(sickDays);
		calculate(usedVacation, usedSick);
		cout << "Would you like to quit? Selecting 'N' will repeat the program. <Y/N>: ";
		cin >> quit;
		cout << endl;
	} while(quit != 'y' || quit != 'Y');
	cout << "Press any key to terminate the program." << endl;
	cin.get();
	cin.get();
	return 0;
};



THIS. Why doesn't it quit when I type 'y' or 'Y'? The others loop just fine if you actually test them.
Was This Post Helpful? 0
  • +
  • -

#7 redhat69  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 33
  • Joined: 28-June 12

Re: Do While Loop Not Exiting

Posted 13 July 2012 - 07:28 AM

Here is your program working fine. There is no need for the or at the end of the do while loop.

#include <iostream>
#include <cmath>
using namespace std;

void explanation();
void employeeType(int & vac, int & sick);
int vacation( int vacationDays);
int sick(int sickDays);
void calculate(int usedVacation, int usedSick);

int main() // The code is limited here to rely mainly on functions.
{
	int vacationDays, sickDays, usedVacation, usedSick;
	char quit;
	do
	{
		explanation();
		employeeType(vacationDays, sickDays);
		usedVacation = vacation(vacationDays);
		usedSick = sick(sickDays);
		calculate(usedVacation, usedSick);
		cout << "Would you like to quit? Selecting 'N' will repeat the program. <Y/N>: ";
		cin >> quit;
		cout << endl;
	} while(quit != 'y');
	cout << "Press any key to terminate the program." << endl;
	cin.get();
	cin.get();
	return 0;
};

void explanation()
{
	cout << "This program serves to calculate the number of miles employees travel annually\nbased on three variables. It's Monday, January 1, 2007. As a new employee of\nWells Fargo, you only work weekdays and receive six federal holidays off.\nWith 255 working days in the year, you receive a differing number of vacation\nand sick days depending on whether you are a manager or a worker." << endl;
	cout << endl;
}

void employeeType(int & vac, int & sick)
{
	char type;
	do
	{	
		cout << "Are you a [M]anager or a [W]orker? ";
		cin >> type;
		if (type == 'm' || type == 'M')
		{
			vac = 20;
			sick = 10;
			break;
		}
		if (type == 'w' || type == 'W')
		{
			vac = 15;
			sick = 15;
			break;
		}
		else
		{
			cout << endl;
			cout << "That is not a valid entry. Please enter 'm', 'M', 'w', or 'W'." << endl;
			cout << endl;
			continue;
		}
	} while (type != 'm' || 'M' || 'w' || 'W');
	cout << "You are allotted " << vac << " vacation days and " << sick << " sick days for the year." << endl;
	cout << endl;
}

int vacation(int vacationDays)
{
	int ndays;
	do
	{
		cout << "Please enter the number of vacation days you plan to use: ";
		cin >> ndays;
		if (ndays > vacationDays || ndays < 0)
		{
			cout << endl;
			cout << "You must enter a valid number within the range of allotted vacation days." << endl;
		}
		else
		{
			cout << endl;
			return ndays;
		}
	}while(ndays > vacationDays || ndays < 0);
}

int sick(int sickDays)
{
	int ndays;
	do
	{
		cout << "Please enter the number of sick days you plan to use: ";
		cin >> ndays;
		if (ndays > sickDays || ndays < 0)
		{
			cout << "You must enter a valid number within the range of allotted sick days." << endl;
			cout << endl;
		}
		else
		{
			cout << endl;
			return ndays;
		}
	}while(ndays > sickDays || ndays < 0);
}

void calculate(int usedVacation, int usedSick)
{
	int daysWork = (255 - usedVacation - usedSick);	// Calculates the number of days worked by subtracting the number of claimed vacation and sick days from the total working days.
	int milesTraveled = daysWork * 530; // Each workday is multipled by a value of 530 miles to account for the daily commute back-and-forth between CityA and CityB.
	cout << "Based on your input, you plan to work " << daysWork << " days this year. Each workday, you\nwill commute 265 miles to and from work for a total of 530 miles. Multiplying\n530 miles by " << daysWork << " workdays, it is estimated that you will travel a total\nof " << milesTraveled << " miles this year. I hope you have a comfortable, properly-insured car!" << endl;
	cout << endl;
}



Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: Do While Loop Not Exiting

Posted 13 July 2012 - 07:30 AM

It's because your do-while condition is incorrectly constructed.

Think about it. Your condition is quit != 'y' || quit != 'Y'. The || operator will return true if either one of the conditions is true.

The user enters 'Y': So the first condition is satisfied: quit != 'y'. Since you said do-while, so the loop is re-entered.

The user enters 'y': The first condition is not satisfied, but the second one is: quit != 'Y'. So again the loop is re-entered.

This post has been edited by Skydiver: 13 July 2012 - 07:35 AM

Was This Post Helpful? 0
  • +
  • -

#9 Calamity-1988  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 27-June 12

Re: Do While Loop Not Exiting

Posted 13 July 2012 - 07:35 AM

View PostSkydiver, on 13 July 2012 - 07:30 AM, said:

It's because your do-while condition is incorrectly constructed.

Think about it. Your condition is quit != 'y' || quit != 'Y'.

The user enters 'Y': So the first condition is satisfied: quit != 'y'. Since you said do-while, so the loop is re-entered.

The user enters 'y': The first condition is not satisfied, but the second one is: quit != 'Y'. So again the loop is re-entered.


So how would I go about having the loop terminate if the user enters 'y' or 'Y'? Is that possible?
Was This Post Helpful? 0
  • +
  • -

#10 redhat69  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 33
  • Joined: 28-June 12

Re: Do While Loop Not Exiting

Posted 13 July 2012 - 07:39 AM

I don't think there is a way too. Is it really that imortant to distinguish between upper and lower case chars in this program? Just seems like a dumb task that doesn't need to be done.
Was This Post Helpful? 0
  • +
  • -

#11 Calamity-1988  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 27-June 12

Re: Do While Loop Not Exiting

Posted 13 July 2012 - 07:40 AM

I agree. The entire program is dumb, but it's how I'm learning function prototyping and definitions. I don't plan to include it in the program I submit, I just want to know for my own benefit in the future. But thanks a lot for your help! You and Skydiver both helped me understand why the loop wasn't exiting. :]
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: Do While Loop Not Exiting

Posted 13 July 2012 - 07:41 AM

Yes, it's possible. Express the condition in English (instead of trying to mangle code). You want the exit condition to be if quit is a 'Y' or if quit is a 'y'.

I can't make it any more explicit than that without writing code and getting reported for just giving away the solution.

This post has been edited by Skydiver: 13 July 2012 - 07:41 AM

Was This Post Helpful? 0
  • +
  • -

#13 redhat69  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 33
  • Joined: 28-June 12

Re: Do While Loop Not Exiting

Posted 13 July 2012 - 07:41 AM

View PostCalamity-1988, on 13 July 2012 - 07:35 AM, said:

View PostSkydiver, on 13 July 2012 - 07:30 AM, said:

It's because your do-while condition is incorrectly constructed.

Think about it. Your condition is quit != 'y' || quit != 'Y'.

The user enters 'Y': So the first condition is satisfied: quit != 'y'. Since you said do-while, so the loop is re-entered.

The user enters 'y': The first condition is not satisfied, but the second one is: quit != 'Y'. So again the loop is re-entered.


So how would I go about having the loop terminate if the user enters 'y' or 'Y'? Is that possible?



Make the whole looop into a function>>>call function>>>>>use if statement to decide whether or not to run it again maybe.
Was This Post Helpful? 0
  • +
  • -

#14 Calamity-1988  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 27-June 12

Re: Do While Loop Not Exiting

Posted 13 July 2012 - 07:51 AM

View PostSkydiver, on 13 July 2012 - 07:41 AM, said:

Yes, it's possible. Express the condition in English (instead of trying to mangle code). You want the exit condition to be if quit is a 'Y' or if quit is a 'y'.

I can't make it any more explicit than that without writing code and getting reported for just giving away the solution.


Got it, I think.

int main() // The code is limited here to rely mainly on functions.
{
	int vacationDays, sickDays, usedVacation, usedSick;
	bool quitting = false;
	char quit;
	while(quitting != true)
	{
		explanation();
		employeeType(vacationDays, sickDays);
		usedVacation = vacation(vacationDays);
		usedSick = sick(sickDays);
		calculate(usedVacation, usedSick);
		cout << "Would you like to quit? Selecting 'N' will repeat the program. <Y/N>: ";
		cin >> quit;
		if (quit == 'y' || quit == 'Y')
		{
			quitting = true;
		}
		cout << endl;
	}
	cout << "Press any key to terminate the program." << endl;
	cin.get();
	cin.get();
	return 0;
};



Works perfectly!
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: Do While Loop Not Exiting

Posted 13 July 2012 - 07:53 AM

View Postredhat69, on 13 July 2012 - 07:39 AM, said:

I don't think there is a way too. Is it really that imortant to distinguish between upper and lower case chars in this program? Just seems like a dumb task that doesn't need to be done.


It's a matter of making the computer-human interface better.

Consider that all that the only objective of the prompt and loop was a way to let the user run the program again. What if the program didn't bother asking and it always looped back to the beginning and the way to quit was by pressing Ctrl-C. Functional right? User friendly? Depends. If it's a repetitive task, it's one less prompt the user has to deal with. Having to know that pressing Ctrl-C quits. Not really.

This in one of the things that distinguishes Apple products and software from Microsoft software. Apple takes time to really think about the UI and how to streamline that process. It's that extra fit and finish. MS stuff tends to go for functional.

Also consider this. After inputting the character from the user, you could have used toupper to force the character to upper case, and then have only one condition in the do-while() loop.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2