【发布时间】:2014-08-19 05:26:33
【问题描述】:
我是 C++ 新手。我试图了解如何利用 C++ 通用输入 (cin)。我正在尝试编写一个程序来检查句子的字符数量和输入句子中元音的数量。我已经成功地做到了,但是当我尝试让代码再运行一次时会出现问题。当它再次运行时,它不再允许第二次输入。我的简化代码如下。
#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
运行此程序时,首先允许用户输入一个句子,输入和结果显示后,要求用户输入'y'或'n'。如果答案是 y,代码将不允许输入句子(getline 所在的位置)并显示所有结果(a_counter...)都为 0,并直接跳回请求输入“y”或“n”。有人可以帮助我吗?将不胜感激。
【问题讨论】:
标签: c++ loops input cin getline