使用 if if 语句检查 C++ 中的范围
checking range in C++ using if if statement
我试图使用 if 语句来检查输入的数字是否适合我允许的范围。但显然它不起作用。这是我的 if 语句
用户输入重量,我检查它是否在小/顶部重量之间
const float SMALL_WEIGHT = 2.3f; // low end 1st range weight
const float TOP_SMALL_WEIGHT = 4.3f; // top end 1st range weight
cout << "Enter the weight of the laptop: ";
cin >> weight;
if ( weight < SMALL_WEIGHT && weight > TOP_SMALL_WEIGHT )
{
cout << "The weight " << weight << " is not between "
<< SMALL_WEIGHT << " and " << TOP_WEIGHT << endl;
return 1;
}
我尝试使用逻辑或||,但它仍然不起作用。
编辑:这是整个代码
#include <iostream>
#include <iomanip>
using namespace std;
const float SALE_START_PRICE = 500.0f; // discount if more than 500
const float DISCOUNT_PCT = .15f; // 15% discount
const float TAX_PCT = .075f; // tax is 7.5%
const float SMALL_WEIGHT = 2.3f; // low end 1st range weight
const float TOP_SMALL_WEIGHT = 4.3f; // top end 1st range weight
const float TOP_MID_WEIGHT = 5.8f; // top 2nd range weight
const float TOP_WEIGHT = 7.9f; // top 3rd range weight
const float SMALL_SHIP = 5.4f; // 1st range basic ship charge
const float MID_SHIP = 5.8f; // 2nd range basic ship charge
const float TOP_SHIP = 6.2f; // 3rd range basic ship charge
const float SMALL_SHIP_PCT = .025f;
const float MID_SHIP_PCT = 2.75f;
const float TOP_SHIP_PCT = 3.5;
int main()
{
float price, weight;
float discountAmount, salePrice;
float taxAmount, shippingCharges, totalSaleAmount;
cout << "Enter the original price of the laptop: ";
cin >> price;
cout << "Enter the weight of the laptop: ";
cin >> weight;
if ( weight < SMALL_WEIGHT && weight > TOP_SMALL_WEIGHT )
{
cout << "The weight " << weight << " is not between "
<< SMALL_WEIGHT << " and " << TOP_WEIGHT << endl;
return 1;
}
if ( price > SALE_START_PRICE )
{
discountAmount = price * DISCOUNT_PCT;
}
else
discountAmount = 0.00;
taxAmount = price * TAX_PCT;
salePrice = price - discountAmount;
if (weight < TOP_SMALL_WEIGHT)
shippingCharges = SMALL_SHIP + ( salePrice * SMALL_SHIP_PCT );
else if ( weight >= TOP_SMALL_WEIGHT && weight < MID_SHIP )
shippingCharges = MID_SHIP + ( salePrice * MID_SHIP_PCT);
else
shippingCharges = TOP_SHIP + ( salePrice * TOP_SHIP_PCT );
totalSaleAmount = salePrice + taxAmount + shippingCharges;
cout << fixed << showpoint << setprecision(2);
cout << "The original price : $" << price << endl;
cout << "The discount : $" << discountAmount << endl;
cout << "The sale price : $" << salePrice << endl;
cout << "The sales tax : $" << taxAmount << endl;
cout << "The shipping : $" << shippingCharges << endl << endl;
cout << "The total sale : $" << totalSaleAmount << endl;
return 0;
}
weight
不能既小于SMALL_WEIGHT
又大于TOP_SMALL_WEIGHT
。
我认为您想使用逻辑或||
所以它变成了:
if (weight < SMALL_WEIGHT || weight > TOP_SMALL_WEIGHT)
{
cout << "The weight " << weight << " is not between "
<< SMALL_WEIGHT << " and " << TOP_WEIGHT << endl;
return 1;
}
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- 尝试通过多个向量访问变量时,向量下标超出范围
- 错误:未在此范围内声明'reverse'
- 正在将指针转换为范围
- 我的简单if-else语句是如何无法访问的代码
- 如何改进一堆在已知值范围内评估变量的 else-if 条件?
- C++带有 while 语句范围捕获的 if 语句中的错误
- C++ if 语句范围内的宏未编译
- 在 c++ 中检查 if 条件中的向量位置范围
- 从“if constexpr”分支扩展对象生存期/范围
- 为什么所有可能的整数都"true"在 C++ 的 if 语句内的 long int 范围内,而 0 不是?
- 更改if范围内变量的类型
- 重构使用 if 语句的重复范围检查
- 在“if constexpr”块中声明的变量的范围
- 访问 C++ 中 if 循环范围之外的矢量
- 使用 if if 语句检查 C++ 中的范围
- C++ : 在 if/else 中声明 : var 未在此范围内声明
- 在 C++ 中的 if 条件中创建的对象的范围
- c++在if块内为函数范围定义变量
- 将对象范围扩展到if语句之外