Infinite Loop

Read the "before you post" instructions and expect many rude answers. No problem..yes I'm a beginner and this is part of a homework assignment. Also your jobs are safe as I do not want to become a programmer although I am excited learning what I have so far

My issue is when a space is included when prompted for Captains name the program goes into an infinite loop and I do not know why. Here is my code.

So, what happens? When the user is asked to enter a name and enters "Harvey Wallbanger\n", the subsequent cin >> name only extracts the first word, which means that " Wallbanger\n" is still present in the stream when we get to the next extraction operation, which expects a number as input. Obviously what is already in the input stream cannot be stuffed into an int, so the stream goes into an error state. That state is never cleared, so all subsequent extraction operations fail.

Typically when you want to extract a full line of input, spaces and all, you would use std::getline. It extracts a line of text, all whitespace preserved, up to and including the next newline in the input stream. That newline is discarded by the function.

But, as you can see, we only get the opportunity to enter one name. What is this, you're asking? Well, the key is in understanding how the formatted extraction works, and how it interacts with unformatted extraction (such as getline.)

When we did std::cin >> name and entered "Harvey Wallbanger\n" we saw that what was left in the input stream after extraction was " Wallbanger\n". Note that the whitespace after Harvey was left in tact.

When we do std::cin >> fuelPods and enter a number like so: "30\n" what do you think is left in the stream after our extraction? "\n" is. So, when we get back to the top of the loop, our next extraction operation (getline) encounters a newline in the input stream right away. And since it only extracts input up to the first newline it encounters, the string it extracts is empty.

Not quite what we wanted. Fortunately, now that we know what the problem is we can just ignore a character after the last formatted extraction operation that comes before an unformatted extraction operation:

The replies are greatly appreciated. I've attempted both solutions but my program isn't working exactly the way it is supposed to. Ideally it asks for ship id, captain name then quantities of the supplies and as long as -999 isn't entered it performs calculations then SHOULD ask me for another ship ID but it comes back with enter captain's name. I've been committing about 20 hours a week on top of my full time job to this class (online) and still find myself lost most of the time. The instructor puts out the syllabus and that is pretty much it. So it seems I paid a lot of money to learn programming on my own though I assumed I would be taught......No worries, I'll get through it one way or another.

Thank you Cire! The explanation helped me immensely and I have a better understanding of my program, the mistakes and why the cin.ignore() was needed. Fixed the problem and it works as it is supposed too!