How to use getline to get 2 different inputs | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to use getline to get 2 different inputs

So i made basic username and password code and the part of the code where my issue arises is this: cout << "Please enter your username." << endl; getline(cin,username1); getline(cin,username2); cout << "Please enter your password." << endl; getline(cin,password1); getline(cin,password2); This is a portion of my code...i have 2 if statements that ofc is set to check for the username and password. So lets say i have 2 usernames "blah" and "cha" and the password for "blah" is "apple" and for "cha" its "orange". Now when i run my program i cant type either username to where it corresponds to its own password. So if i run it and since username1 appears first in my code it occupies the first line if i wanted to type username2 i would have to press enter first to get to the next line and then type and its the same for the passwords. So im confused as to how i can fix it so i can write username1 or 2 and just press enter once so i can move on to type the password that corresponds with the username i chose. Thanks.

15th Jan 2019, 12:31 AM
Noobly
13 Answers
+ 2
So you want to be able to have a user system, where your program knows if the typed in password is an already existing user? If so, you should store your usernames in an array. Take in one input, then check that input for a value in the username array. If it is in there, save the index number and use that to compare the password for the second input.
15th Jan 2019, 3:35 AM
Zeke Williams
Zeke Williams - avatar
+ 2
I think this might be what you're looking for. Luis Méndez has a good example as well: https://code.sololearn.com/cQjRvRu63JIM/?ref=app
15th Jan 2019, 4:16 AM
Zeke Williams
Zeke Williams - avatar
+ 1
If you want to enable access when a combination of username-password is correct, maybe you have to do something like this: https://code.sololearn.com/cujNU06CBM7u/?ref=app If it isn't what you want, tell me, and if it works, tell me too.
15th Jan 2019, 4:05 AM
Luis Méndez
Luis Méndez - avatar
+ 1
I did haha yo hablo espanol tambien amigo xD su ingles es muy bueno
15th Jan 2019, 5:02 PM
Noobly
+ 1
Jajajajajaj gracias, el tuyo también
15th Jan 2019, 5:07 PM
Luis Méndez
Luis Méndez - avatar
0
Can you write your code in Code Playground and send it here? Maybe i'll understand it better
15th Jan 2019, 2:57 AM
Luis Méndez
Luis Méndez - avatar
0
Here is the code i wrote and its my solution to a problem in this book im using to help me learn C++ and its in the if statement section so im trying to figure out how to solve it solely using if statements
15th Jan 2019, 3:39 AM
Noobly
0
Note i am using the CodeBlocks IDE to help me do this
15th Jan 2019, 3:40 AM
Noobly
0
Why don't you use cin instead getline?
15th Jan 2019, 3:43 AM
Luis Méndez
Luis Méndez - avatar
0
I didnt use cin because when i type one username and press enter and it never runs the next block of code so i never reach the output asking me for a password
15th Jan 2019, 3:45 AM
Noobly
0
I just tried it and it worked Luis Mendez exactly the way i wanted. Thanks to you both Luis and Zeke! :)
15th Jan 2019, 4:58 PM
Noobly
0
You're welcome, i hope you understood me, i'm not english haha
15th Jan 2019, 5:01 PM
Luis Méndez
Luis Méndez - avatar