我正在爲我的C++課程製作一個程序,以使用不同的函數驗證日期,但主要是布爾值。我的問題是,它不會在事件發生時給出錯誤。我已經嘗試使用else命令而不是將返回false;沒有別的,但它似乎沒有改變任何東西。這裏是儘管代碼:無法獲得布爾值來返回false C++
int main()
{
char Data[80];
int Month,Day,Year;
int *pMonth,*pDay,*pYear;
pMonth = &Month;
pDay = &Day ;
pYear = &Year ;
cout << "\n\t\tGive me date : ";
cin >> Data;
trial();
PauseScreen(28,20,3);
return 0;
}
void SetCursorPosition(int X, int Y)
{
COORD XY = { Y,X };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),XY);
}
void SetTextColor(int Color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), Color);
}
void ClearScreen()
{
system("cls");
}
void PauseScreen(int x, int y, int color)
{
SetCursorPosition(x,y);
SetTextColor(color);
system("pause");
}
int InputValues(char *A, int *pM, int *pD, int *pY)
{
char Buffer[10];
Buffer[0] = A[0];
Buffer[1] = A[1];
Buffer[2] = '\0';
*pM = atoi(Buffer);
Buffer[0] = A[3];
Buffer[1] = A[4];
Buffer[2] = '\0';
*pD = atoi(Buffer);
Buffer[0] = A[6];
Buffer[1] = A[7];
Buffer[2] = A[8];
Buffer[3] = A[9];
Buffer[4] = '\0';
*pY = atoi(Buffer);
return strlen(A);
}
bool ValidateMonth(int A)
{
if (A > 0 && A < 13)
{
return true;
}
return false;
}
bool ValidateDay(int day,int month)
{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 9|| month == 10|| month == 12 && (day > 0 && day < 32))
{
return true;
}
return false;
}
bool ValidateDayTwo(int day,int month)
{
if (month == 4 || month == 6 || month == 8 || month == 11 && (day > 0 && day < 31))
{
return true;
}
return false;
}
void trial()
{
if(ValidateDay && ValidateDayTwo && ValidateMonth)
{
SetCursorPosition(10,10);
cout << "Date is Valid";
}
else
{
SetCursorPosition(10,10);
cout << "You done messed up BALAKI";
}
}
哪些方法在您預期它們時不會返回false? –
'if(ValidateDay && ValidateDayTwo && ValidateMonth){...}'您需要將一些參數傳遞給您的呼叫。 – pzaenger
'if(condition)return true;否則返回false;'。爲什麼不只是'返回條件;'? –