【问题标题】:Auction project: C++ error with cin拍卖项目:C++ 错误与 cin
【发布时间】:2012-02-26 14:41:20
【问题描述】:

我正在做一个假设要复制拍卖的项目。我想询问第一个投标人的姓名,如果投标人的姓名为黑色,则会显示一条错误消息,说明 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;
        }
    }

【问题讨论】:

  • 家庭作业? (这只是一个填充物,因为 SO 不允许短 cmets)

标签: c++ iostream cout getline cin


【解决方案1】:

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


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

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

getline(cin,bidder2);

仅此:

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

你明白为什么吗?

【讨论】:

  • 感谢您的帮助。那么我应该如何让 getline 函数在到达下一个 getline 之前读取换行符
  • @user1188766:由于您希望用户在自己的行中给出价格,您可以让getPrice 在获得价格后致电getline。 (作为健全性检查,您可以确保 getline 的结果是一个空字符串,或者至少是一个只有空格的字符串。)这将为您吞下换行符。 (还有其他方法,但我认为这是您需要的最简单的方法。)
  • 我应该如何摆脱之前在输入流中等待的换行符
  • @user1188766:我不明白。您是否出于某种原因不想修改getPrice
  • 现在,如果我将第一个 Bidder1 ID 留空或第一个 getline 留空,则下一个 get 行需要两次输入才能进入下一行代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
相关资源
最近更新 更多