Problem with input String

I'm writing a program which does some stuffs such as reading information and printing them. My problems here is when I run the code, instead of inputting the ID, the program skips it and move to input the pass. I have no idea how it works. Can anyone help me solve this ? This is my code:
( To be specific, the exercise asks me to input ID and pass which do not contain any spaces )
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
#include<iostream>
#include<string>
using namespace std;
void get(string &name, string &SSN, string &ID, string &pass);
void show(string &name, string &SSN, string &ID, string &pass);
int main()
{
	string name, SSN, ID, pass;
	get(name, SSN, ID, pass);
	show(name, SSN, ID, pass);
	return 0;

}
void get(string &name, string &SSN, string &ID, string &pass)
{
	cout<<"Enter your name: ";
	getline(cin, name);
	cout<<endl;
	
	do
	{
	cout<<"Enter your Social Security number: ";
	cin>>SSN;
	cout<<endl;
	}
	while ((SSN.length()!=11) || (SSN[3]!='-') || (SSN[6]!='-'));
	
	string space=" ";
	
	do
	{
		cout<<"Enter your user ID: ";
		getline(cin, ID);
		cout<<endl;
	}
	while (ID.find(space)<ID.length());
	
	do
	{
		cout<<"Enter your password: ";
		getline(cin, pass);
		cout<<endl;
	}
	while (pass.find(space)<pass.length());
}
void show(string &name, string &SSN, string &ID, string &pass)
{
	cout<<"Student 's name: "<<name<<endl;
	cout<<"Social Security number: ";
	for (int k=0; k<=SSN.length()-1; k++)
	  {
		if (SSN[k]=='-')
		  cout<<"-";
		else
		  cout<<"x";
	  }
	cout<<endl;
	cout<<"ID user: "<<ID<<endl;
	cout<<"Password: ";
	for (int i=1; i<=pass.length(); i++)
	  cout<<"x";
	cout<<endl;
}
	
Last edited on
Typical "getline after formatted input" problem
http://stackoverflow.com/a/21567292
in short: std::getline(std::cin >> std::ws, some_string)
Topic archived. No new replies allowed.