5 Replies - 1069 Views - Last Post: 18 March 2013 - 11:00 PM Rate Topic: -----

#1 popa  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 15-February 13

Do while loop not working correctly

Posted 18 March 2013 - 09:10 PM

I'm trying to make my program work correctly. When you type Y or y, the program will output, when you type n or N it should kill the program. Where am I going wrong?

#include <iostream>

using namespace std;

bool date_checker(int month, int day, int year);
/*
Checks the month, date, and year to see if they fall
within the correct paramaters listed below:

1)Month must be between 1 and 12.
2)If the month is 4,6,9,or 11 the day (DD)
must be between 1 and 30.
3)If the month is 1,3,5,7,8,10,or 12 the date
needs to be between 1 and 31.
4)If the month is 2 the date needs to be 
between 1 and 29.
*/

//Begin user input and output
int main()
{
	int month;
	int day;
	int year;
	char ans;

//Begin welcome message and test information
	cout << "Welcome to Date Checker!\n";
	cout << "Testing first ...." << endl;

	if (date_checker(13,24,1999))
		cout << "Date: 13/24/1999 is valid.\n";
	else 
		cout << "Date: 13/24/1999 is NOT valid!\n";

	if (date_checker(2,30,1999))
		cout << "Date: 2/30/1999 is valid.\n";
	else 
		cout << "Date: 2/30/1999 is NOT valid!\n"; 

	if (date_checker(2,29,1999))
		cout << "Date: 2/29/1999 is valid.\n";
	else
		cout << "Date: 2/29/1999 is NOT valid!\n";		

	if (date_checker(4,31,1999))
		cout << "Date: 4/31/1999 is valid.\n";
	else
		cout <<"Date: 4/31/1999 is NOT valid!\n";

	if (date_checker(1,31,1999))
		cout << "Date: 1/31/1999 is valid.\n";
	else
		cout <<"Date: 1/31/1999 is NOT valid!\n";
//End welcome message and test information
		
//Begin do while loop
do
{
	cout << "Now taking inputs from the user ...\n";
	cout << "Please enter a date (mm dd yyyy): ";
	cin >> month >> day >> year;
	cout << "\n";
	cout << "Assume there are 29 days in February! ";
	if (month == true || day == true || year == true)
	{
		cout << "Date: " << month << "/" << day << "/" << year << " is valid.\n";
		cout << "Do you want to continue (Y/N)?";
		cin >> ans;
		break;
	}
  	else (month == false || day == false || year == false);
	{
		cout << "Date: " << month << "/" << day << "/" << year << " is NOT valid!\n";
		cout << "Do you want to continue (Y/N)? ";
		cin >> ans;
		break;
	}
	
}
while (ans == 'Y' || ans == 'y');
return 0;
//End do while loop				   
}


//End user input and output

//Begin function definition
bool date_checker(int month, int day, int year)
{
//Begin funtion body

	//Month has to be 1-12
	if (month < 1 || month > 12)
	{ 
		//cout << "valid number";
		return false;
	}

	//Month 4,6,9,11 has to have day value of 1-30
	if ((month == 4 || month == 6 || month == 9 || month == 11) && (day >=1 && day <=30))
	{ 
		//cout << "valid number";
		return true;
	}

	//Month 1,3,5,7,8,10,12 has to have day value of 1-31
	if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day >=1 && day <=31))
	{
		//cout << "valid number";
		return true;
	}
	//Month 2 has to have day value of 1-29
	if ((month == 2) && (day >=1 && day <=29))
	{
		//cout << "valid number";
		return true;
	}

	//if month and day value to not match values above
	else
	{
		//cout << "NOT a valid number";
		return false;
	}
	 
//End function body
}
//End function definition





Is This A Good Question/Topic? 0
  • +

Replies To: Do while loop not working correctly

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Do while loop not working correctly

Posted 18 March 2013 - 09:17 PM

Learn about loops. A do-while loop will do everything in its scope at least once, whereas a while loop will not execute its body if the condition is false.
Was This Post Helpful? 0
  • +
  • -

#3 popa  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 15-February 13

Re: Do while loop not working correctly

Posted 18 March 2013 - 09:30 PM

Ah, so basically you're suggesting that I turn my do while loop into a while loop?

The only issue I'm having when thinking about making it a while loop is that lines 27 - 55 can only be executed once, when the program is executed. After that it has to take inputs from the user (line 60) until the condition is met where N or n is entered. If Y or y is entered it should return to ask the user for inputs once again.
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Do while loop not working correctly

Posted 18 March 2013 - 09:47 PM

