【发布时间】:2015-07-12 07:04:20
【问题描述】:
解释:
使用两个输入流流,都使用getline() 来捕获用户输入。第一个getline()在userStringPrompt()函数中被调用:
string userStringPrompt()
{
string userString;
cout << "Enter a string: ";
getline(cin, userString);
return userString;
}
这设置了一个字符串,程序稍后将使用它来执行功能,比如计算它的辅音、元音等。
第二个输入用于菜单选择(选择对上述字符串执行什么功能/方法):
string userMenuPrompt()
{
string userString;
getline(cin, userString);
return userString;
}
这接受用户输入:A, B, C, D, E 并执行操作。例如,如果用户在第一个输入流中输入:
你好
然后在第二个输入流中输入“A”,它应该计算hello中的元音,然后从计算中返回数字,例如2
问题:
当userStringPrompt 中的输入没有空格时,程序可以正常运行。例如,Hello 有效,但 Hello World 会破坏应用程序并导致强制关闭。我不知道为什么,因为我正在使用 getline(cin, string) 捕获流。
整个代码:
#include <string>
#include <iostream>
using namespace std;
string userStringPrompt();
string userMenuPrompt();
void showMenu();
int countConsonants(string *ptr);
int countVowels(string *ptr);
int countConsonantsVowels(string *ptr);
int main()
{
string userInput = "";
string userString;
bool userStringSet = false;
while (userInput != "e") {
if (!userStringSet) {
userString = userStringPrompt();
userStringSet = true;
}
showMenu();
userInput = userMenuPrompt();
string *pointerVariable = &userString;
if (userInput == "a") {
cout << endl;
cout << "Vowel count in \"" << userString << "\": " << countVowels(pointerVariable) << endl;
cout << endl;
break;
}
if (userInput == "b") {
cout << endl;
cout << "Consonants count in \"" << userString << "\": " << countConsonants(pointerVariable) << endl;
cout << endl;
break;
}
if (userInput == "c") {
cout << endl;
cout << "Vowel count in \"" << userString << "\": " << countVowels(pointerVariable) << endl;
cout << "Consonants count in \"" << userString << "\": " << countConsonants(pointerVariable) << endl;
cout << endl;
break;
}
if (userInput == "d") {
userStringSet = false;
break;
}
}
return 0;
}
int countConsonants(string *ptr) {
string getVar = *ptr;
int stringSize = getVar.length();
int vowelCount = countVowels(ptr);
int totalConsonants = stringSize - vowelCount;
return totalConsonants;
}
int countVowels(string *ptr) {
int vowelsToCount = 0;
string stringVar = *ptr;
for (int i = 0; i < stringVar.length(); i++){
if (tolower(stringVar[i]) == 'a' || tolower(stringVar[i]) == 'e' || tolower(stringVar[i]) == 'i' || tolower(stringVar[i]) == 'o' || tolower(stringVar[i]) == 'u') {
vowelsToCount++;
}
}
return vowelsToCount;
}
int countConsonantsVowels(string *ptr) {
int count = countConsonants(ptr);
count += countVowels(ptr);
return count;
}
string userStringPrompt()
{
string userString;
cout << "Enter a string: ";
getline(cin, userString);
return userString;
}
string userMenuPrompt()
{
string userString;
getline(cin, userString);
return userString;
}
void showMenu() {
cout << "A) Count the number of vowels in the stream" << endl;
cout << "B) Count the number of consonants in the stream" << endl;
cout << "C) Count both the vowels and consonants in the stream" << endl;
cout << "D) Enter another string" << endl;
cout << "E) Exit the program" << endl;
}
任何指导/提示将不胜感激。我完全糊涂了。
【问题讨论】:
-
在我的编译器上完美运行。
-
您使用的是哪个编译器?我目前正在使用 MS Visual C++ 2013 ...想知道这是否与它有关。您是否能够浏览菜单并计算带有空格的字符串的元音?
-
Gcc,代码块 13.12
-
我尝试了 VS 13 的代码,除了你要求一个大写字母并期望一个小写的程序运行
Hello和Hello World输入和a选项 -
然后发布更多信息... 有例外吗?您是从 VS(本地调试)运行还是运行 exe(Ctrl+F5)?发布/调试?更多信息会有所帮助
标签: c++ string char getline c-strings