gpt4 book ai didi

C++ LogIn.exe 在终端中崩溃

转载 作者:行者123 更新时间:2023-11-28 06:12:07 26 4
gpt4 key购买 nike

我正在尝试在 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 保存到磁盘,则文件中不会有字符串文本,但可能(取决于实现)指向实际文本内容的指针。当您阅读它们时,您实际上使用指向未分配内存的指针初始化了字符串,因此发生了访问冲突。

关于C++ LogIn.exe 在终端中崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31057531/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com