所以基本上我刚开始学习c ++并完成了我完全固定的作业,我用了几天的时间在google上搜索了一下,但是却找不到如何使它起作用。
分配非常简单:
根据用户的输入(出生年,月,日)计算一个人的年龄。
用户需要输入出生日期(例如:星期四)
程序需要验证是否确实正确
现在我停留在第3点;
我制定了一个公式来计算某人出生的那一天。基本上,该公式输出的数字是0-6,并且引用的是一天(0是星期天,1是星期一,依此类推)
问题是用户会输入例如星期四,但是程序仅将nr 4识别为星期四。因此,程序无法确认或否认用户确实是在星期四出生的。
为解决此问题,我尝试了以下操作:
这是计算数字0到6(例如,0应该等于星期日)的公式
int fdb = ((jcr + mc + ec + birthday - sjc) % 7); //
这是完整的代码:
int birthdayfdb;
int sunday;
int monday;
int tuesday;
int wednesday;
int thursday;
int friday;
int saturday;
cout <<"On which day are you born?" << endl;
cin >> birthdayfdb; // Here the users inputs the day where he or she is born on
if ((birthdayfdb == sunday && fdb == 0) || (birthdayfdb == monday && fdb == 1) || (birthdayfdb == tuesday && fdb == 2) || (birthdayfdb == wednesday && fdb == 3) || (birthdayfdb == thursday && fdb == 4) || (birthdayfdb == friday && fdb == 5) || (birthdayfdb == saturday && fdb == 6))
{
cout << "This is Correct" << endl;
}
else
{
cout << "This is incorrect" << endl;
return 0;
}
所以这行不通,因为无论用户输入什么生日,它总是说这是正确的。
我在互联网上查找了各种各样的东西(stackflow,youtube等),但是我不知道该怎么做。
完成:
#include <iostream>
using namespace std;
int main ( ) {
int birthyear;
int birthmonth;
int birthday;
cout << “What is your birth year?” << endl;
cin >> birthyear;
if ((birthyear > 2008) || (birthyear < 1928))
{
cout <<“You are too old/young.“<< endl;
return 0;
}
cout << "In which month are you born? (Answer should be in
numbers)“<< endl;
cin >> birthmonth;
if (((birthmonth > 9) && (birthmonth == 2008)) || ((birthmonth < 9)
&& (birthmonth == 1928)))
{
cout <<"You are too old/young.”<< endl;
return 0;
}
cout << “On which day are you born?” << endl;
cin >> birthday;
if (((birthday > 24) && (birthmonth == 9) && (birthyear == 2008)) ||
((birthday < 24) && (birthmonth == 9) && (birthyear == 1928)))
{
cout <<"You are too old/young.”<< endl;
return 0;
}
double ageinyears = (2018 - birthyear);
int agemonths = (9 - birthmonth);
int agedays = (24 - birthday);
int totalmonths = ((ageinyears*12) + agemonths);
cout << “Your age is "<<ageinyears<<" jaar, "<<agemonths<<" months,
and " <<agedays<<" days, or "<<totalmonths<<" months.” << endl;
if ((birthday == 24) && (agemonths != 0))
{
cout <<" "<< endl;
cout << “Congratulations on your month anniversary!“<< endl;
cout <<" "<< endl;
}
else
{
cout << " "<< endl;
}
if ((birthday == 24) && (agemonths == 0))
cout <<" "<< endl;
cout << “Congrationlations with your birthday!”<< endl;
cout <<" "<< endl;
}
int jc = birthyear % 100;
int jcr = ((jc + (jc / 4)) % 7);
int ec;
if ((birthyear >= 1900) && (birthyear <= 1999))
{
ec = 0;
}
if ((birthyear >= 2000) && (birthyear <= 2099))
{
ec = 6;
}
int mc;
if ((birthmonth == 1) || (birthmonth == 10))
{
mc = 0;
}
if (birthmonth == 5)
{
mc = 1;
}
if (birthmonth == 8)
{
mc = 2;
}
if ((birthmonth == 2) || (birthmonth == 3) || (birthmonth == 11))
{
mc = 3;
}
if (birthmonth == 6)
{
mc = 4;
}
if ((birthmonth == 9) || (birthmonth == 12))
{
mc = 5;
}
if ((birthmonth == 4) || (birthmonth == 7))
{
mc = 6;
}
int sjc;
if ((birthmonth == 1 || 2) && (birthyear % 4 == 0 ))
{
sjc = 1;
}
else
{
sjc = 0;
}
int fdb = ((jcr + mc + ec + birthday - sjc) % 7);
cout << fdb << endl;
std::string birthdayfdb;
cout <<"On which day are you born?” << endl;
cin >> birthdayfdb;
bool check = false;
switch(fdb){
case 0:
if(birthdayfdb() == “sunday”) check = true;
case 1:
if(birthdayfdb() == “monday”) check = true;
case 2:
if(birthdayfdb() == “tuesday”) check = true;
case 3:
if(birthdayfdb() == “wednesday”) check = true;
case 4:
if(birthdayfdb() == “thursday”) check = true;
case 5:
if(birthdayfdb() == “friday”) check = true;
case 6:
if(birthdayfdb() == “saturday”) check = true;
}
if(check){
cout << "This is correct" << endl;
}
else{
cout << "This is incorrect" << endl;
}
}
答案 0 :(得分:1)
您需要使用Switch Statement
才能实现所需的功能。
这是您固定的完整代码:
#include <iostream>
#include <string>
using namespace std;
int main ( ){
int birthyear;
int birthmonth;
int birthday;
cout << "What is your birth year?" << endl;
try{
cin >> birthyear;
}
catch(...){
cout << "A number is the only accepted input" << endl;
return 0;
}
cout << "In which month are you born? (Answer should be in numbers from 1 to 12)"<< endl;
try{
cin >> birthmonth;
}
catch(...){
cout << "A number is the only accepted input" << endl;
return 0;
}
cout << "On which day are you born?" << endl;
try{
cin >> birthday;
}
catch(...){
cout << "A number is the only accepted input" << endl;
return 0;
}
if ((birthday > 24 || birthday < 1) && (birthmonth > 9 || birthmonth < 1) && (birthyear > 2008 || birthyear < 1918)) {
cout <<"You are too old/young."<< endl;
return 0;
}
double ageinyears = (2018 - birthyear);
int agemonths = (9 - birthmonth);
int agedays;
switch (birthmonth){
case 2:
if((birthyear%100 == 0) && (birthyear%400 == 0)){
agedays = (29 - birthday);
}
else if(birthyear%4 == 0 && birthyear%100 != 0){
agedays = (29 - birthday);
}
else{
agedays = (28 - birthday);
}
break;
case 4:
agedays = (30 - birthday);
break;
case 11:
agedays = (30 - birthday);
break;
case 6:
agedays = (30 - birthday);
break;
case 9:
agedays = (30 - birthday);
break;
default:
agedays = (31 - birthday);
break;
}
int totalmonths = ((ageinyears*12) + agemonths);
cout << "Your age is " <<ageinyears<< " year, "<<agemonths<<" months, and " <<agedays<<" days, or "<<totalmonths<<" total months." << endl;
if ((birthday == 24) && (agemonths == 0)){
cout <<" "<< endl;
cout << "Congrationlations with your birthday!"<< endl;
cout <<" "<< endl;
}
else if ((agedays == 24) && (agemonths != 0)) {
cout <<" "<< endl;
cout << "Congratulations on your month anniversary!"<< endl;
cout <<" "<< endl;
}
int jc = birthyear % 100;
int jcr = ((jc + (jc / 4)) % 7);
int ec;
if ((birthyear >= 1900) && (birthyear <= 1999)){
ec = 0;
}
else if ((birthyear >= 2000) && (birthyear <= 2099)){
ec = 6;
}
int mc;
switch(birthmonth){
case 1:
mc = 0;
break;
case 2:
mc = 3;
break;
case 3:
mc = 3;
break;
case 4:
mc = 6;
break;
case 5:
mc = 1;
break;
case 6:
mc = 4;
break;
case 7:
mc=6;
break;
case 8:
mc = 2;
break;
case 9:
mc = 5;
case 10:
mc = 0;
break;
case 11:
mc = 3;
break;
case 12:
mc = 5;
break;
}
int sjc;
if ((birthmonth == 1 || 2) && (birthyear % 4 == 0 )){
sjc = 1;
}
else{
sjc = 0;
}
int fdb = ((jcr + mc + ec + birthday - sjc) % 7);
std::string birthdayfdb;
cout <<"On which day are you born? E.G. monday" << endl;
try{
cin >> birthdayfdb;
}
catch(...){
cout << "only english days name are accepted!";
return 0;
}
cin >> birthdayfdb;
bool check = false;
switch(fdb){
case 0:
if(birthdayfdb == "sunday") check = true;
break;
case 1:
if(birthdayfdb == "monday") check = true;
break;
case 2:
if(birthdayfdb == "tuesday") check = true;
break;
case 3:
if(birthdayfdb == "wednesday") check = true;
break;
case 4:
if(birthdayfdb == "thursday") check = true;
break;
case 5:
if(birthdayfdb == "friday") check = true;
break;
case 6:
if(birthdayfdb == "saturday") check = true;
break;
}
if(check){
cout << "This is correct" << endl;
}
else{
cout << "This is incorrect" << endl;
}
}
如您所见,我们使用了布尔变量(bool check;
)来保存用户输入是否经过验证,以便在屏幕上打印(以后)消息。
当然,您可以像这样在Switch Case Statement
内部打印:
case 0:
if(birthdayfdb == "sunday"){
cout << "This is correct" << endl;
}
else{
cout << "This is incorrect"<< endl;
}
但是这会使它变长和变慢。另外,在上述情况下,存在很多重复(每种情况下的if语句几乎相同),而如果输入的验证正常,则首选仅对单个打印进行编码,同时保存在bool变量上。
修复:
”
的{{1}}来表示字符串"
而不是If statement
else if
switch statement
cout << " " <<endl;
变量声明为string
,例如int
int birthdayfdb
称为函数而不是字符串。例如。 birthdayfdb
有关String,Data Types,Switch Case statement和Try/Catch statement的更多信息,请阅读链接的文档!