visual studio 2013-C++LogIn.exe在终端中崩溃

visual studio 2013 - C++ LogIn.exe crashes in terminal

本文关键字:终端 崩溃 exe studio 2013-C++LogIn visual      更新时间:2023-10-16

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