c++ - 我该如何解决这个问题并调试它,因为我已经匹配了它
问题描述
.cpp(27): 错误 C2181: 非法 else 不匹配 if
cpp(37): 错误 C2181: 非法 else 不匹配 if
你好,如何解决这个问题我想建立一个关于折扣和订单总成本的程序
下面是代码..它说的问题,但我不能纠正它谢谢
#include < iostream >
using namespace std;
#define retailPrice 20
int main() {
float quantity, discount;
float totalCost;
cout << "Welcome to GLAMOUR bookstore,Enjoy our cool promotion.\n";
cout << "Please enter the quantity of packages you have purchased:";
cin >> quantity;
{
if (quantity < 0)
cout << "Sorry,quantity cannot be negative\n";
else if (0 <= quantity && quantity < 10)
discount = '0';
totalCost = quantity * retailPrice;
}
{
if (10 <= quantity && quantity <= 19) discount = '20';
totalCost = quantity * retailPrice * 0.8f;
else if (20 <= quantity && quantity <= 49) discount = '30';
totalCost = quantity * retailPrice * 0.7f;
}
{
if (50 <= quantity && quantity <= 99) discount = '40';
totalCost = quantity * retailPrice * 0.6f;
else if (quantity >= 100) discount = '50';
totalCost = quantity * retailPrice * 0.5f;
}
cout << "Total cost of purchase = RM" << totalCost << ".\n";
cout << "discount =" << discount << "%\n";
return 0;
}
解决方案
这些错误是由括号错位引起的。检查此代码,我从中删除了错误:
#include <iostream>
using namespace std;
#define retailPrice 20
int main()
{
float quantity,discount;
float totalCost;
cout << "Welcome to GLAMOUR bookstore,Enjoy our cool promotion.\n";
cout << "Please enter the quantity of packages you have purchased:";
cin >> quantity;
if (quantity<0){
cout << "Sorry,quantity cannot be negative\n";
}
else if (0<=quantity && quantity<10){
discount = '0';
totalCost = quantity * retailPrice;
}
if (10<=quantity && quantity<=19){
discount = '20';
totalCost = quantity * retailPrice*0.8f;
}
else if (20<=quantity && quantity<=49){
discount = '30';
totalCost = quantity * retailPrice*0.7f;
}
if (50<=quantity && quantity<=99){
discount = '40';
totalCost = quantity * retailPrice*0.6f;
}
else if (quantity>=100){
discount = '50';
totalCost = quantity * retailPrice*0.5f;
}
cout << "Total cost of purchase = RM" << totalCost << ".\n";
cout << "discount =" << discount << "%\n";
return 0;
}
推荐阅读
- git - 如何从本地 git 存储库中删除所有敏感信息?
- apache-spark - 我怎么知道镶木地板文件块的大小?
- r - 从逻辑回归结果创建条形图的问题
- javascript - 警告:无法对未安装的组件执行 React 状态更新。这是一个无操作,但它表明您的应用程序中存在内存泄漏
- python - 返回 Pandas Dataframe 中字符串中第一个数字/字母的索引/位置
- javascript - Puppeteer - 如何评估返回文本的 XPath?
- scrapy - Scrapy - 动态文件命名表单解析项
- php - 解析多维数组 PHP
- javascript - 如何在反应中每 5 秒加 1 和 setState
- javascript - 如何在 Javascript 中通过用户操作使反馈消失?