0

我需要知道为什么我总是在以下上下文中返回 NULL(例如,无论我说“是”还是“否”,对于我得到 ​​null 的任何问题(我假设是因为问题过程在它运行),即使我在 main 中说不。我试图让它这样做:对于所有无效的答案,我返回 null,如果 null 从问题过程开始,如果答案有效,我返回 true 或 false,如果为 true继续,如果错误退出程序。

 bool question1()
{
    string answer2;
    cout << "Are you 18 or older and have a valid Driver's License? Yes or No: ";
    getline( cin, answer2);
    transform(answer2.begin(), answer2.end(), answer2.begin(), ::tolower);
    cout << endl;
    if( answer2 == "yes")
    {
        cout << "Alright! " << endl << "You are set for registration. Please fill out the registration form. ";
        return true;
    }

    else if( answer2 == "no")
    {


        cout << "Do you know someone else who is 18 or older that can register? Yes or No ";
        getline( cin, answer2); 
        transform(answer2.begin(), answer2.end(), answer2.begin(), ::tolower);
        if( answer2 == "yes")
        {
            cout << "Good, then please continue the process in their place. Please fill out the registration form";
            return true;
        }
        else if( answer2 == "no")
        { 
            cout << "Please come back later when you have the appropriate personel";
            return false;
        }



        else
        {
            cout << "The answer given was invalid. Please give a valid answer. " << endl << endl ;
            return NULL;
        }

    }



    else
    {
        cout << "The answer given was invalid. Please give a valid answer. " << endl << endl;

        return NULL;


        }

 void registerPerson( array< string, nameSize > namesOfPeople, array< string, idSize > idlen)
{
    string pName;
    string dLicense;
    static int i = 0;
    static int b = 0;
    static int c = 0;
    unsigned int x = 1;


    cout << endl << endl << "REGISTRATION FORM:" << endl << endl << "------------------" << endl;
    cout << "Please" << endl << "enter the following: \n \n";
    cout << "Name: ";
    getline( cin, pName );

    for ( int j = i; j<=800; ++ j )
    {

        namesOfPeople[j] = pName;
        cout << namesOfPeople[j];
        i = i + 1;

        break;

    }

        cout << endl;

    while( x = 1)
    {
        cout << "Driver\'s Licence Number( Must be 9 characters long, no dashesh ): ";
        cin >> dLicense;
        if ( dLicense.length() < 9 || dLicense.length()> 9 )
        {
            cout << "The entered number was invalid. Please try again";
        }

        else
        {
            for ( int a = i; c<=800; ++ a )
                {

                    idlen[a] = dLicense;
                    cout << idlen[a];
                    c = c + 1;

                    break;
                }



        }
    }
}





}

{

    int main()
            array< string, nameSize > names = {};
            array< string, idSize > ids = {};

    carShare mycarShare1;










    carShare mycarShare2;
    mycarShare2.welcomeMessage();

    mycarShare2.question1();

    if( mycarShare1.question1() == NULL)
    {
        mycarShare1.question1();
    }

    else if( mycarShare1.question1() == true)
    {
    mycarShare1.registerPerson(names, ids);
    }


    else
    {
        return 0;
    }




    system( "PAUSE" );
    return 0;

}
4

1 回答 1

0

我可能会对其进行更改以处理其内部的无效输入question1()。重新开始这个问题的一种简单方法是让question1自己在无效输入上再次调用,直到它得到一个像这里这样的有效输入(我在所有更改的地方都放了评论。):

bool question1()
{
    string answer2;
    cout << "Are you 18 or older and have a valid Driver's License? Yes or No: ";
    getline( cin, answer2);
    transform(answer2.begin(), answer2.end(), answer2.begin(), ::tolower);
    cout << endl;
    if( answer2 == "yes")
    {
        cout << "Alright! " << endl << "You are set for registration. Please fill out the registration form. ";
        return true;
    }

    else if( answer2 == "no")
    {


        cout << "Do you know someone else who is 18 or older that can register? Yes or No ";
        getline( cin, answer2); 
        transform(answer2.begin(), answer2.end(), answer2.begin(), ::tolower);
        if( answer2 == "yes")
        {
            cout << "Good, then please continue the process in their place. Please fill out the registration form";
            return true;
        }
        else if( answer2 == "no")
        { 
            cout << "Please come back later when you have the appropriate personel";
            return false;
        }



        else
        {
            cout << "The answer given was invalid. Please give a valid answer. " << endl << endl ;
            return question1(); // call itself again on invalid input.
        }

    }



    else
    {
        cout << "The answer given was invalid. Please give a valid answer. " << endl << endl;

        return question1(); // call itself again on invalid input.


    }


}

int main()
{
    array< string, nameSize > names = {};
    array< string, idSize > ids = {};

    carShare mycarShare1;

    carShare mycarShare2;
    mycarShare2.welcomeMessage();

    bool answer = mycarShare2.question1();

/*  if( answer == NULL) we don't need this anymore if we handle errors inside question1()
    {
        mycarShare1.question1();
    }
*/
    if( answer == true) // notice the change to if instead of else if here.
    {
        mycarShare1.registerPerson(names, ids);
    }


    else
    {
        return 0;
    }


    system( "PAUSE" );
    return 0;

}

另一种不使用递归的方法是do... while循环,这是一个只有question1()循环的版本:

bool question1()
{
    string answer2; // has to be declared before the loop.
    do
    {
        cout << "Are you 18 or older and have a valid Driver's License? Yes or No: ";
        getline( cin, answer2);
        transform(answer2.begin(), answer2.end(), answer2.begin(), ::tolower);
        cout << endl;
        if( answer2 == "yes")
        {
            cout << "Alright! " << endl << "You are set for registration. Please fill out the registration form. ";
            return true;
        }

        else if( answer2 == "no")
        {


            cout << "Do you know someone else who is 18 or older that can register? Yes or No ";
            getline( cin, answer2); 
            transform(answer2.begin(), answer2.end(), answer2.begin(), ::tolower);
            if( answer2 == "yes")
            {
                cout << "Good, then please continue the process in their place. Please fill out the registration form";
                return true;
            }
            else if( answer2 == "no")
            { 
                cout << "Please come back later when you have the appropriate personel";
                return false;
            }

        }

    }while(answer2 != "yes" && answer2 != "no"); // loop as long as the input is invalid.

}

======================================

编辑:

作为对其多次打印的澄清,这是原始代码中的罪魁祸首:

mycarShare2.question1();

if( mycarShare1.question1() == NULL)
{
    mycarShare1.question1();
}

else if( mycarShare1.question1() == true)
{
    mycarShare1.registerPerson(names, ids);
}


else
{
    return 0;
}

mycarShare2.question1()是一个函数调用,你在那部分调用了你的函数 3 次。您想要的可能是将其保存在 bool 变量中,然后仅在 if/else 语句中进行测试,如下所示:

bool answer = mycarShare2.question1();

if( answer == NULL)
{
    mycarShare1.question1();
}

else if( answer == true)
{
    mycarShare1.registerPerson(names, ids);
}


else
{
    return 0;
}

注意:这只是为了展示您可能期望它在原始代码中执行的方式,而不是解决方案。如前所述bool,只能是trueorfalse并且NULL根本不需要检查。(请参阅我上面的解决方案。)

于 2013-11-14T00:12:54.503 回答