How come the following code is not letting me enter the "player_one_name"? Also, is there a way to kind of dynamically set how many characters the getline function can get? For example, instead of having to specify in the beginning, player_one_name [100], the computer gets some characters from the user and allocates proper space for the input?
1 2 3 4 5 6 7 8 9 10 11
int main ()
{
char player_one_name [100];
char player_two_name [100];
cout << "Enter the name of player 1: ";
cin.getline (player_one_name, 100);
cout << "Enter the name of player 2: ";
cin.getline (player_two_name, 100);
return 0;
}
@jlb
The output from my code is like the following. It won't let me enter the name of player 1.
Enter the name of player 1: Enter the name of player 2:
Also, could you possibly write an example of that? Sure, use a std::string instead of the C-string then use the proper version of getline() that works with the std::string.
I am trying to run the program such that I am able to input for player_one_name and player_two_name. So total of two inputs from the user.
@MiiNiPaa
Nope. I just wrote that to try the getline () function. Also, the following code is also having a difficult time handling the getline () function. Line 60 is where the problem rises. I'm not sure why the getline () does that...
#include <iostream>
#include <map>
usingnamespace std;
/*Write a program with two options: register user and log in. Register user allows a new user to create a
login name and password. Log in allows a user to log in and access a second area, with options for "change
password" and "log out". Change password allows the user to change the password, and log out will return
the user to the original screen.*/
void make_account (map<string,string>&account, string user_name, string password)
{
account[user_name] = password;
}
int main ()
{
map<string, string>account;
int option = 0;
string user_name;
string password;
cout << "Enter 1 to register user." << endl;
cout << "Enter 2 log into your account." << endl;
cout << "Enter 3 to exit the program." << endl;
cout << "Enter: ";
cin >> option;
while (option != 3)
{
switch (option)
{
case 1:
{
cout << "Enter username: ";
cin >> user_name;
cout << "Enter password: ";
cin >> password;
make_account (account, user_name, password);
break;
}
case 2:
{
cout << "Enter username: ";
cin >> user_name;
while (account.find (user_name) == account.end ())
{
cout << "Incorrect username. Enter again: ";
cin >> user_name;
}
cout << "Enter password: ";
cin >> password;
while (account[user_name] != password)
{
cout << "Incorrect password. Enter again: ";
cin >> password;
}
cout << "If you would like to change your password, enter 'change'." << endl;
cout << "If you would like to log out, enter 'log out'. " << endl;
cout << "Enter: ";
string user_input;
getline (cin, user_input, '\n');
cout << endl;
if (user_input == "change")
{
cout << "Enter your new password: ";
string new_password;
cin >> new_password;
account[user_name] = new_password;
}
elseif (user_input == "log out")
{
break;
}
else
{
cout << "Invalid input." << endl;
}
break;
}
}
cout << endl;
cout << "Enter 1 to register user." << endl;
cout << "Enter 2 log into your account." << endl;
cout << "Enter 3 to exit the program." << endl;
cout << "Enter: ";
cin >> option;
}
return 0;
}