c++在这种情况下,我怎么能忽略第二条消息呢


double const A = 0.80,
B = 0.60,
C = 0.40,
D = 0.20;
char code1,
code2;
int volume,
student;
double price,
totalcost,
totalvolume,
totalprofit;
cout << "Enter the number price for one book :";
cin >> price;
cout << "Enter the book in stock :";
cin >> volume;
cout << "Enter the number of student :";
cin >> student;
cout << "Is the book required or suggested? Enter 'R' or 'S' :";
cin >> code1;
cout << "Is the book new or used? Enter 'N' or 'U' :";
cin >> code2;
if (code1 == 'R' && code2 == 'N')
{
totalvolume = student * A - volume;
cout << " We need to buy :" << totalvolume << endl;
if (totalvolume !=0)
totalcost = totalvolume * price;
cout << " totalcost is :" << totalcost << endl;
}
else
{
}
cout << " We need to buy :" << totalvolume << endl;

return 0;
}

我想知道是否有人能帮我,有没有一种方法可以让我忽略第二条输出消息("totalcost is"(,只在totalvolume为0时打印出("We need to buy"(?这是我在大学里的第一个项目,而loop还没有教过,也不允许使用。我只能使用if语句或开关。非常感谢。

这段代码:

if (totalvolume !=0)
totalcost = totalvolume * price;
cout << " totalcost is :" << totalcost << endl;

该代码片段中的第三行";总成本为:";如果外部";如果";语句条件满足。如果您希望它只在this If语句为true时打印,则必须用大括号{}将其括起来。所以它应该是这样的:

if (totalvolume !=0){
totalcost = totalvolume * price;
cout << " totalcost is :" << totalcost << endl;
}

最新更新