简体   繁体   中英

C++ cin.ignore and getline in while loop

I'm very new to programming, and I'm having trouble using getline in a while loop. When I cout the course variable the first letter is missing. Getting rid of cin.ignore sends it into an infinite loop.

Here's what I have so far:

#include <iostream>
#include <string>
using namespace std;
int main (){
   string answer = "Yes";
   string course;

   while (answer == "Yes"){
      cin.ignore();
      cout<< "Enter a course name: ";
      getline (cin, course);
      cout<< course << endl;

      cout<< "Continue ('Yes' or 'No')? ";
      cin>> answer;
      cout<< answer << endl;
   }

   return 0;
}

Move the ignore to the bottom of your loop. It's there to remove the newline character that the cin >> operator leaves in the buffer, so you only need it after you've used cin >> .

You should also pass arguments to ignore to ignore everything until you hit a newline in case they've entered more than just "Yes" or "No" on the line. You can do that with:

cin.ignore(numeric_limits<streamsize>::max(), '\n');

See a working example here: ideone .

The problem that I see is when you remove cin.ignore() you get interaction as follows:

Enter a course name: Math

Math

Continue ('Yes' or 'No')? Yes

Yes

Enter a course name:

Continue ('Yes' or 'No')? Yes // etc, etc...

The second time it does not prompt you for class input. This is because the Enter/Return you use to submit the info is getting extracted by getline() , which stops at the first '\\n' character it sees.

One way to fix it is use cin.ignore(), after your custom input. Mind that if reading from file, you should end the line after class input to get the same result as here.

  while (answer == "Yes"){
                                     // REMOVE cin.ignore() FROM HERE
      cout<< "Enter a course name: ";
      getline (cin, course);
      cout<< course << endl;

      cout<< "Continue ('Yes' or 'No')? ";
      cin>> answer;
      cout<< answer << endl;

      {    // THIS IS MORE EFFICIENT
           cin.ignore();                // ADD cin.ignore() HERE TO DISCARD '\n'
      }

      {   // THIS WORKS BETTER FOR HUMAN INPUT
          string dummy;
          getline (cin, dummy)          
      }
  }

If you use getline , stick to getline . Mixing it with stream operations will easily mess things up.

#include <iostream>
#include <string>

int main() {
  std::string answer = "Yes";
  std::string course;

  while (answer == "Yes") {
    do {
      std::cout << "Enter a course name: ";
      std::getline(std::cin, course);
    } while (course == "");

    std::cout << course << '\n';

    do {
      std::cout << "Continue ('Yes' or 'No')? ";
      std::getline(std::cin, answer);
    } while (answer != "Yes" && answer != "No");

    std::cout << answer << '\n';
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM