gpt4 book ai didi

c++ - 代码占用了我一半的输入

转载 作者:行者123 更新时间:2023-11-30 02:18:31 25 4
gpt4 key购买 nike

我正在上编程课学习 C++。我们必须解决 if 和 else 语句问题。下面是我的代码,我试图找出它减半的原因。

#include <iostream>
#include <iomanip>

using namespace std;

int main() {

int age;
double price;
char category;
double finalPrice;

cin >> price;
cin >> age;
cin >> category;

if (age <= 0) {
cout << "Wrong input";
}
if (age > 0 && age <= 5) {
if (category != 'A' || 'a') {
finalPrice = price - (( price * 100)/100);
cout << fixed;
cout << setprecision(2) << finalPrice;
}

else if(category == 'A' || 'a') {
finalPrice = price - (( price * 0)/100);
cout << fixed;
cout << setprecision(2) << finalPrice;
}
}

if (age > 5 && age <= 12) {
if (category != 'B' || 'b') {
finalPrice = price - (( price * 50)/100);
cout << fixed;
cout << setprecision(2) << finalPrice;
}
else if(category == 'B' || 'b') {
finalPrice = price - (( price * 0)/100);
cout << fixed;
cout << setprecision(2) << finalPrice;
}
}

if (age > 12 && age <= 26) {
if (category != 'C' || 'c') {
finalPrice = price - (( price * 60)/100);
cout << fixed;
cout << setprecision(2) << finalPrice;
}
else if(category == 'C' || 'c') {
finalPrice = price - (( price * 0)/100);
cout << fixed;
cout << setprecision(2) << finalPrice;
}
}

if (age > 26 && age <= 60) {
if (category != 'D' || 'd') {
finalPrice = price - (( price * 70)/100);
cout << fixed;
cout << setprecision(2) << finalPrice;
}
else if(category == 'D' || 'd') {
finalPrice = price - (( price * 0)/100);
cout << fixed;
cout << setprecision(2) << finalPrice;
}
}

if (age > 60) {
if (category != 'E' || 'e') {
finalPrice = price - (( price * 80)/100);
cout << fixed;
cout << setprecision(2) << finalPrice;
}
else if(category == 'E' || 'e') {
finalPrice = price - (( price * 0)/100);
cout << fixed;
cout << setprecision(2) << finalPrice;
}
}

return 0;
}

以上是我的学校作业代码。

当我输入值 14.56 25 C我得到 5.8.2

的输出

但是我的预期输出应该是14.56

我只是在监督什么吗?我不明白它是怎么减半的。

谢谢!

最佳答案

(category != 'A' || 'a') 等 -

我怀疑你的意思是“类别不是‘A’或‘a’”;试试 ((category != 'A') && (category != 'a'))

提示:编译器正在评估两个语句 category != 'A' OR 'a'

因为 'a 是非零的,它会评估为 true 并打乱你的逻辑

此外,您可以通过采用大写的 category 并将其与“A”进行比较来简化此操作,因此只需要一次比较。

关于c++ - 代码占用了我一半的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52072465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com