我无法从文件中读取以确认用户名
I cant read from files to confirm username
我正在尝试制作一个程序,您必须使用用户名登录(为了简单起见,我首先使用用户名,稍后我会添加密码(为了使它工作,我想将用户名写入一个文件(稍后我将加密(,当您要登录时,它将检查文件用户名是否正确。但是当我输入错误的用户名时,它会自动登录并且不会要求创建新帐户。如果我使用正确的用户名登录,也是如此。我做错了什么?(我是新手,这是我的第一个半体面的程序,所以如果我做错了什么,请不要太苛刻。
所以。我做了一些我理解的事情。这就是我现在得到的:但是,现在它不会将新用户名写入Usernames_and_passwords文件。我非常困惑...
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <fstream>
using namespace std;
string user_input;
string birth_year;
string user_age;
string current_user_profile, current_user_password;
string username, password;
string newprofile;
string makenewusername, makenewpassword;
void TalkToAi() { //VOID TALKTOAI
while (true) {
cout << "write something: ";
cin >> user_input;
transform(user_input.begin(), user_input.end(), user_input.begin(), ::tolower); //TRANSLATES ALL UPPERCASE LETTERS TO LOWER CASE SO THE SYSTEM CAN UNDERSTAND!
cout << user_input << "n"; //###FOR TESTING PORPOSES!!!###
//IF LIBRARY!
if (user_input == "what's my age?" || user_input == "count my age" || user_input == "whats my age?") {
//###CONTINUE HERE!!!###
}
}
}
void StartUp() { //VOID UPONSTARTUP (WHAT TO DO, ALSO READS DIFFRENT PROFILES AND PASSWORDS FOR LOGIN)
cout << "what profile should i load?" << "n" << "profile name: ";
cin >> current_user_profile;
fstream Myfile;
Myfile.open("Usernames_and_passwords.txt");
if (Myfile.is_open()) {
while (getline (Myfile, username) ) {
if (username == current_user_profile) {
cout << "n" << "Hello, " << username << "n";
break;
}
}
if (username != current_user_profile) {
cout << "wrong username or username unfortunately not found.n";
cout << "shall i create a new profile? Yes/No: ";
cin >> newprofile;
if (newprofile == "Yes" || newprofile == "yes") {
cout << "new profile username: ";
cin >> makenewusername;
Myfile << makenewusername << endl;
}
}
}
}
它不会写入文件,因为一旦你使用 getline(( 读完文件,就会设置 eof 标志。您需要使用以下方法打开文件:
Myfile.open("Usernames_and_passwords.txt",fstream::in | fstream::out | fstream::app);
这会告诉程序打开文件进行读取和写入。 fstream::app 告诉程序将文本附加到文件的末尾。
然后,为了在eof被击中后重置,您可以这样做
Myfile.clear();
Myfile.seekg(0, ios::beg);
这将清除 eof 标志,并将指针移回文件的开头。在此之后,您可以写入文件。
其他几点评论:您的循环已断开:如果文件为空,它将不起作用,如果输入的用户名在第二行而不是第一行,它将写入重复的用户名。
以下是函数的修改版本:
void StartUp() { //VOID UPONSTARTUP (WHAT TO DO, ALSO READS DIFFRENT PROFILES AND PASSWORDS FOR LOGIN)
cout << "what profile should i load?" << "n" << "profile name: ";
cin >> current_user_profile;
fstream Myfile;
Myfile.open("Usernames_and_passwords.txt",fstream::in | fstream::out | fstream::app);
if (Myfile.is_open()) {
while (getline (Myfile, username) ) {
if (username == current_user_profile) {
cout << "n" << "Hello, " << username << "n";
return;
}
}
cout << "wrong username or username unfortunately not found.n";
cout << "shall i create a new profile? Yes/No: ";
cin >> newprofile;
if (newprofile == "Yes") {
cout << "new profile username: ";
cin >> makenewusername;
Myfile.clear();
Myfile.seekg(0, ios::beg);
Myfile << makenewusername << endl;
}
}
}
我还建议让 StartUp 返回一个关于操作是否成功的布尔值,这样您就可以决定终止程序。 例如,如果用户输入"否"。
相关文章:
- C++如何通过用户输入删除列表元素
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 使用用户定义函数的字符串反转
- 用户定义函数中的指针和输入
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 如何只允许用户输入正整数
- 如何确认我的constexpr表达式实际上已经在编译时执行
- C++如何计算用户输入的数字中的偶数位数
- 通过for循环使用用户输入填充列表
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- 在C++中,我如何接受不同于同一行的用户输入
- 当用户超过按钮点击限制时报告
- Visual C++(VS2017)中用户定义的转换不明确
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 当用户键入分隔符时,停止getline()输入
- C++,在用户确认后继续向文件添加记录
- FireBreath:如何安全地向用户显示确认对话框