Getline Question

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; 
}
Last edited on
How come the following code is not letting me enter the "player_one_name"?

The program should accept entry for both player1 and player2.

Also, is there a way to kind of dynamically set how many characters the getline function can get?

Sure, use a std::string instead of the C-string then use the proper version of getline() that works with the std::string.
@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.
Last edited on
How are you trying to run the program?

And is that all code you have in your program? Is there anything like cin >> x; before that?
@jlb

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...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <map>
using namespace 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; 
				} 
				else if (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; 
}
the following code is also having a difficult time handling the getline () function
This is because you have cin >> foo before that. Classic problem of unformatted input after formatted.

http://stackoverflow.com/a/21567292
Topic archived. No new replies allowed.