c++ - 拍卖项目 : C++ error with cin

标签 c++ iostream cout getline cin

我正在从事一个旨在复制拍卖的项目。我想询问第一个投标人的姓名,如果投标人的姓名留为黑色,则给出一条错误消息,指出 BidderID 留空。

问题

代码在询问“Bidder1 ID”后继续自动跳过 cin,并直接转到错误消息:bidder ID blank。我正在使用我创建的名为 getName 的函数,我认为这是问题所在,但它在代码中使用的其他地方运行正常。

int main()
{
    double bid1, bid2;//declare bid1, bid2

    printHeader();

    string lotName= getName("Enter lot name: "); //lot name
    double reservePrice= getPrice("Reserve price: $");
    if (reservePrice<=0) {
        printErrorMessage(5);
        return 0;
    }

    cout<<"\n";

    string bidder1= getName("Bidder 1 ID: "); //bidder1 name


    if (bidder1== "") {
        printErrorMessage(3);
        bid1=0;
    }

    else {
        bid1= getPrice("Bidder1 price: $"); //bidder 1 price
        bool lead= isBidPriceGood (bid1, reservePrice); //true if bid1>reservePrice
        if (lead==true)
            cout<<"\n"<<bidder1<<" is high bidder, current price = $"<<bid1<<endl<<endl;
    }

    string bidder2= getName("Bidder 2 ID: "); //bidder2 name

    getline(cin,bidder2);

    if (bidder2== "") {
        printErrorMessage(3);
        bid2=0;
    }
    else {
        bid2= getPrice("Bidder1 price: $"); //bidder 2 price
        isBidPriceGood (bid2, reservePrice); //true if bid2>reservePrice
    }

    //function

    string getName(string prompt)
    {
        string name;
        cout<<prompt;
        getline(cin,name);
        return name;
    }




    double getPrice(string prompt)
    {
        string x;
        double price;
        cout<< prompt;
        cin>>price;
        getline(cin,x);
        return price;
    }

    void printErrorMessage(int num)
    {
        if (num == 1) {
            cout << endl
                << "  ERROR: Reserve not met, bid rejected" << endl << endl;
        } else if (num == 2) {
            cout << endl
                << "  ERROR: Negative price, bid rejected" << endl << endl;
        } else if (num == 3) {
            cout << endl
                << "  ERROR: Blank bidder ID, no bid allowed" << endl << endl;
        } else if (num == 4) {
            cout << endl
                << "ERROR: Neither bidder met Reserve, auction canceled" << endl << endl;
        } else if (num == 5) {
            cout << endl
                << "ERROR: Reserve is not positive, auction canceled" << endl << endl;
        } else {
            cout << "   This should never print" << endl << endl;
        }
    }

最佳答案

getline(cin,name) 将一直读取到下一个换行符。可能你已经在输入流中有一个换行符在等待,来自你没有在换行符中读取的一些先前的输入。例如,您的 cin>>price 将读入一个数字,但不会读入该数字后的换行符,因此如果调用 getName 之前有一个调用getPrice,那么价格之后的换行符仍然在等待,getName 会将其视为行尾。


针对更新的问题进行了编辑:您需要更改此内容:

string bidder2= getName("Bidder 2 ID: "); //bidder2 name

getline(cin,bidder2);

仅此而已:

string bidder2= getName("Bidder 2 ID: "); //bidder2 name

你知道为什么吗?

关于c++ - 拍卖项目 : C++ error with cin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9137818/

相关文章:

c++ - CLion cout打开命令提示符窗口,而不使用CLion的终端

c++ - 添加typename导致程序编译失败

C++ 从字符串中读取整行

c++ - cin.eof() 功能

c++ - 包含错误 : Identifier "cout" is undefined. <iostream> 并使用命名空间标准;

c++ - << 的奇怪行为(至少在我看来是这样)

c++ - 在其自己的析构函数中删除链表是否安全?

c++ - 使用初始化列表作为函数参数实现 operator[] 的类对象示例

java - 标记和预读限制

c++ - 如何使用cout以全精度打印 double 值?