我需要知道为什么我总是在以下上下文中返回 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;
}