C Getline力量可以关闭带有空间的控制台应用程序
C++ getline forces to close a console application with space
说明:
使用两个输入流流,都使用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;
}
任何指导/提示都将不胜感激。我绝对困惑。
我想您的程序不会压碎,通常只是退出。
The program '[8776] Chapter10.exe' has exited with code 0 (0x0).
还可以,未返回错误代码。
运行程序时,它要求输入,进行工作,并且因为没有更多的工作 - 退出。要查看输出,您可以执行以下操作之一:
- 尝试使用VS中的Ctrl F5运行它,它将添加
pause
命令到末尾。 - 另一种运行方式是打开命令行并从那里运行它。
在您的情况下,我想问题是在break
语句中。当您选择使用字符串的操作时,您会破坏循环(用户输入e
时应运行该循环)。删除所有break
s,这将是可以的。
相关文章:
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 命名空间中具有.h和.cpp文件的类
- 从父命名空间重载类型
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- C++:将控制台输出存储在宏中更好吗
- 在命名空间中定义函数还是限定函数
- C++:对不存在的命名空间使用命名空间指令
- 通过继承类使用来自不同命名空间的运算符
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- SSH通过/sbin/SSH无法读取RSA密钥文件(从控制台运行)
- 使用命名空间时出现多个定义错误
- C++控制台应用程序阻止退出
- OpenGL相机和相机空间转型的困惑
- 如何修复此iFile以将.txt输出到控制台
- CUDA内核和数学函数的显式命名空间
- 为什么控制台要求输入,即使代码中没有输入
- 打印第二列时的2d字符矢量打印空间
- 嵌套的匿名命名空间
- winrt 命名空间是否具有控制台函数?
- C Getline力量可以关闭带有空间的控制台应用程序