简体   繁体   中英

While loop takes input of first and second cin

My intention for these lines of code was to create a program which would display how many characters a phrase the user typed had. I completed my lines of code with one more task, to create a prompt for the user to either end the program or loop it. To do so, I created a while loop. Everything went well until I was prompted to "Go again?". No matter what input I typed, it would automatically give me my output for the first input, meaning it was also taking in input for my first cin. For reference this is my code:

#include <iostream>
#include <string.h>
#include <stdlib.h>

using namespace std;

int main()
{
char again = 'y';
string phrase;
while (again == 'y' || again == 'Y')
{

cout << "Enter your phrase to find out how many characters it is: ";
getline(cin,phrase);
cout << phrase.length() << endl;
cout << "Go again? (y/n) " ;

cin >> again;

}
cout << "The end." << endl;

system ("pause");
}

Sorry if my question is vague or I have used false terms, I have just started leaning c++.

Thanks.

You should add std::cin.ignore() after cin>>again .

#include <iostream>
#include <string.h>
#include <stdlib.h>

using namespace std;

int main()
{
char again = 'y';
string phrase;
while (again == 'y' || again == 'Y')
{

cout << "Enter your phrase to find out how many characters it is: ";
getline(cin,phrase);
cout << phrase.length() << endl;
cout << "Go again? (y/n) " ;

cin >> again;
cin.ignore();
}
cout << "The end." << endl;

system ("pause");
}

The problem is that after std::cin the new line will still stay in the input buffer and getline() will read that newline. std::ignore() will simply grab a character from input buffer and discards it.

For further information refer http://www.augustcouncil.com/~tgibson/tutorial/iotips.html#problems

There are a few problems with std::cin << varname .

When a user types an 'enter' after the input variable, cin will only read the variable, and will leave the 'enter' for the next read.

As you have mixed the cin with getline() , you sometimes eat the 'enter' and sometimes do not.

One solution is to add an ignore call after the cin call to eat the 'enter'

cin >> again;
std::cin.ignore();

A second solution is to use std::getline(). It is often a good practice to combine getline() with std::istringstream. This is an alternative way to solve this problem using getline() and istringstream. See comments in the code for explanations.

#include <iostream>
#include <string.h>
#include <sstream>

int main()
{
  std::string line;
  std::string input;
  std::istringstream instream;

  do {
    instream.clear(); // clear out old errors

    std::cout << "Enter your phrase to find out how many characters it is: "
              << std::endl;

    std::getline(std::cin, line);
    instream.str(line); // Use line as the source for istringstream object

    instream >> input;  // Note, if more than one word was entered, this will
                        // only get the first word

    if(instream.eof()) {
      std::cout << "Length of input word:  " << input.length() << std::endl;

    } else {
      std::cout << "Length of first inputted word:  " << input.length() << std::endl;

    }
    std::cout << "Go again? (y/n) " << std::endl;
  // The getline(std::cin, line) casts to bool when used in an `if` or `while` statement.
  // Here, getline() will return true for valid inputs. 
  // Then, using '&&', we can check
  // what was read to see if it was a "y" or "Y".
  // Note, that line is a string, so we use double quotes around the "y"
  }  while (getline(std::cin, line) && (line == "y" || line == "Y"));

  std::cout << "The end." << std::endl;

  std::cin >> input; // pause program until last input.
}

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