![](/img/trans.png)
[英]Is there a function in C++/C that asks the user for a file as input by opening a file explorer window for them to select the file?
[英]Login System C++: After entering my string User & Pass, It asks me to re-input them
我的代碼是我要建立的商店,它可以正常運行(注冊系統),直到輸入用戶名和密碼(登錄系統)為止。輸入用戶名后,程序會要求我重新登錄並繼續進行操作(可以肯定是因為這是一會兒真實循環)。 (這是一個項目,因此存在帶有構造函數和類的單獨的不同文件。)
這是我的代碼的一部分:
while(true)
{
cout << "Would you like to register or login?" << endl;
string answer = "";
cin >> answer;
if(answer == "register" || answer == "Register")
{
cout << "What would be your designated username?: " << endl;
string newUser;
cin >> newUser;
for(int i = 0; i < 20; i++)
{
if(customers[i] -> username != newUser)
{
cout << "what would be your designated password?: " << endl;
string newPass;
cin >> newPass;
customers[lastRegisteredID] = new Customer(newUser, newPass);
lastRegisteredID++;
break;
}
}
//^Register Part.
}
if(answer == "login" || answer == "Login")
{
cout << "Your username: " << endl;
string UserAttempt;
cin >> UserAttempt;
for(int j = 0; j < 20; j++)
{
if(customers[j] -> username == UserAttempt)
{
cout << "Username Found!" << endl;
tempCustomer = customers[j];
cout << "Your password: " << endl;
string PassAtempt;
cin >> PassAtempt;
if(tempCustomer -> password == PassAtempt)
{
cout << "Password correct \n Successfully logged in." << endl;
loggedin = true;
break;
}
}
}
}
//^Login part.
}
您要問的問題(正如您對問題的評論所說)來自事實,您break;
只會使您脫離for循環(最里面的循環),而不會進入while循環。 解決此問題的最簡單方法是將while(true)
替換為while(!loggedin)
。 還要注意,您發布的代碼中還有其他一些問題。 因為我不知道這些錯誤是否也在您的最終代碼中,所以我只會列出它們(我發現的幾個):
執行成功的login
操作后,您不會脫離while
循環。
另外,如果注冊的客戶少於20個,您的代碼也會崩潰。
另外,您的代碼允許多個用戶使用相同的用戶名甚至相同的密碼進行注冊。
嘗試類似這樣的方法:
Customer* customers[20];
int numCustomers = 0;
bool loggedin = false;
Customer* findCustomer(const std::string &user)
{
for(int i = 0; i < numCustomers; ++i)
{
if (customers[i]->username == user)
return customers[i];
}
return NULL;
}
...
while (true)
{
std::cout << "Would you like to register or login?" << std::endl;
std::string answer;
std::cin >> answer;
std::transform(answer.begin(), answer.end(), ::tolower);
if (answer == "register")
{
std::cout << "What would be your designated username?: " << std::endl;
string newUser;
std::cin >> newUser;
Customer *cust = findCustomer(newUser);
if (cust)
{
std::cout << "That username is already taken!" << endl;
continue;
}
if (numCustomers >= 20)
{
std::cout << "Too many users are registered!" << endl;
continue;
}
std::cout << "what would be your designated password?: " << std::endl;
std::string newPass;
std::cin >> newPass;
customers[numCustomers] = new Customer(newUser, newPass);
++numCustomers;
continue;
}
if (answer == "login")
{
std::cout << "Your username: " << std::endl;
std::string UserAttempt;
std::cin >> UserAttempt;
std::cout << "Your password: " << std::endl;
std::string PassAttempt;
std::cin >> PassAttempt;
Customer *cust = findCustomer(UserAttempt);
if ((cust) && (cust->password == PassAttempt))
{
std::cout << "Successfully logged in" << std::endl;
loggedin = true;
break;
}
std::cout << "Not logged in!" << std::endl;
continue;
}
std::cout << "Unknown command! Try again" << std::endl;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.