我正在尝试构建一个程序,其中它将你的名字和姓氏作为输入(到目前为止),并将其传递给即将到来的函数。 现在我正在尝试在递归函数中重置变量,因为我想到了下面的场景:如果有人不知何故写错了他们的名字/姓氏,我想给他们一个重新写的机会。。。 但是程序不会删除“坏”数据,并且不管用户写错多少次他们的名字,程序都会保留这些数据。 这是到目前为止的代码:
#include <iostream>
using namespace std;
string name_data()
{
string name;
string surname;
cout<<"Welcome to CV/resume creator. Please enter your surname: "<<endl;
cin>>surname;
cout<<endl<<"Now please enter your name: "<<endl;
cin>>name;
bool false_characters=false;
string unallowed="!@#$%^&*()1234567890";
for(int i=0; i<unallowed.size(); i++){
for(int j=0; j<surname.size(); j++){
if(unallowed[i]==surname[j]){
cout<<"Sorry, your surname cannot possibly contain those characters in it (unless you're Elon Musk's heir).\nReturning to beginning..."<<endl;
false_characters=true;
name_data();
}
}
for(int k=0; k<name.size(); k++){
if(unallowed[i]==name[k]){
cout<<"Sorry, your name cannot possibly contain those characters in it (unless you're Elon Musk's heir).\nReturning to beginning..."<<endl;
false_characters=true;
name_data();
}
}
}
string confirmation;
if(false_characters!=true){
cout<<endl<<"Your name is "+surname+" "+name+". Is that correct?"<<endl;
cin>>confirmation;
if(confirmation=="no"){
cout<<"Back to beginning..."<<endl<<endl;
surname.clear();
name.clear();
name_data();
}
}
string full_name=surname+" "+name;
return full_name;
}
int main()
{
cout<<name_data();
return 0;
}
您已经在函数中声明了name
和surname
局部变量。 当您再次调用该函数时,将创建这些变量的另一个副本。 要使用在递归调用中收集的值,您必须返回它们并将它们存储在调用代码中,但是您的递归调用语句
name_data();
删除返回的值。 您可以:
return name_data();
),
非常感谢你们的解决方案。 我纠正了它现在与您的提示和建议,它是工作的我想要的。 下面是who Itesting的修改代码:
#include <iostream>
using namespace std;
string name_data()
{
string name;
string surname;
cout<<"Welcome to CV/resume creator. Please enter your surname: "<<endl;
cin>>surname;
cout<<endl<<"Now please enter your name: "<<endl;
cin>>name;
bool false_characters=false;
string unallowed="!@#$%^&*()1234567890";
for(int i=0; i<unallowed.size(); i++){
for(int j=0; j<surname.size(); j++){
if(unallowed[i]==surname[j]){
cout<<"Sorry, your surname cannot possibly contain those characters in it (unless you're Elon Musk's heir).\nReturning to beginning..."<<endl;
false_characters=true;
return name_data();
}
}
for(int k=0; k<name.size(); k++){
if(unallowed[i]==name[k]){
cout<<"Sorry, your name cannot possibly contain those characters in it (unless you're Elon Musk's heir).\nReturning to beginning..."<<endl;
false_characters=true;
return name_data();
}
}
}
string confirmation;
if(false_characters!=true){
cout<<endl<<"Your name is "+surname+" "+name+". Is that correct?"<<endl;
cin>>confirmation;
if(confirmation=="no"){
cout<<"Back to beginning..."<<endl<<endl;
return name_data();
}
}
string full_name=surname+" "+name;
return full_name;
}
int main()
{
cout<<name_data();
return 0;
}
当您需要更正数据时,调用name_data();
,因此它将执行,然后返回到数据不正确的作用域。 相反,您需要使数据不正确的作用域返回新作用域中的数据,因此请执行如下操作。 使其返回不仅打电话
if(confirmation=="no"){
cout<<"Back to beginning..."<<endl<<endl;
return name_data();
}
请注意,在所做的每个调用堆栈中都有独立的数据,因此不需要clear()
。
最好将递归样式更改为
bool repeatCond = true;
while(repeatCond){
//your body
//ask the user if this is satisfying and change the condition
}
以避免数据成本和重复成本。