How to use getline to get 2 different inputs | Sololearn: Learn to code for FREE!

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.

c++

1/15/2019 12:31:00 AM

Noobly

13 Answers

New Answer

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

+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

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

+1

I did haha yo hablo espanol tambien amigo xD su ingles es muy bueno

+1

Jajajajajaj gracias, el tuyo también

0

Can you write your code in Code Playground and send it here? Maybe i'll understand it better

0

https://code.sololearn.com/cw6H8hvqBTp6/?ref=app

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

0

Note i am using the CodeBlocks IDE to help me do this

0

Why don't you use cin instead getline?

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

0

I just tried it and it worked Luis Mendez exactly the way i wanted. Thanks to you both Luis and Zeke! :)

0

You're welcome, i hope you understood me, i'm not english haha