How to allow user to do input more than twice?

十年热恋 提交于 2019-12-11 04:13:39

问题


I am new to C++. I am trying to understand how to utilize the C++ common input (cin). I am trying to write a program that checks the amount of characters of the sentence and the amount of vowels in the input sentence. I have successfully done it, but a problem happens when I try to get the code to run through one more time. When it runs through one more time, it doesn't allow second input anymore. My simplified code is below.

#include <iostream>
#include <string>

using namespace std;

int main()
{
  char rerun = 'y';
  string input;
  int a_counter, e_counter, i_counter, o_counter, u_counter;
  a_counter = e_counter = i_counter = o_counter = u_counter = 0;

  do
  {
    getline(cin, input); // asking user to input a sentence
    // already written code here that uses for loop to do the vowel counting
    // already written code to use the cout command to output the result
    cin >> rerun; // ask to type 'y' or 'n' to continue, assume user only types y or n
  } while (rerun == 'y');
} //end of main function

When running this program, at first user would be allowed to input a sentence and, after the input and the result are displayed, user is asked to put in 'y' or 'n'. If answer is y, the code would not allow to put in the sentence (where getline is) and display result of everything (a_counter...) are all 0 and jump straight back to requesting to put 'y' or 'n'. Can somebody help me? It would be much appreciated.


回答1:


When the line

cin >> rerun;

is executed, the '\n' is left in the input stream. Next time you run

getline(cin, input);

you get an empty line.

To fix the problem, add the line

cin.ignore();

right after

cin >> rerun;

Here's what your loop should look like:

do
{
  getline(cin, input);
  cin >> rerun;
  cin.ignore();
}while (rerun == 'y');



回答2:


the problem that happens here is when you enter \n just after entering y or n is treated as a character input for rerun and so the condition becomes false..

Let us try to understand what is happening in your code.. Suppose you are giving the input

abcde(\n)

y(\n)

abc(\n)

the string is terminated when it encounters a \n..so

input = "abcde" (no problem here)

now when you enter y(\n) in rerun its ok..the condition becomes true but the problem starts here..the \n is now taken in input..i.e

rerun = 'y'

and

input = '\n'(only a line with nothing will be printed..try with cout)

and now as soon as you enter something assuming that it will go in input it doesn't really happen but actually it goes inside rerun(because input is already containing \n) and if it is other than y it will be evaluated false..hope i am clear

here is another method to do it

#include <string>
#include <cstdio>
#include <iostream>

using namespace std;

int main()
{
    char eatNewline = '\0';
    char rerun = 'y';
    string input;
    int a_counter, e_counter, i_counter, o_counter, u_counter;
    a_counter = e_counter = i_counter = o_counter = u_counter = 0;

    do {
        getline(cin, input);
        cin >> rerun;
        eatNewline = getchar();
    } while (rerun == 'y');

 return 0;
}


来源:https://stackoverflow.com/questions/24463056/how-to-allow-user-to-do-input-more-than-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!