拍卖项目:C++ cin 错误

发布于 2025-01-02 12:54:18 字数 2618 浏览 5 评论 0原文

我正在研究一个应该复制拍卖的项目。我想询问第一个投标人的姓名,如果投标人的姓名留为黑色,则会显示一条错误消息,指出 BidderID 留空。

问题

在询问“Bidder1 ID”后,代码继续自动跳过 cin,并直接显示错误消息:bidder ID 空白。我正在使用我创建的名为 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;
        }
    }

I am working on project that is suppose to replicate an auction. I am suppose to ask for the first bidder's name and if the bidder's name is left black give an error message saying BidderID left blank.

Problem

The code keeps on automatically skipping the cin after asking for "Bidder1 ID" and going straight to the error message of error: bidder ID blank. I am using a function I created called getName, which I figured is the problem but other places used in the code it is acting properly.

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;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百思不得你姐 2025-01-09 12:54:18

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

:您明白为什么吗?

getline(cin,name) will read until the next newline. Probably you already have a newline waiting in the input-stream, from some previous input where you didn't read in the newline. For example, your cin>>price will read in a number, but won't read in a newline after that number, so if a call to getName is preceded by a call to getPrice, then the newline after the price will still be waiting, and getName will see it as the end of the line.


Edited for updated question: You need to change this:

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

getline(cin,bidder2);

to just this:

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

Do you see why?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文