本文介绍了如何创建一个名为播放器用户名的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 你好, 我试图创建一个测试登录程序。 (我知道这不安全但是我正在尝试创建一些要学习的东西。) 这一行给出了一个错误:file.open(用户名+。txt); 我认为+在那条线上不起作用,但我不确定。 这是错误: c:\program files(x86)\ codeblocks \mingw \bin\..\lib\gcc \mingw32 \4.7.1 \ include \ c ++ \ stream | 702 |注意:void std :: basic_ofstream< _CharT,_Traits> :: open(const char *,std :: ios_base :: openmode)[with _CharT = char; _Traits = std :: char_traits< char> ;; std :: ios_base :: openmode = std :: _ Ios_Openmode] | 这是代码: #include < iostream > #include < string > #include < fstream > 使用 命名空间标准; 枚举 MainProgramParts {SUCCESS,FAILED,STOP,OPTIONS}; MainProgramParts选项(); MainProgramParts success(); MainProgramParts失败(); int main() { MainProgramParts nextPart = OPTIONS; while (nextPart!= STOP){ switch ( nextPart){ case 选项: nextPart = options(); break ; case 成功: nextPart = success(); break ; case FAILED: nextPart = failed(); break ; } } } MainProgramParts选项(){ int choice; cout<< 1:注册<< ENDL; cout<< 2:登录<< endl<< ENDL; cin>>选择; if (choice == 1 ) { string username; 字符串密码; cout<< 用户名:; cin>>用户名; cout<< 密码:; cin>>密码; ofstream文件; file.open(用户名+ .txt); file<<用户名<< endl<<密码; file.close(); main(); if (choice == 2 ) { string username; 字符串密码; string un; string pw; cout<< 用户名:; cin>>用户名; cout<< 密码:; cin>>密码; ifstream read(用户名+ .txt); getline(读取,取消); getline(读,pw); if (un == username&& pw == password) { 返回成功; } else { return FAILED; } } } } MainProgramParts success(){ cout<< 已成功登录!; } MainProgramParts失败(){ cout<< 密码/用户名不正确!; } 谢谢, ~ Steff 解决方案 Hello,I tried to create a test login program.(I know this isn't safe but I'm trying to create some things to learn).This line gives an error: file.open(username + ".txt");I think the + in that line doesn't work but I'm not sure.This is the error:c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\fstream|702|note: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]|This is the code:#include <iostream>#include <string>#include <fstream>using namespace std;enum MainProgramParts{ SUCCESS, FAILED, STOP, OPTIONS };MainProgramParts options();MainProgramParts success();MainProgramParts failed();int main(){ MainProgramParts nextPart = OPTIONS; while(nextPart != STOP){ switch(nextPart){ case OPTIONS: nextPart = options(); break; case SUCCESS: nextPart = success(); break; case FAILED: nextPart = failed(); break; } }}MainProgramParts options(){ int choice; cout << "1: Register" << endl; cout << "2: Login" << endl << endl; cin >> choice; if(choice == 1) { string username; string password; cout << "Username: "; cin >> username; cout << "Password: "; cin >> password; ofstream file; file.open(username + ".txt"); file << username << endl << password; file.close(); main(); if(choice == 2) { string username; string password; string un; string pw; cout << "Username: "; cin >> username; cout << "Password: "; cin >> password; ifstream read(username + ".txt"); getline(read, un); getline(read, pw); if(un == username && pw == password) { return SUCCESS; } else { return FAILED; }}}}MainProgramParts success(){ cout << "Successfully logged in!";}MainProgramParts failed(){ cout << "Incorrect password / username!";}Thanks,~Steff 解决方案 这篇关于如何创建一个名为播放器用户名的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 16:18