c++ - 当我尝试输入“Y”时出现错误代码,然后 if 代码不起作用并直接转到 else 并完成
问题描述
这是我的代码
cout<<"Do you have a discount?? (Y/N) :";
cin>>code;
cout<<endl;
if (code=="Y"){
all_cost = cost - 100000;
}
else {
all_cost = cost;
}
其余代码都可以工作,只有当代码不起作用时
解决方案
我复制了你的代码并添加了一个string code
声明,它对我有用。
#include <iostream>
#include<string>
using namespace std;
int main(){
cout<<"Do you have a discount?? (Y/N) :";
string code;
int all_cost = 0;
int cost = 0;
cin>>code;
cout<<endl;
if (code=="Y"){
all_cost = cost - 100000;
}else {
all_cost = cost;
}
cout<<all_cost<<endl;
}
推荐阅读
- reactjs - 从映射中排除项目
- c++ - sort() 方法 c++ 中的比较器函数。为大量数字获得不同的解决方案
- angular - Angular 8 - 将所有材料模块导入主应用程序模块是否比在不同模块中导入单个材料模块更好?
- android - Samsung Health SDK - Android : 读取理想时间的总燃烧卡路里
- docker - 如何在运行时将敏感数据发送到 docker 容器
- linux - 如何删除基于另一个文件的行?
- flutter - 如何在flutter中创建自定义tabBar
- c++ - 无法 typedef std::chrono::microseconds 覆盖其定义以更改其基础类型
- python - 用字符串python填充空列表元素
- firebase - 使用 react-native-firebase 创建 Firebase 动态链接失败 - React Native