C Getline力量可以关闭带有空间的控制台应用程序

C++ getline forces to close a console application with space

本文关键字:空间 控制台 应用程序 力量 Getline      更新时间:2023-10-16

说明:

使用两个输入流流,都使用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).

还可以,未返回错误代码。

运行程序时,它要求输入,进行工作,并且因为没有更多的工作 - 退出。要查看输出,您可以执行以下操作之一:

  1. 尝试使用VS中的Ctrl F5运行它,它将添加pause命令到末尾。
  2. 另一种运行方式是打开命令行并从那里运行它。

在您的情况下,我想问题是在break语句中。当您选择使用字符串的操作时,您会破坏循环(用户输入e时应运行该循环)。删除所有break s,这将是可以的。