到目前为止,这是我的程序:
int main()
{
char choice = 'D';
string inputString;
cout << "Please input a string." << endl;
getline(cin, inputString);
LetterCount letterCount(inputString);
while(choice != 'E')
{
cout << "Please choose from the following: " << endl
<< "A) Count the number of vowels in the string." << endl
<< "B) Count the number of consonants in the string." << endl
<< "C) Count both the vowels and consonants in the string." << endl
<< "D) Enter another string." << endl << "E) Exit the program." << endl;
cin >> choice;
if(choice == 'A' || choice == 'a')
{
cout << "There are " << letterCount.vowelCount() << " vowels in this string." << endl;
}
else if(choice == 'B' || choice == 'b')
{
cout << "There are " << letterCount.consonantCount() << " consonants in this string." << endl;
}
else if(choice == 'C' || choice == 'c')
{
cout << "There are " << letterCount.vowelCount() << " vowels and " << letterCount.consonantCount()
<< " consonants in this string, for a total of " << (letterCount.vowelCount() + letterCount.consonantCount())
<< " letters." << endl;
}
else if(choice == 'D' || choice == 'd')
{
cout << "Please type in another string." << endl;
getline(cin, inputString);
letterCount.setInputString(inputString);
}
else
{
choice = 'E';
}
}
}
我只包括主要的,因为它是这里的问题提供者,其他一切都正常运行。
当我使用选项'D'(输入一个新字符串)时出现问题,一旦按下回车,程序就会直接返回到选择提示并将 inputString 变量设置为空白(不是单词空白,但里面什么都没有)
第一个 getline(cin, inputString) 工作得很好,第二个是问题提供者......有什么建议吗?