I wrote following function
void validateUser(void)
{
string uName;
string uPassword;
char c;
map <char*, char*> authMap;
authMap["balaji"] = "balaji";
authMap["rohan"] = "rohan";
cout << "Please Enter your user name :";
cin >> uName;
cout << "Please Enter your password :";
// initTermios(0);
while((c = getchar()) != '\n')
{
uPassword.push_back(c);
}
cout << "YOU Entered :: "<< uPassword <<std::endl;
}
When i executed the above function, i found that after entering user name i press enter key then control does not enter into while loop even if i did not entered any password string.
Ant solution to above ? Thanks in advace.
The newline character entered as a result of entering the username is still in standard input, and it is this that is read next thus the loop terminating condition is immeditately false. You need to skip past the newline character, which can be achieved using
ignore():Suggest not mixing use of C and C++ IO functions (I am unsure of the exact rules how this interact but I prefer to avoid it):
or just use
std::getline():As mentioned in my comment, the map will be keyed by the address of the string literal, not the string content. Suggest using
map<std::string, std::string>instead.