In my code I want to prompt the user for input for there first and last name, however, I want to display it like so:
Last Name:_ First Name:
But I can only get it to show like this:
Last Name:_
Then after user input for Last Name, it shows:
Last Name: John First Name:_
I'm new to c++ so I don't know much of the stuff concerning rules etc. But I do know why it is displayed the way it is, I just do not know how to fix it, or even if you can fix it.
1 2 3 4 5 6 7 8 9
string lName, fName;
cout << "How many users do you wish to enter: ";
cin >> userAmt;
for (int i = 1; i <= userAmt; i++)
{
cout << "Last Name: ", cin >> lName, "First Name: ", cin >> fName;
}
Do not use comma operator unless absolutely needed and you know how is it works.
Do you need to display both First and Last name lines with cursor being on the first name position?
If so, you cannot do that using standard C++, as it works on streams, not on screen. You should use system specific function to do that.
Yes, I think the problem here is with the number of names. If it was just one first and last name, perhaps it would be easier. Great use of the for loop, though. Look at your code and try to understand the logical flow. Let's assume those commas are (;).
1. You have two variables (lName, fName)
2. You prompt the user for the number of users
3. You read this in as (userAmt)
4. Enter the for loop and stay in as long as (i) is less than userAmt
5. You prompt the user for their last name
6. You read this in as (lName)
7. You prompt the user for their first name
8. You read this in as (fName)
9. repeat steps 5+ until (i) is equal to the entered (userAmt)
Where are you displaying the result on the screen?
Unless you store (lName) and (fName) somewhere else each time the loop is ran, your display will only show the last user entered first name and last name.... Unless you are literally just trying to immediately echo (repeat) the user's entry back on the console.