visual studio 2013-C++LogIn.exe在终端中崩溃
visual studio 2013 - C++ LogIn.exe crashes in terminal
我正试图在Microsoft Visual Studios 2013 中用c++创建登录屏幕
代码工作,但在此之后:
cout << "Create account for user 1, 2, 3, 4, or 5?" << endl;
cin >> menuSelect;
cout << endl;
if (menuSelect == 1)
{
p1.getData1();
}
程序将崩溃并显示:
Unhandled exception at 0x50E1DF58 (msvcp120d.dll) in LogIn.exe:
0xC0000005: Access violation reading location 0x008AD1D4.
任何帮助都会很棒。。。非常感谢。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class account
{
protected:
string username1;
string username2;
string username3;
string username4;
string username5;
string pswd1;
string pswd2;
string pswd3;
string pswd4;
string pswd5;
public:
int menuSelect;
void getData1()
{
cout << "Enter a username: (no spaces)" << endl;
cin >> username1;
cout << endl;
cout << "Enter a password: (no spaces)" << endl;
cin >> pswd1;
cout << endl;
}
void getData2()
{
cout << "Enter a username: (no spaces)" << endl;
cin >> username2;
cout << endl;
cout << "Enter a password: (no spaces)" << endl;
cin >> pswd2;
cout << endl;
}
void getData3()
{
cout << "Enter a username: (no spaces)" << endl;
cin >> username3;
cout << endl;
cout << "Enter a password: (no spaces)" << endl;
cin >> pswd3;
cout << endl;
}
void getData4()
{
cout << "Enter a username: (no spaces)" << endl;
cin >> username4;
cout << endl;
cout << "Enter a password: (no spaces)" << endl;
cin >> pswd4;
cout << endl;
}
void getData5()
{
cout << "Enter a username: (no spaces)" << endl;
cin >> username5;
cout << endl;
cout << "Enter a password: (no spaces)" << endl;
cin >> pswd5;
cout << endl;
}
void showData1()
{
cout << endl;
cout << "Your username is: " << username1 << endl;
cout << endl;
cout << "Your password is: " << pswd1 << endl;
cout << endl;
}
void showData2()
{
cout << endl;
cout << "Your username is: " << username2 << endl;
cout << endl;
cout << "Your password is: " << pswd2 << endl;
cout << endl;
}
void showData3()
{
cout << endl;
cout << "Your username is: " << username3 << endl;
cout << endl;
cout << "Your password is: " << pswd3 << endl;
cout << endl;
}
void showData4()
{
cout << endl;
cout << "Your username is: " << username4 << endl;
cout << endl;
cout << "Your password is: " << pswd4 << endl;
cout << endl;
}
void showData5()
{
cout << endl;
cout << "Your username is: " << username5 << endl;
cout << endl;
cout << "Your password is: " << pswd5 << endl;
cout << endl;
}
string getUsername1()
{
return username1;
}
string getUsername2()
{
return username2;
}
string getUsername3()
{
return username3;
}
string getUsername4()
{
return username4;
}
string getUsername5()
{
return username5;
}
string getPswd1()
{
return pswd1;
}
string getPswd2()
{
return pswd2;
}
string getPswd3()
{
return pswd3;
}
string getPswd4()
{
return pswd4;
}
string getPswd5()
{
return pswd5;
}
};
int accept()
{
int menuSelect;
cout << "Press 1 to do something" << endl;
cin >> menuSelect;
cout << endl;
if (menuSelect == 1)
{
cout << "Do something 1" << endl;
cout << endl;
}
else
{
cout << "Error: Invalid Choice" << endl;
}
return 0;
}
int main()
{
account p1;
ifstream infile("DATA.DAT", ios::binary);
infile.read(reinterpret_cast<char*>(&p1), sizeof(p1));
char answer;
int menuSelect;
cout << "Have you created an account yet? y/n" << endl;
cin >> answer;
if (answer == 'n' || 'N')
{
cout << endl;
cout << "Create account for user 1, 2, 3, 4, or 5?" << endl;
cin >> menuSelect;
cout << endl;
if (menuSelect == 1)
{
p1.getData1();
}
else if (menuSelect == 2)
{
p1.getData2();
}
else if (menuSelect == 3)
{
p1.getData3();
}
else if (menuSelect == 4)
{
p1.getData4();
}
else if (menuSelect == 5)
{
p1.getData5();
}
else
{
cout << "Error: Invalid Choice" << endl;
}
ofstream outfile("DATA.DAT", ios::binary);
outfile.write(reinterpret_cast<char*>(&p1), sizeof(p1));
}
else if (answer == 'y' || 'Y')
{
string nam;
cout << endl;
cout << "Please enter your username:" << endl;
cin >> nam;
cout << endl;
if (nam == p1.getUsername1() || nam == p1.getUsername2() || nam == p1.getUsername3() || nam == p1.getUsername4() || nam == p1.getUsername5())
{
string pwd;
cout << "Please enter your password:" << endl;
cin >> pwd;
cout << endl;
if (pwd == p1.getPswd1() || pwd == p1.getPswd2() || pwd == p1.getPswd3() || pwd == p1.getPswd4() || pwd == p1.getPswd5())
{
cout << "Access Granted" << endl;
cout << endl;
cout << "Press 1 to start program" << endl;
cin >> menuSelect;
cout << endl;
if (menuSelect == 1)
{
return accept();
}
}
else
{
cout << "Access Denied" << endl;
cin >> menuSelect;
cout << endl;
if (menuSelect == 0)
{
return main();
}
}
}
else
{
cout << "Access Denied" << endl;
cin >> menuSelect;
cout << endl;
if (menuSelect == 0)
{
return main();
}
}
}
else
{
cout << endl;
cout << "Error: Invalid Choice" << endl;
}
return 0;
}
您不能以这种方式从文件中读取p1!p1是一个复杂的类,其中字段(字符串)具有非平凡的初始化。如果将p1保存到磁盘,则文件中不会有字符串的文本,但可能(取决于实现)指向实际文本内容的指针。当您读取它们时,您实际上使用指向未分配内存的指针来初始化字符串,从而导致访问冲突。
相关文章:
- 当回溯以零开始时,如何调试崩溃
- 如何在linux终端中同时编译和运行c++代码
- 内联映射初始化的动态atexit析构函数崩溃
- 执行函数时导致崩溃的变量
- 程序崩溃并显示"std::out_of_range"错误
- 终端不会为C++文件创建.exe文件吗
- CoInitialize()在单独的线程上崩溃而不返回
- 使用调试/崩溃报告将应用程序部署到客户端
- 如何处理linux终端中带有负号(-)的C++中的命令行参数
- VS Code "command":"make"与终端窗口中的命令行"make"不同
- 为什么所有C++编译器都会崩溃或挂起此代码
- 为什么lambda在clang上崩溃而不是在gcc上崩溃
- 为什么我的多线程作业队列崩溃
- ExtractIconEx:可以工作,但偶尔会崩溃
- 为什么引用传递会导致此崩溃(C++)
- 试图创建流或fopen时程序崩溃
- Mac os x app bundle 在下载并运行时崩溃,但在终端或更改 Info.plist 时运行良好
- C++程序崩溃终端 - 如何调试
- visual studio 2013-C++LogIn.exe在终端中崩溃
- 我的C++模拟器使终端崩溃