我的编码有什么问题吗
Is there soething wrong with my coding?
这个编码是用C++语言编写的,基本上我刚开始学习这个语言,所以我在编码方面不是很好。。希望有人能帮助我。
#include <conio.h>
#include <iostream>
int main(){
char name[50], status[3];
int type;
float price, discount, total;
cout << "Please enter patient's name : ";
cin >> name ;
cout << "Please enter patient's status [Children(C) | Adult(A)] : ";
cin >> status ;
cout << "Please enter type of treatment[Restoration(1) | Extraction(2) | Scaling(3) : ";
cin >> type ;
if(status=="C" || status=="c"){
if(type==1){
price = 6.0;
discount = price * 0.90;}
else if(type==2){
price = 15.5;
discount = price * 0.90;}
else{
price = 4.0;
discount = price * 0.90;}}
else if(status=="A" || status =="a"){
if(type==1){
price = 7.5;
discount = price * 0.95;}
else if(type==2){
price = 18.0;
discount = price * 0.95;}
else{
price = 5.5;
discount = price * 0.95;}}
cout << " nn HUSNA DENTAL"<<endl;
cout << " ===================================================================="<<endl;
cout << " Name : "<<name<<endl;
cout << " Type of treatment : "<<type<<endl;
cout << " Payment before discount: "<<price<<endl;
cout << " Payment after discount : "<<(total=price-discount)<<endl;
cout << " ===================================================================="<<endl;
getch();
}
输出为:
请输入患者姓名:Deena
请输入患者状态[儿童(C)|成人(A)]:
请输入治疗类型[恢复(1)|提取(2)|缩放(3):1
HUSNA DENTAL
====================================================================
Name : Deena
Type of treatment : 1
Payment before discount: 0
Payment after discount : -1.00061e-37
====================================================================
status=="C" || status=="c"
你在这里比较两个相等的指针。我不认为这是你想要做的。你的意思是status[0] == 'c'
吗?还是strcmp(status, "C") == 0
?
或者,更好的是,既然它被标记为C++,为什么不使用std::string让你的生活变得轻松呢?
#include <iostream>
#include <string>
using namespace std;
int main() {
string name, status;
int type = 0;
cout << "Please enter patient's name : ";
cin >> name;
cout << "Please enter patient's status [Children(C) | Adult(A)] : ";
cin >> status;
cout << "Please enter type of treatment[Restoration(1) | Extraction(2) | Scaling(3) : ";
cin >> type;
float price = 0.0f, discount = 0.0f;
if (status == "C" || status == "c") {
if (type == 1) { price = 6.0f; discount = price * 0.90f; }
else if (type == 2) { price = 15.5f; discount = price * 0.90f; }
else if (type == 3) { price = 4.0f; discount = price * 0.90f; }
}
else if (status == "A" || status == "a") {
if (type == 1) { price = 7.5f; discount = price * 0.95f; }
else if (type == 2) { price = 18.0f; discount = price * 0.95f; }
else if (type == 3) { price = 5.5f; discount = price * 0.95f; }
}
cout
<< " HUSNA DENTAL"
<< endl << "===================================================================="
<< endl << " Name : " << name
<< endl << " Type of treatment : " << type
<< endl << " Payment before discount: " << price
<< endl << " Payment after discount : " << discount
<< endl << "===================================================================="
<< endl;
}
相关文章:
- 警告处理为错误这里有什么问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 当我尝试添加 2 个大字符串时,我无法弄清楚出了什么问题
- 违反const正确性:我应该现实地期待什么问题
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- 我的逻辑反转字符串中的元音有什么问题?
- 需要以下代码的帮助,下面的代码有什么问题
- 常量公共成员有什么问题?
- c++无值sort()的问题是什么?
- 以下代码中的函数模板有什么问题?
- 这个返回元素位置的基于循环的函数有什么问题?
- creat_list2功能有什么问题?
- 基本的 c++ 问题:如果我在函数中创建某些内容并返回它会发生什么?
- 我遇到了黑客排名中的问题"TWO STRINGS"的三个测试用例的分段错误。原因是什么?
- 什么是钻石问题?是一系列问题还是特定问题?
- 格式说明符C++有什么问题
- 我应该在 main 函数中写什么来测试我的问题?
- 任何人都可以告诉我我的 C++ 代码出了什么问题?
- 方法问题 - 什么会改变值,什么不会改变?什么是无效的?