#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
Do while loop not working correctly
Page 1 of 15 Replies - 378 Views - Last Post: 18 March 2013 - 11:00 PM
#1
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?
Replies To: Do while loop not working correctly
#2
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.
#3
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.
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.
#4
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.
#5
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.
#6
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.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote






|