我正在创建一个程序,询问用户的出生日期,然后打印出一周中的哪一天。我完成了代码,现在正在工作,但我想添加一个循环,所以它可以再次重复询问用户的出生日期,我现在坚持在哪里放置循环。请帮忙。
这是代码:
#include <iostream>
using namespace std;
const int JAN_DAYS = 31,
FEB_DAYS = 28,
MAR_DAYS = 31,
APR_DAYS = 30,
MAY_DAYS = 31,
JUN_DAYS = 30,
JUL_DAYS = 31,
AUG_DAYS = 31,
SEP_DAYS = 30,
OCT_DAYS = 31,
NOV_DAYS = 30;
int month, day, year;
int decPrev;
int dayOfWeek;
char date;
int main()
{
cout << "Input birth date [dd/mm/yyyy] " << endl; //prompts the user to input their birthdate with the following format
std::cin >> day >> date >> month >> date >> year;
cout << "You entered " << month << "/" << day << "/" << year << endl;
if (day > 0 && day < 32 && month > 0 && month < 13 && year > 0)
{
// calculates the day of the week for December 31 of the previous year
decPrev = ((year-1)*365+((year-1)/4)-((year-1)/100)+((year-1)/400))%7;
// adds the days of each month before the month entered
switch(month)
{
case 12:
decPrev += NOV_DAYS;
case 11:
decPrev += OCT_DAYS;
case 10:
decPrev += SEP_DAYS;
case 9:
decPrev += AUG_DAYS;
case 8:
decPrev += JUL_DAYS;
case 7:
decPrev += JUN_DAYS;
case 6:
decPrev += MAY_DAYS;
case 5:
decPrev += APR_DAYS;
case 4:
decPrev += MAR_DAYS;
case 3:
decPrev += FEB_DAYS;
if (0 == (year % 4)) decPrev += 1;
case 2:
decPrev += JAN_DAYS;
case 1:
decPrev += day;
break;
default:
break;
}
dayOfWeek = decPrev % 7;
// translates the number value to a string for the day
switch(dayOfWeek)
{
case 0:
cout << "The day of the week for " << month << "/"
<< day << "/" << year << " is Sunday" << endl;
break;
case 1:
cout << "The day of the week for " << month << "/"
<< day << "/" << year << " is Monday" << endl;
break;
case 2:
cout << "The day of the week for " << month << "/"
<< day << "/" << year << " is Tuesday" << endl;
break;
case 3:
cout << "The day of the week for " << month << "/"
<< day << "/" << year << " is Wednesday" << endl;
break;
case 4:
cout << "The day of the week for " << month << "/"
<< day << "/" << year << " is Thursday" << endl;
break;
case 5:
cout << "The day of the week for " << month << "/"
<< day << "/" << year << " is Friday" << endl;
break;
case 6:
cout << "The day of the week for " << month << "/"
<< day << "/" << year << " is Saturday" << endl;
break;
default:
break;
}
}
else cout << "Invalid input." << endl;
cin.get();
cin.ignore(); cin.ignore();
return 0;
}
答案 0 :(得分:1)
您可以将while
循环放在main()
的开头,但我更喜欢do ... while
,因为此代码必须至少执行一次
int main()
{
char choice;
do //your loop begins.
{
cout << "Input birth date [dd/mm/yyyy] " << endl; //prompts the user to input their birthdate with the following format
std::cin >> day >> date >> month >> date >> year;
cout << "You entered " << month << "/" << day << "/" << year << endl;
if (day > 0 && day < 32 && month > 0 && month < 13 && year > 0)
{
// calculates the day of the week for December 31 of the previous year
decPrev = ((year-1)*365+((year-1)/4)-((year-1)/100)+((year-1)/400))%7;
// adds the days of each month before the month entered
switch(month)
{
case 12:
decPrev += NOV_DAYS;
case 11:
decPrev += OCT_DAYS;
case 10:
decPrev += SEP_DAYS;
case 9:
decPrev += AUG_DAYS;
case 8:
decPrev += JUL_DAYS;
case 7:
decPrev += JUN_DAYS;
case 6:
decPrev += MAY_DAYS;
case 5:
decPrev += APR_DAYS;
case 4:
decPrev += MAR_DAYS;
case 3:
decPrev += FEB_DAYS;
if (0 == (year % 4)) decPrev += 1;
case 2:
decPrev += JAN_DAYS;
case 1:
decPrev += day;
break;
default:
break;
}
dayOfWeek = decPrev % 7;
// translates the number value to a string for the day
switch(dayOfWeek)
{
case 0:
cout << "The day of the week for " << month << "/"
<< day << "/" << year << " is Sunday" << endl;
break;
case 1:
cout << "The day of the week for " << month << "/"
<< day << "/" << year << " is Monday" << endl;
break;
case 2:
cout << "The day of the week for " << month << "/"
<< day << "/" << year << " is Tuesday" << endl;
break;
case 3:
cout << "The day of the week for " << month << "/"
<< day << "/" << year << " is Wednesday" << endl;
break;
case 4:
cout << "The day of the week for " << month << "/"
<< day << "/" << year << " is Thursday" << endl;
break;
case 5:
cout << "The day of the week for " << month << "/"
<< day << "/" << year << " is Friday" << endl;
break;
case 6:
cout << "The day of the week for " << month << "/"
<< day << "/" << year << " is Saturday" << endl;
break;
default:
break;
}
}
else cout << "Invalid input." << endl;
//Here is the statement which asks the user to continue or not.
cout<<"Want to continue?";
cin>>choice;
//If y or Y is entered, the loop will continue,
//otherwise it will terminate.
} while (choice == 'y' || choice == 'Y');
return 0;
}
答案 1 :(得分:0)
一个简单的循环程序就像这样工作(它将永远运行):
int main()
{
while(true)
{
//do stuff
}
}
另一种选择是这样做(它将运行直到x == 0):
int main()
{
do{
//do stuff
cin >> x;
}while(x!=0)
}