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;
}