Yes. All you need is a special case in your while loop that breaks out if the user no longer wishes to continue.
Was This Post Helpful? 0
  • +
  • -

#5 popa  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 15-February 13

Re: Do while loop not working correctly

Posted 18 March 2013 - 10:19 PM

//Program: Lab 07 
//Professor: Dr. Honggang Zhang
//Class: CISC 1600 RH Section 1 
#include <iostream>

using namespace std;

bool date_checker(int month, int day, int year);
/*
Checks the month, date, and year to see if they fall
within the correct paramaters listed below:

1)Month must be between 1 and 12.
2)If the month is 4,6,9,or 11 the day (DD)
must be between 1 and 30.
3)If the month is 1,3,5,7,8,10,or 12 the date
needs to be between 1 and 31.
4)If the month is 2 the date needs to be 
between 1 and 29.
*/

//Begin user input and output
int main()
{
	int month;
	int day;
	int year;
	char ans;

//Begin welcome message and test information
	cout << "Welcome to Date Checker!\n";
	cout << "Testing first ...." << endl;

	if (date_checker(13,24,1999))
		cout << "Date: 13/24/1999 is valid.\n";
	else 
		cout << "Date: 13/24/1999 is NOT valid!\n";

	if (date_checker(2,30,1999))
		cout << "Date: 2/30/1999 is valid.\n";
	else 
		cout << "Date: 2/30/1999 is NOT valid!\n"; 

	if (date_checker(2,29,1999))
		cout << "Date: 2/29/1999 is valid.\n";
	else
		cout << "Date: 2/29/1999 is NOT valid!\n";		

	if (date_checker(4,31,1999))
		cout << "Date: 4/31/1999 is valid.\n";
	else
		cout <<"Date: 4/31/1999 is NOT valid!\n";

	if (date_checker(1,31,1999))
		cout << "Date: 1/31/1999 is valid.\n";
	else
		cout <<"Date: 1/31/1999 is NOT valid!\n";
//End welcome message and test information
		
//Begin while loop

	cout << "Now taking inputs from the user ...\n";
	cout << "Please enter a date (mm dd yyyy): ";
	cin >> month >> day >> year;
	cout << "\n";
	cout << "Assume there are 29 days in February! ";
	
	
	while(month == true || day == true || year == true)
	{
		cout << "Date: " << month << "/" << day << "/" << year << " is valid.\n";
		cout << "Do you want to continue (Y/N)?";
		cin >> ans;
		if (ans == 'n' || ans == 'N')
		break;
		else if (ans == 'y' || ans == 'Y')
		{
		cout << "Now taking inputs from the user ...\n";
		cout << "Please enter a date (mm dd yyyy): ";
		cin >> month >> day >> year;
		cout << "\n";
		cout << "Assume there are 29 days in February! ";
		}
	}
	
  	while (month == false || day == false || year == false)
	{
		cout << "Date: " << month << "/" << day << "/" << year << " is NOT valid!\n";
		cout << "Do you want to continue (Y/N)? ";
		cin >> ans;
		if (ans == 'n' || ans == 'N')
		break;
		else if (ans == 'y' || ans == 'Y')
		{
		cout << "Now taking inputs from the user ...\n";
		cout << "Please enter a date (mm dd yyyy): ";
		cin >> month >> day >> year;
		cout << "\n";
		cout << "Assume there are 29 days in February! ";
		}
	}
	
//End while loop				   
}


//End user input and output

//Begin function definition
bool date_checker(int month, int day, int year)
{
//Begin funtion body

	//Month has to be 1-12
	if (month < 1 || month > 12)
	{ 
		//cout << "valid number";
		return false;
	}

	//Month 4,6,9,11 has to have day value of 1-30
	if ((month == 4 || month == 6 || month == 9 || month == 11) && (day >=1 && day <=30))
	{ 
		//cout << "valid number";
		return true;
	}

	//Month 1,3,5,7,8,10,12 has to have day value of 1-31
	if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day >=1 && day <=31))
	{
		//cout << "valid number";
		return true;
	}
	//Month 2 has to have day value of 1-29
	if ((month == 2) && (day >=1 && day <=29))
	{
		//cout << "valid number";
		return true;
	}

	//if month and day value to not match values above
	else
	{
		//cout << "NOT a valid number";
		return false;
	}
	 
//End function body
}
//End function definition






I feel like I'm getting closer but when I enter the wrong date (i.e., 13 12 1999) the program halts. If I enter in something that is true (i.e., 01 07 1999) the program functions properly. I'm lost.
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Do while loop not working correctly

Posted 18 March 2013 - 11:00 PM

How can you be lost with your own code? Follow through the logic to see what is happening.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1