更新时间:2023-12-02 12:50:10
int check(字符串用户名)
{
cin>>用户名;
字符串数据1,数据2;
ifstream infile;
infile.open(profile.txt,ios :: app); //< - 你要将什么附加到输入文件?
getline(infile,data1);
if(username == data1)
{
cout<< 用户名退出<< endl;
cout<< 输入新用户名:;
cin>>用户名;
返回检查(用户名); //< - 以递归方式调用函数(文件一次又一次打开)不是一个好主意
}
其他
{
}
}
int check(string username)
{
cin >> username;
string data1, data2;
ifstream infile;
infile.open("profile.txt", ios::app); //<-- What are you going to append to a input file?
getline(infile, data1);
if (username == data1)
{
cout << "Username exits" << endl;
cout << "Enter new username: ";
cin >> username;
return check(username);//<-- it is NOT a good idea to call the function recursively (the file is open again and again)
}
else
{
}
}
我的建议:执行检查
功能只是为了检查,然后再问一遍(在调用代码中)用户输入用户名如果失败。
我的一般建议:不要使用转到
,使用 C ++
功能。
My advice: implement the check
function simply for checking, then ask again (in the calling code) the user to enter the username if it fails.
My general advice: don't use goto
, use the C++
features.