當我在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代替)
此代碼有*很多錯誤。因爲你沒有提出問題,所以很難幫助你。你明白這些錯誤信息是什麼意思嗎?如果是這樣,什麼阻止你修復它們?如果沒有,你應該這樣說 - 否則我們不知道你需要什麼幫助。 –
Turbo C++已過時。它的最後一個版本是2006年。對於我來說,爲什麼講師會在2014年使用它來教C++,這是無法解釋的。 –
@DavidSchwartz嗨。我有關於使用char和鉗子傳遞函數參數的不清楚的圖像。在閱讀一些答案後。例如:在刪除'const'之後,並在if比較時賦值*。 –