1

I am making a small program. First I made a Header File:

private:

    string UserName, Password;
public:


    void setUN(string);
    void setP(string);

    string getUN();

Then in my cpp file:

    void UserInfo::setUN(string un){

    UserName = un;
}


    string UserInfo::getUN(){

    return UserName;
}

After that in my main I make a object:

UserInfo addUser[100];

to add users

cout<<"Enter Username : ";

    getline(cin,tUN);
    addUser[0].setUN(tUN);

After that in my other function void LoginScreen()

I made the same object:

UserInfo addUser[100];

string EUN, EP;
system("cls");
cout<<"Enter Username : ";
cin>>EUN;
cout<<endl;
cout<<"Enter Password : ";
//cin>>EP;

for( int a = 0; a <= 100; a++){
    if (EUN == addUser[a].getUN()){
        system("cls");
        cout<<"OMG HELP MEEE ";
        break;
    }
}

It works fine when till get to this for loop and gives this error:

std::bad_alloc at memory location 0x002b123c

Can you tell me what the error means and how can I get rid of this.

4

1 回答 1

3

UserInfo addUser[100]; has elements indexed from 0 - 99.

So fix:-

for( int a = 0; a <= 100; a++){
                ^^This should be a < 100
}
于 2013-09-22T14:18:34.943 回答