-1

I am attempting to write a simple login form. I want to store the list of login accounts in a STL map.

when retrieving text values from text boxes on the form. The boxes return "String^"

So what I have is:

map <String^, String^> NamePassList;
typedef pair<String^, String^> StringPair; 

string line, usrName, password;
usrName = password = "";
ifstream ifs("login.in");
if (ifs.is_open()){
    while (!ifs.eof()){
        getline(ifs,line);
        bool endofusername = false;
        for (int i = 0; i < line.length(); i++){
            if (line[i] == ' '){
                endofusername = true;
            }
            if (!endofusername){
                usrName += line[i];
            }
            else{
                password += line[i];
            }
        }
        String ^ temp1 = gcnew String(usrName.c_str());
        String ^ temp2 = gcnew String(password.c_str());
        NamePassList.insert(StringPair(temp1, temp2));
    }
    ifs.close();
}

String ^ UserName = txtUserName->Text;
String ^ Password = txtPassword->Text;

map<String^, String^>::iterator nameItor;
nameItor = NamePassList.find(UserName);

if (nameItor->first == UserName){
    if (nameItor->second == Password){
        MessageBox::Show("Sucsess!", "log", MessageBoxButtons::OK);
    }
    else
        MessageBox::Show("Fail!", "log", MessageBoxButtons::OK);
}
else
{
    MessageBox::Show("Fail!", "log", MessageBoxButtons::OK);
}

when compiled the error i get of from the "map class" and utility.

This seams like the best way to do it, but everything I try gives me a new error.

Any help would be great!! thanks all.

P0W
  • 46,614
  • 9
  • 72
  • 119

1 Answers1

0

To possibly reduce some errors, try and simplify your code using std::string.

Given the option not to use the managed String type, I'd opt for std::string. Since you're using std::string already, try that type for NamePassList. Also, typedef the map. Then for inserts, use value_type.

typedef map<string, string> StringMap;
StringMap NamePassList;

to insert:

StringMap::value_type vt(usrName, password);
StringMap::_Pairib pair = NamePassList->insert(vt);
if (pair.second == false)
{
   ... problems... key already exists ....
}

And also for the search:

StringMap::iterator iter = NamePassList.find(UserName);
if (iter != NamePassList.end())
{
   ...found...
}

You don't want to "use" the iter until you know it's valid by testing against end().

bvj
  • 3,294
  • 31
  • 30