質問

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.

役に立ちましたか?

解決

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

So fix:-

for( int a = 0; a <= 100; a++){
                ^^This should be a < 100
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top