繁体   English   中英

getline(cin,variable)不想在c ++中正常工作?

[英]getline(cin, variable) not wanting to work properly in c++?

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

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

我想你必须添加这样的东西: std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\\n'); 在你的cin >> choice

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

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

在这里解释得很好

另一个解决方案是:

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

这只会从缓冲区中删除一个'\\ n'..(因为cin >> choice添加了一个\\n

你是否更好地使用开关盒,比如这个?

#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();}
    }
}

你没有正确使用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;
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM