我的编码有问题吗?

时间:2013-06-25 15:18:51

标签: c++

这个编码是用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 << "        \n\n                                   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)]:A

请输入治疗方法[恢复(1)|提取(2)|缩放(3):1

                               HUSNA DENTAL
  ====================================================================
              Name                   : Deena
              Type of treatment      : 1
              Payment before discount: 0
              Payment after discount : -1.00061e-37
  ====================================================================

2 个答案:

答案 0 :(得分:4)

status=="C" || status=="c"

你在这里比较两个相等的指针。我不认为这就是你的意思。你的意思是status[0] == 'c'吗?或strcmp(status, "C") == 0

或者,更好的是,因为这是标记的C ++,为什么不使用std :: string让你的生活更轻松?

答案 1 :(得分:0)

#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;
}