当答案 == true 时如何使 void 不是 return
How to make void not return when answer == true
我制作了这个程序,当我输入“是”时,它应该结束我的程序,而不是它在等我说更多的话,然后它随我的 void nottrue() 一起出现;我应该怎么做才能避免这种情况?这是我的代码
#include <iostream>
using namespace std;
void CharacterWorld();
void nottrue();
int main()
{
CharacterWorld();
nottrue();
return 0;
}
void CharacterWorld()
{
string CharacterName;
int CharacterAge;
string yesorno;
cout << " Hi, welcome to the Vanish World! " << endl;
cout << " What's your name champion? " << endl;
cin >> CharacterName;
cout << " ...And what's your age? " << endl;
cin >> CharacterAge;
cout << " ... So your name is " << CharacterName << " and your age is " << CharacterAge << " Is that right?" << endl;
cin >> yesorno;
if (yesorno == "yes")
{
cout << " Okey! so let's start your journey champion!" << endl;
}
else
{
cout << " SO what's your name then ??" << endl;
return nottrue();
}
}
void nottrue()
{
string CharacterName;
int CharacterAge;
string yesorno;
cin >> CharacterName;
cout << " and what's your age?" << endl;
cin >> CharacterAge;
cout << " ...Okey, already. Your name is " << CharacterName << " and your age is " << CharacterAge << endl;
}
虽然return nottrue()
有效,但它只是一个函数调用,因为调用者和被调用函数都没有返回值。您不会以任何方式改变流量。
您必须使用函数返回的结果来控制流程。例如
bool CharacterWorld()
{
//...
if (yesorno == "yes")
{
cout << " Okey! so let's start your journey champion!" << endl;
return true;
}
else
{
cout << " SO what's your name then ??" << endl;
return false;
}
}
int main()
{
if(!CharacterWorld())
nottrue();
return 0;
}
还有预定义的exit()
函数可以退出程序。
我制作了这个程序,当我输入“是”时,它应该结束我的程序,而不是它在等我说更多的话,然后它随我的 void nottrue() 一起出现;我应该怎么做才能避免这种情况?这是我的代码
#include <iostream>
using namespace std;
void CharacterWorld();
void nottrue();
int main()
{
CharacterWorld();
nottrue();
return 0;
}
void CharacterWorld()
{
string CharacterName;
int CharacterAge;
string yesorno;
cout << " Hi, welcome to the Vanish World! " << endl;
cout << " What's your name champion? " << endl;
cin >> CharacterName;
cout << " ...And what's your age? " << endl;
cin >> CharacterAge;
cout << " ... So your name is " << CharacterName << " and your age is " << CharacterAge << " Is that right?" << endl;
cin >> yesorno;
if (yesorno == "yes")
{
cout << " Okey! so let's start your journey champion!" << endl;
}
else
{
cout << " SO what's your name then ??" << endl;
return nottrue();
}
}
void nottrue()
{
string CharacterName;
int CharacterAge;
string yesorno;
cin >> CharacterName;
cout << " and what's your age?" << endl;
cin >> CharacterAge;
cout << " ...Okey, already. Your name is " << CharacterName << " and your age is " << CharacterAge << endl;
}
虽然return nottrue()
有效,但它只是一个函数调用,因为调用者和被调用函数都没有返回值。您不会以任何方式改变流量。
您必须使用函数返回的结果来控制流程。例如
bool CharacterWorld()
{
//...
if (yesorno == "yes")
{
cout << " Okey! so let's start your journey champion!" << endl;
return true;
}
else
{
cout << " SO what's your name then ??" << endl;
return false;
}
}
int main()
{
if(!CharacterWorld())
nottrue();
return 0;
}
还有预定义的exit()
函数可以退出程序。