【问题标题】:getline(cin, variable) not wanting to work properly in c++?getline(cin, variable) 不想在 C++ 中正常工作?
【发布时间】:2010-03-21 00:13:38
【问题描述】:

到目前为止,这是我的程序:

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) 工作得很好,第二个是问题提供者......有什么建议吗?

【问题讨论】:

  • 查看我添加到我的答案中的链接。

标签: c++ getline


【解决方案1】:

我认为您必须添加以下内容: std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(),'\n'); 在你的cin &gt;&gt; choice之后

原因是,用户实际输入了'D\n',但只有'D'适合choice变量,所以'\n'进入cin的缓冲区。 当您调用 getline 时,getline 会在缓冲区中看到 '\n' 并返回,其中没有任何内容...

上面的调用将删除 cin 缓冲区中的所有 '\n'。

解释得很好here

另一种解决方案是:

char newline;
cin >> choice;
cin.get(newline);

这只会从缓冲区中删除一个“\n”。(因为cin &gt;&gt; choice 添加了一个\n

【讨论】:

  • ...那行代码几乎没有意义...它是做什么的?正如我所说,第一个 getline 工作正常,第二个(在选项 D 的 if 循环中)是一个问题。
  • 啊,太棒了,很有道理,我会试一试!我需要任何特殊的#include 吗?只是把它扔回去 5 个错误
  • 你必须#include &lt;limits&gt; 才能获得numeric_limits
【解决方案2】:

使用开关盒会更好吗,比如这个?

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//Initialize
void newString();
void menu();
void switchCases(choice);

//Variables
string inputString;
char choice;
LetterCount letterCount(inputString);


int main(){
    menu();
}

void newString(){
    cout << "Please type in another string." << endl;
    getline(cin, inputString);
    letterCount.setInputString(inputString);
}

void menu(){
    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;
        newString();
}



void switchCases(char choice){
    switch (choice){
        case 'A':
        case 'a':{
            cout << "There are " << letterCount.vowelCount() << " vowels in this string." << endl;
            break;}
        case 'B':
        case 'b':{
            cout << "There are " << letterCount.consonantCount() << " consonants in this string." << endl;
            break;}
        case 'C':
        case 'c':{
            cout << "There are " << letterCount.vowelCount() << " vowels and " << letterCount.consonantCount()
            << " consonants in this string, for a total of " << (letterCount.vowelCount() + letterCount.consonantCount())
            << " letters." << endl;
            break;}
        case 'Q':
        case 'q':{
            break;}
        default:
        case 'D':
        case 'd':{
            main();}
    }
}

【讨论】:

  • 如果该人选择 f、p 或数字作为选项会发生什么? if 语句做了我需要它做的事情,所以它仍然是我的选择。不争论切换的好处,我喜欢它们,但如果在我的情况下效果更好。
  • 你可以在 switch 案例中添加一个default: 语句,我把它放在最后,所以如果他们输入错误,它会再次显示菜单,你可以添加一个评论说这个选项不存在。看Travis给出的例子,和他刚刚添加的控制台消息一模一样。
【解决方案3】:

您没有正确使用 getline。尝试使用这样的骨架...

#include <iostream>
#include <iomanip>
#include <string>

using std::getline;
using std::string;
using std::cin;
using std::cout;
using std::endl;


const string instructions = "Please choose from the following:\nA) Count the number of vowels in the string.\nB) Count the number of consonants in the string.\nC) Count both the vowels and consonants in the string.\nD) Enter another string.\nE) Exit the program.\n";

int main()
{
    // print instructions
    cout << instructions;

    // input text
    string text;

    bool processing = true;

    // start loop
    while (processing && getline(cin, text) && !text.empty())
    {
        // switch on input character
        switch (text[0])
        {
            case 'A':
            case 'a':
            {
                cout << "Doin A stuff" << endl;;
                break;
            }
            case 'B': 
            case 'b':
            {
                cout << "Doin B stuff" << endl;;
                break;
            }
            case 'C': 
            case 'c':
            {
                cout << "Doin C stuff" << endl;;
                break;
            }   
            case 'D': 
            case 'd':
            {
                cout << "Doin D stuff" << endl;;
                break;
            }
            case 'E':
            case 'e':
            {
                cout << "Exiting..." << endl;;
                processing = false;
                continue;
                break;
            }
            default:
            {
                cout << "I have no idea what you entered" << endl;
                processing = false;
                continue;
                break;
            }
        }   


        // print instructions for next loop
        cout << endl << instructions;
    }

    return 0;
} 

【讨论】:

    猜你喜欢
    • 2011-08-05
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 2014-11-28
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    相关资源
    最近更新 更多