【发布时间】:2021-04-01 06:23:50
【问题描述】:
我的注册登录程序在输出过程中遇到了一些问题:
-
我必须先注册一个新用户并通过(即使我已经将以前的用户名和密码存储在我试图从中检索它的文本文件中),之后只有我可以使用以前的用户名和密码登录,并且重复关闭调试窗口后重新开始调试(如果我在运行程序时直接选择登录,会输出“无效的用户名或密码”)
-
当从新注册的用户名注销时,程序跳转到 int main() 并显示“1. 注册....” 但是从以前的用户名中注销,它会跳转到 void login() 并显示“用户名:”
*注意:最后一个功能尚未完成,但我认为它不会影响它(?)(在我添加void accountPage()tho 之前程序运行良好)
*我不应该使用指针,而且我对 c++ 很陌生
代码有点长,但它只是很多简单的功能,如果有人能在任何地方指出我的错误,我将不胜感激
#include <iomanip>
#include <cctype>
#include <fstream>
#include <string>
using namespace std;
//Global Variables
int Choice1;
int mobile, ic;
string user, pass, name, inUser, inPass;
//Function Prototypes
void register_user();
void login();
void bookRoom();
bool CheckCredentials(string, string);
void accountPage();
int main()
{
cout << "Welcome to Cozy Homes!\n";
cout << "Operating hours: 11am - 10pm Tuesday - Sunday\n\n\n\n";
do {
cout << "\n1. Register\n";
cout << "2. Log In\n";
cout << "3. Exit\n";
cout << "Please enter a number:";
cin >> Choice1;
if (Choice1 == 1)
{
register_user();
}
else if (Choice1 == 2)
{
login();
}
else if (Choice1 == 3)
{
cout << "Exiting now...\n";
return 0;
}
else if (Choice1 < 1 || Choice1 > 3)
{
cout << "Please choose a number from the menu!" << endl;
}
} while (Choice1 != 3);
system("pause");
return 0;
}
//Register page
void register_user()
{
cin.ignore();
cout << "\n\n\n" << "New Username: ";
getline(cin, user);
cout << endl;
cout << "New Password: ";
getline(cin, pass);
cout << endl;
cout << "Full name: ";
getline(cin, name);
cout << endl;
cout << "Mobile Number: ";
cin >> mobile;
cout << endl;
cout << "Ic Number (without \" - \"): ";
cin >> ic;
cout << endl;
cout << "Registered Successfully!" << endl;
cout << endl;
//Store username and password in login file
ofstream l("login.txt", ios::app);
if (!l.is_open()) {
cout << "could not open file \n";
}
l << user << " " << pass << endl;
l << endl;
l.close();
//Store other details in customer file
ofstream c("customer.txt", ios::app);
if (!c.is_open()) {
cout << "could not open file \n";
}
c << user << endl;
c << pass << endl;
c << name << endl;
c << mobile << endl;
c << ic << endl;
c << '\n';
c.close();
}
//Log in page
void login()
{
do
{
cout << "\nUsername: ";
cin >> inUser;
cout << "Password: ";
cin >> inPass;
if (CheckCredentials(inUser, inPass) == true)
{
cout << "\nLogin sucessful!" << endl;
cout << "Welcome, " << inUser << endl;
cout << endl;
accountPage(); // Redirects user to their account page after successfully logged in
}
else
cout << "\nInvalid username or password. " << endl;
} while (CheckCredentials(inUser, inPass) != true);
}
//Validate their username and password
bool CheckCredentials(string inUser, string inPass)
{
string u;
string p;
bool status = false;
ifstream f;
f.open("login.txt");
if (!f.is_open())
{
cout << "Unable to open file!\n";
}
else if (f)
{
while (!f.eof())
{
f >> u >> p;
if (inUser == u && inPass == p)
{
status = true;
}
else
{
status = false;
}
}
}
f.close();
return status;
}
//Account Page
void accountPage()
{
int Choice2;
do
{
cout << "1. Profile\n";
cout << "2. Book a Room\n";
cout << "3. Cancel Booking\n";
cout << "4. Logout\n";
cout << "Please enter a number: ";
cin >> Choice2;
if (Choice2 == 1)
{
}
else if (Choice2 == 2)
{
}
else if (Choice2 == 3)
{
}
else if (Choice2 == 4)
{
cout << "Logging out.....\n\n\n\n";
cout << endl;
}
} while (Choice2 != 4);
}
//Booking page
void bookRoom() {
cout << " ";
}
```
【问题讨论】:
-
注册函数中省略了什么?当您写入文件时,您没有用户值并通过。我必须说,奇怪的练习
-
@PhilipStuyck 省略号还在吗?我现在编辑了代码,你能看到添加的代码吗?
-
在循环中第一个while循环是错误的。那时,如果您从一个新程序开始,inuser 和 inpass 是空的。你需要一个 do while 循环。
-
do { cout << "\nUsername: "; cin >> inUser; cout << "Password: "; cin >> inPass; if (CheckCredentials(inUser, inPass) == true) { cout << "\nLogin sucessful!" << endl; cout << "Welcome, " << inUser << endl; cout << endl; accountPage(); // Redirects user to their account page after successfully logged in } else cout << "\nInvalid username or password. " << endl; } while (CheckCredentials(inUser, inPass) != true);喜欢这样吗?因为我试过这个,但我遇到了同样的问题 -
我不会读那个。但还是错了
标签: c++ registration