java中等待用户输入数字的语句,getline()不等待用户输入

我有一个代码,应该使用用户输入来获取披萨的顺序 .

#include

#include

using namespace std;

int main()

{

double totalCosts = 0;

// PIZZA

cout << "What size is your first pizza?\n"

"small: $8.00\n"

"medium: $10.00\n"

"large: $12.00\n";

string currentSize = "";

getline(cin, currentSize);

cout << "\n";

if(currentSize == "small")

{

totalCosts += 8;

}

else if(currentSize == "medium")

{

totalCosts += 10;

}

else if(currentSize == "large")

{

totalCosts += 12;

}

else

{

cout << "invalid size: " << currentSize;

return 0;

}

string pizzaToppings = "";

cout << "Choose from the following toppings:\n"

"onions: $1.00\n"

"peppers: $1.20\n"

"ham: $1.50\n"

"hamburger: $1.25\n"

"pepperoni: $1.55\n"

"salami: $1.63\n"

"sausage: $1.44\n";

getline(cin, pizzaToppings);

cout << "\n";

if(pizzaToppings.find("onions") != string::npos)

{

totalCosts += 1;

}

else if(pizzaToppings.find("peppers") != string::npos)

{

totalCosts += 1.2;

}

else if(pizzaToppings.find("ham") != string::npos)

{

totalCosts += 1.5;

}

else if(pizzaToppings.find("hamburger") != string::npos)

{

totalCosts += 1.25;

}

else if(pizzaToppings.find("pepperoni") != string::npos)

{

totalCosts += 1.55;

}

else if(pizzaToppings.find("salami") != string::npos)

{

totalCosts += 1.63;

}

else if(pizzaToppings.find("sausage") != string::npos)

{

totalCosts += 1.44;

}

else

{

cout << "Choose at least one valid topping";

return 0;

}

// DELIVERY AND FINAL PRINTOUT

string needToDeliver = "";

cout << "Do you want delivery? yes/no\n";

getline(cin, needToDeliver);

cout << "\n";

if(needToDeliver == "yes")

{

totalCosts += 3;

cout << "What is your address?";

getline(cin, needToDeliver); // address doesn't matter

}

cout << "Your total cost is: $" << totalCosts << ". Thank you for buying C++ Pizza.";

return 0;

}

此代码提供以下输出:

你的第一个披萨是多大?

小:8.00美元

中:10.00美元

大:12.00美元

无效大小:

我认为发生的是第一个getline函数不是在等待任何用户输入,而是输入一个空字符串 . 它为什么这样做?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值