我正试图建立一个程序,在这个程序中,它把你的名字和姓氏作为输入(到目前为止),并把它传递给即将到来的函数。现在我想在递归函数中做一些重置变量的事情,因为我想到了下面的情况:如果有人不知怎么写错了自己的名字姓氏,我想给他们一个重新写一次的机会......但是无论用户写错多少次名字,程序都不会删除 "坏 "数据,而是保留它。这就是目前的代码。
#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_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
}
以避免数据的成本和递归的成本。
你已经声明了 name
和 surname
函数中的局部变量。当你再次调用函数时,这些变量的另一个副本会被创建。要使用在递归调用中收集的值,你必须返回它们并将它们存储在调用代码中,但你的递归调用语句中的
name_data();
丢弃返回值。你可以
return name_data();
),非常感谢你们的解决方案。在你们的提示和建议下,我现在改正了它,它按照我的要求工作了。下面是修改后的代码,有兴趣的朋友可以看看。
#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;
}