【问题标题】:C++ getline forces to close a console application with spaceC++ getline 强制关闭带有空格的控制台应用程序
【发布时间】: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 的代码,除了你要求一个大写字母并期望一个小写的程序运行 HelloHello World 输入和 a选项
  • 然后发布更多信息... 有例外吗?您是从 VS(本地调试)运行还是运行 exe(Ctrl+F5)?发布/调试?更多信息会有所帮助

标签: c++ string char getline c-strings


【解决方案1】:

我猜你的程序没有崩溃,它只是正常退出。

The program '[8776] Chapter10.exe' has exited with code 0 (0x0).

没问题,没有返回错误码。

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

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

在你的情况下,我猜问题出在break 语句中。当您选择如何处理字符串时,您会中断循环(应该在用户输入e 时运行)。删除所有breaks 就可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多