0

次のコンテキストで常に NULL を返す理由を知る必要があります (たとえば、null を取得している質問に対して「はい」または「いいえ」と言っても (質問プロセスが繰り返されるためだと思います)私はそれを実行しようとしています: すべての無効な回答に対して null を返します. null の場合は質問プロセスを最初からやり直します. false の場合はプログラムを終了します。

 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のみが可能であり、のチェックはまったく必要ありません。(上記の私の解決策を参照してください。)truefalseNULL

于 2013-11-14T00:12:54.503 に答える