拍卖项目:C++ cin 错误
我正在研究一个应该复制拍卖的项目。我想询问第一个投标人的姓名,如果投标人的姓名留为黑色,则会显示一条错误消息,指出 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
getline(cin,name)
将读取直到下一个换行符。也许您已经在输入流中等待了一个换行符,该换行符来自您未在换行符中读取的先前输入。例如,您的cin>>price
将读取一个数字,但不会读取该数字后面的换行符,因此如果对getName
的调用前面是调用getPrice
,则价格后面的换行符仍将等待,并且getName
会将其视为行尾。编辑更新的问题:您需要将其更改
为
:您明白为什么吗?
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, yourcin>>price
will read in a number, but won't read in a newline after that number, so if a call togetName
is preceded by a call togetPrice
, then the newline after the price will still be waiting, andgetName
will see it as the end of the line.Edited for updated question: You need to change this:
to just this:
Do you see why?