Ignoring input after space

Initially my problem was if I input something like "asdf qwer" it would read "asdf" as input for UserID and "qwer" as input for password. So I am trying to make it so it ignores everything after the space and will only read "asdf". After adding cin.ignore, if I input "asdf qwer" it does what I want but instead of reading "asdf" it reads "qwer". How do I get it to read asdf and ignore everything after the space instead of ignoring everything before the space?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cout << endl << "Please enter UserID: ";
cin.ignore(200, ' ');
cin >> inUserID; 
cout << inUserID << endl;   //just to check what it reads the input as
	
cout << "Please enter Password: ";
cin >> inPassword;

cout << "Please enter PIN: ";
cin >> inPIN;		
while (!cin)
{
	cin.clear();
	cin.ignore(200,'\n');
	cout << "Please re-enter correct PIN: ";
	cin >> inPIN;
}
instead of the ignore before cin >> inUserID do cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); after the cin
Hi @jigaboo247,
i hope this help;

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
//str_space.cpp
//##

#include <iostream>
#include <string>


using namespace std;



int main(){

string userID,userPSS;

        cout<<"Enter user ID: ";
        cin>>userID;
        cin.ignore(100,'\n');

        cout<<"\nEnter Password: ";
        cin>>userPSS;
        cin.ignore(100,'\n');

        cout<<"\nUser ID: "<<userID<<endl;
         cout<<"\nUser Password: "<<userPSS<<endl;



return 0; //indicates success
}//end of main
./str_space 
Enter user ID: asdf qwer

Enter Password: 1234

User ID: asdf

User Password: 1234
cin.ignore(100,'\n'); will only ignore 100 characters till '\n' if user enters more than 100 characters after the space this will fail. Apart from that it should work as well
sweet thx guys
@codewalker you are right,
was just an example,
something more accurate
would be your code;

@jigaboo247
you are welcome!
Topic archived. No new replies allowed.