我需要知道为什么我总是在以下情况下返回NULL(例如,无论我说"是"或者"否",对于任何问题我我得到null(我假设因为问题进程在运行时重复),即使我在main中说不。我试图让它做到这一点:对于所有无效的答案,我返回null,如果null开始在问题上进程,如果回答有效,我返回true或false,如果true,则返回true,如果是false quit program。
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;
}
答案 0 :(得分: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
只能是true
或false
,因此根本不需要检查NULL
。 (参见上面的解决方案。)