c++ 为什么getline在我的程序的顶端不工作,而是在另一个函数中工作?

vc6uscn9  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(94)

我正在编写一个程序,该程序会循环直到用户输入一个字符而不是yY来停止它。我使用std::getline(),因为我需要在另一个函数中从用户那里获得一整行文本,并且它在我程序的那个函数中运行良好。当要求用户输入一个字符来继续或停止时,它不工作。我看到了一个编译器错误:
没有与“getline”调用匹配的函数
我正在使用std::getline(),因为std::cinstd::getline()一起使用会导致不必要的行为。我不知道如何解决此问题,希望得到一些帮助。

#include <iostream>
#include <string>
#include <stack>
using namespace std;

static int ASCII_OFFSET = 48;
//48 is the ASCII value for char '0', so we take the ASCII value of char '0' and subtract 48 to get the integer 0

void postCalculator();

int main()
{
    char again = 'y';
    while(again == 'y'||again == 'Y')
    {
        postCalculator();
        cout << "Type 'Y' or 'y' to continue, or type any other letter to quit:";
        getline(cin, again);
    }
}

/////
///// the line:       getline(cin, again);         is the issue
/////

void postCalculator()
{
    string equation;
    stack<int> stack1;
    int a, b, c, holder;

    cout << "Please enter the RPN expression to be calculated:" << endl;
    getline(cin, equation);

    for(int i = 0; i < equation.length(); i++)
    {
        if(equation[i] >= '0' && equation[i] <= '9')
        {
            holder = static_cast<int>(equation[i]) - ASCII_OFFSET;
            cout << "Token = " << holder << ", ";
            stack1.push(holder);
            cout << "Push " << holder << endl;
        }
        else if(i == ' ')
        {
            continue;
        }
        else if(equation[i] == ':')
        {
            break;
        }

        if(equation[i] == '-')
        {
            cout << "Token = -, ";
            a = stack1.top();
            stack1.pop();
            cout << "Pop " << a << " ";
            b = stack1.top();
            stack1.pop();
            cout << "Pop " << b << " ";

            c = b - a;

            stack1.push(c);
            cout << "Push " << c << endl;
        }
        else if(equation[i] == '*')
        {
            cout << "Token = *, ";
            a = stack1.top();
            stack1.pop();
            cout << "Pop " << a << " ";
            b = stack1.top();
            stack1.pop();
            cout << "Pop " << b << " ";

            c = b * a;

            stack1.push(c);
            cout << "Push " << c << endl;
        }
        else if(equation[i] == '/')
        {
            cout << "Token = /, ";
            a = stack1.top();
            stack1.pop();
            cout << "Pop " << a << " ";
            b = stack1.top();
            stack1.pop();
            cout << "Pop " << b << " ";

            c = b / a;

            stack1.push(c);
            cout << "Push " << c << endl;
        }
        else if(equation[i] == '+')
        {
            cout << "Token = +, ";
            a = stack1.top();
            stack1.pop();
            cout << "Pop " << a << " ";
            b = stack1.top();
            stack1.pop();
            cout << "Pop " << b << " ";

            c = b + a;

            stack1.push(c);
            cout << "Push " << c << endl;
        }
    }
    cout << "Token = Pop " << stack1.top() << endl << endl;
    stack1.pop();
}

我尝试过使用std::cin而不是std::getline(),以及std::cin.clear(),尽管我不完全理解其行为,但都没有成功。

x9ybnkn6

x9ybnkn61#

你不能用std::getline()读取一个char,它只适用于std::(basic_)string类型。因此,你必须:

  • char again更改为std::string again,例如:
int main()
{
    ...
    string again;
    do
    {
        ...
    }
    while (getline(cin, again) &&
          (again == "y" || again == "Y"));
    ...
}
  • 使用operator>>代替std::getline(),例如:
...
#include <limits>
...

int main()
{
    ...
    char again;
    do
    {
        ...
        if (!(cin >> again)) break;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    while (again == 'y' || again == 'Y');
    ...
}

相关问题