user3576250
user3576250

Reputation: 11

Can't get boolean to return false C++

I am making a program for my C++ course to validate the date using different functions but mostly boolean. My problem is that it won't give false when it is. I have tried it using the else command instead of leaving the return false; without the else but it didn't seem to change anything. Here is the code though:

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";
    }
}

Upvotes: 1

Views: 1766

Answers (3)

Eutherpy
Eutherpy

Reputation: 4571

You didn't call the functions correctly (well, you actually didn't call them at all) - there are no arguments:

if(ValidateDay(?) && ValidateDayTwo(?) && ValidateMonth(?))

For example, your ValidateDayTwo function takes two parameters.

The fact that the functions' return types are bool doesn't change anything, this doesn't do what you think it does. The name of the function is a pointer to the function itself, and you didn't get false as expected, because that pointer is not NULL.

Upvotes: 1

fredoverflow
fredoverflow

Reputation: 263078

bool ValidateMonth(int A)
{
    if ( A > 0 && A < 13 )
    {
        return true;
    }

    return false;    
}

There is no need to say "If the condition is true, return true; otherwise, return false". You can simply return the result of evaluating the condition:

bool ValidateMonth(int A)
{
    return A > 0 && A < 13;
}

Upvotes: 1

Alan
Alan

Reputation: 46813

You're not actually calling your functions in your if statement. ValidDay, ValidDayTwo, ValidMonth

if(ValidateDay && ValidateDayTwo && ValidateMonth)

Instead you'll need to invoke the function by passing in arguments

if(ValidateDay(somearg1) && ValidateDayTwo(somearg2) && ValidateMonth(somearg2))

Upvotes: 4

Related Questions