0

I am trying to create a LogIn screen in c++ in Microsoft Visual Studios 2013

the code works but after this:

    cout << "Create account for user 1, 2, 3, 4, or 5?" << endl;

    cin >> menuSelect;

    cout << endl;

    if (menuSelect == 1)
    {
        p1.getData1();
    }

The program will crash and show this:

Unhandled exception at 0x50E1DF58 (msvcp120d.dll) in LogIn.exe: 
0xC0000005: Access violation reading location 0x008AD1D4.

Any help will be great... Thank you!!

#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;
}
pnuts
  • 58,317
  • 11
  • 87
  • 139
Willm132
  • 17
  • 9
  • possible duplicate of [Serializing a class which contains a std::string](http://stackoverflow.com/questions/7046244/serializing-a-class-which-contains-a-stdstring) – NathanOliver Jun 25 '15 at 18:08
  • `else if (answer == 'y' || 'Y')` This does not do what you think it does. C++ does not speak in English vernacular. You have to specify on both sides of the `||` what you are comparing. – PaulMcKenzie Jun 25 '15 at 18:18
  • Also, you have repeated code. I can't imagine if you had 100 users to maintain. Why not use an array of `std::string`? And of course, the ubiquitous use of this construct: `infile.read(reinterpret_cast(&p1), sizeof(p1));` You're not the first one to do this, as a matter of fact, it seems to be an epidemic. Where are you getting code like this from, because I've been trying to find the source of this erroneous piece of code. – PaulMcKenzie Jun 25 '15 at 18:20

1 Answers1

1

You can't read p1 from file that way! p1 is a complex class where fields (strings) have non trivial initializations. If you save p1 to disk then you won't have text of string inside the file, but probably (depend on implementation) pointers to actual text content. When you read them you actually initialize the string with pointers to unallocated memory, hence the access violation.

marom
  • 5,064
  • 10
  • 14
  • The code works fine and it will allow me to login with accounts created. It just crashes when the is no more code to implement. What would you suggest me to do? – Willm132 Jun 25 '15 at 18:11
  • You need to implement a more complex scheme for saving/loading your struct. For example place one string per line (assuming your strings do not contain newlines) – marom Jun 25 '15 at 18:16