How to use do-while loop to ask whether or not user wants multiple inputs?

closed account (4zUjy60M)
[deleted]
Last edited on
The >> operator only reads what's necessary to get the value so the rest of the line remains in cin ready be read by the next read operation. This can be a problem when using the getline function because the way it works is that it just reads input until it finds a newline character so in this case it will just find the newline character that was left over from when you inputted Y or N and return an empty string. The easiest way around this is to always discard all leading whitespace characters (spaces, tabs, newlines) before trying to read the line. I suggest always using getline this way, it's almost always what you want.

 
getline(cin >> ws, name)


You might also want to move the printing of the question from line 1 to line 21 because otherwise it will only ask before the loop start and not every time. You might also want to move line 4 to the end of the loop so that you don't have to answer the question the first time.
Last edited on
Further more:

Line 11: What is size? It is supposed to be name.size().
Line 13: The if doesn't make sense since you do the same thing in both branches.

It would be nice if the code would be compilable in order to reproduce the problem.
can you share the output?
..that will make esay to identify the problem
Topic archived. No new replies allowed.