我已经查看了其他网站,但无法找到适合我的任何内容。
我尝试根据订购的商品和订购商品的数量为家具销售添加折扣,找到下表:
表(0): 订购金额(提供折扣):1-5(5%),6-9(10%),10 +(20%)
椅(1) 订购金额(提供折扣):1-5(3%),6-9(7%),10 +(12%)
这是我的代码,我仍然无法使用它,因为这是我使用C ++编程语言的第二周。如果有人能够让我知道我做错了什么,我将不胜感激:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int yc;
char cc;
cout <<"Enter furniture code:";
cin >> yc;
cout <<"Enter amount ordered:";
cin >> cc;
switch (yc)
{
case 0: cout <<"you selected item: " << yc << endl;
break;
case 1: cout <<"you selected item: " << yc << endl;
break;
}
switch (cc)
{
case '1':
case '2':
case '3':
case '4':
case '5':
cout <<"discount applied: " << cc << endl;
break;
case '6':
case '7':
case '8':
case '9':
cout <<"discount applied: " << cc << endl;
break;
default:
if (cc >= 10)
cout <<" discount applied: " << cc << endl;
}
system("PAUSE");
return 0;
}
如果我的输入是:
Enter furniture code: 0
Enter amount ordered: 4
输出:
you selected item: 0
discount applied: 4
我想输出为:
you selected item: 0
适用折扣:5%
因为我选择了表格(0),订购金额在1-5之间。
答案 0 :(得分:0)
首先,这个:
switch (yc)
{
case 0: cout <<"you selected item: " << yc << endl;
break;
case 1: cout <<"you selected item: " << yc << endl;
break;
}
没有必要,因为两种情况都输出相同的东西。只需将其替换为:
cout <<"you selected item: " << yc << endl;
其次,你应该使用一系列if-else语句,考虑你的情况很少:
if (yc == 0) {
if (cc <= 5) {
cout << "Discount applied: 5%" << endl;
}
else if (cc < 10) {
// discount = 10%
}
// etc...
}
if (yc == 1) {
if (cc <= 5) {
// etc...
答案 1 :(得分:0)
您打印cc
但不是折扣,而是订购金额。相反,您需要打印折扣:
cout <<"discount applied: 5%" << endl;
此外,您应该考虑使用if / else语句而不是switch / case。 例如:
if(cc <= 5)
cout <<"discount applied: 5%" << endl;
else if(cc <= 10)
cout <<"discount applied: 10%" << endl;
else
cout <<"discount applied: 20%" << endl;