2014-04-04 98 views
-1

當我在Turbo C++中編寫這個程序時,它工作正常。但是,當我在CodeBlocks,Xcode中編寫代碼時,出現錯誤char,我不知道爲什麼。我想我可以聲明像char* name這樣的字符串。帶指針的字符傳遞函數

這個程序是關於多級繼承。通過利率和期限的功能,它會根據賬戶顯示輸出type.`

#include <string> 
#include <iostream> 
#include <fstream> 
using namespace std; 

class banktype1{ 
public: 
    char *accountType; 
    float interestAmt,depositAmt,period,totalAmt; 
public: 
    void interestCal(char *x,int y,int z){ 
     accountType=&x; 
     depositAmt=y; 
     period=z; 
     if(accountType=="A") 
      interestAmt=depositAmt*0.5*period; 
     else if(accountType=="B") 
      interestAmt=depositAmt*0.15*period; 
     else if(accountType=="C") 
      interestAmt=depositAmt*0.25*period; 
    } 
}; 

class banktype2:public banktype1{ 
public: 
    void displayData(){ 
     cout<<interestAmt<<"\n"<<depositAmt<<endl; 
     cout<<"Total"<<interestAmt+depositAmt; 
    } 
}; 
class banktype3:public banktype2{ 

}; 
int main(){ 
    banktype3 b1; 
    b1.interestCal("A",1000,12); 
    b1.interestCal("B",1000,12); 
    b1.interestCal("C",1000,12); 
    b1.displayData(); 
    return 0; 
} 

在調用函數,我得到這個通知的地方:

轉換字符串文字到char *已棄用。

,並在條件,如果我得到的地方:

對一個字符串比較的結果是不確定的(使用STRNCMP代替)

+0

此代碼有*很多錯誤。因爲你沒有提出問題,所以很難幫助你。你明白這些錯誤信息是什麼意思嗎?如果是這樣,什麼阻止你修復它們?如果沒有,你應該這樣說 - 否則我們不知道你需要什麼幫助。 –

+1

Turbo C++已過時。它的最後一個版本是2006年。對於我來說,爲什麼講師會在2014年使用它來教C++,這是無法解釋的。 –

+0

@DavidSchwartz嗨。我有關於使用char和鉗子傳遞函數參數的不清楚的圖像。在閱讀一些答案後。例如:在刪除'const'之後,並在if比較時賦值*。 –

回答

1

字符串文字++有類型恆定系統字符arrrays的。

但在任何情況下,函數定義

void interestCal(char *x,int y,int z){ 
    accountType=&x; 
    depositAmt=y; 
    period=z; 
    if(accountType=="A"){ 
    interestAmt=depositAmt*0.5*period; 
    }else if(accountType=="B"){ 
    interestAmt=depositAmt*0.05*period; 
    }else if(accountType=="C"){ 
    interestAmt=depositAmt*0.05*period; 
    } 
} 

是錯誤的。

如果您將字符串文字作爲參數傳遞給函數,編譯器會警告第一個參數應具有類型const char *

本聲明

accountType=&x; 

是無效的。Thje右邊的操作數&x具有類型char **而左操作數的類型 char *,因爲如果else語句也無效ACCOUNTTYPE被聲明爲

char *accountType; 

if(accountType=="A"){ 

等。在這裏你試圖比較指針。

這將是更簡單,更正確,如果你定義ACCOUNTTYPE爲

char accountType; 

和功能將如下

void interestCal(char x, int y, int z) 
{ 
    accountType = x; 
    depositAmt = y; 
    period = z; 
    if (accountType == 'A') 
    { 
     interestAmt = depositAmt * 0.5 * period; 
    } 
    else if (accountType == 'B') 
    { 
     interestAmt = depositAmt * 0.05 * period; 
    } 
    else if(accountType == 'C') 
    { 
     interestAmt = depositAmt * 0.05 * period; 
    } 
} 

,它會從主被稱爲

b1.interestCal('A', 1000, 12); 
b1.interestCal('B', 1000, 12); 
b1.interestCal('C', 1000, 12); 

此外,如果確實這三個if-else語句具有相同的複合語句,那麼他們可能是rew ritten作爲一個如果聲明

if (accountType == 'A' || accountType == 'B' || accountType == 'C') 
    { 
     interestAmt = depositAmt * 0.05 * period; 
    } 
+0

只是一個小點:將函數聲明爲'interestCal(char accountType,int depositAmt,int period)'會更有意義。 – markgz

+0

@Vlad來自莫斯科啊是的謝謝。它的工作。我不知道爲什麼那麼我們愚蠢的老師總是使用指針的東西。當我們在函數中傳遞char時 –

2

使用std::string代替舊的C字符串,它們只是字符數組。

請注意,在C語言字符串的情況下,相等運算符只是比較數組的地址,但在std::string的情況下,其超載進行字符串比較。 (所以你的elseif代碼應該使用std::string)。

另請注意,使用C字符串時,應使用const char*,從不使用char*

解決方法很簡單(記住它是一個很好的經驗法則):除了極少數情況下,其neccesary,始終使用C++功能,而不是其C當量,在這種情況下只使用std::string。它的設計讓您的生活更輕鬆。

-1

啊工作。根據答案進行一些更改後。刪除const。和改變,如果(ACCOUNTTYPE == 'A'),以如果(* ACCOUNTTYPE == 'A')用C

#include <string> 
#include <iostream> 
#include <fstream> 
using namespace std; 

class banktype1{ 
public: 
    char *accountType; 
    float interestAmt,depositAmt,period,totalAmt; 
public: 
    void interestCal(char x,int y,int z){ 
     accountType=&x; 
     depositAmt=y; 
     period=z; 
     if(*accountType=='A'){ 
      interestAmt=depositAmt*0.5*period; 
     }else if(*accountType=='B'){ 
      interestAmt=depositAmt*0.05*period; 
     }else if(*accountType=='C'){ 
      interestAmt=depositAmt*0.05*period; 
     } 

    } 

}; 
class banktype2:public banktype1{ 
public: 
    void displayData(){ 
     cout<<interestAmt<<"\n"<<depositAmt<<endl; 
     cout<<"Total"<<interestAmt+depositAmt; 
    } 
}; 
class banktype3:public banktype2{ 

}; 
int main(){ 
    banktype3 b1; 
    b1.interestCal('A',1000,12); 
    b1.interestCal('B',1000,12); 
    b1.interestCal('C',1000,12); 
    b1.displayData(); 
    return 0; 
} 
+0

不要這樣做:您正在比較數組的第一個字符和一個字符,**不比較字符串**。請閱讀我的答案。 – Manu343726

+0

這不是一個答案。你甚至沒有解釋代碼的工作原理! – 0x499602D2

+0

@ Manu343726。我之前使用過String。但我們的講座總是使用'char'。爲什麼上面的代碼是錯的。 ? –