2014-04-04 7 views
-1

이 프로그램을 Turbo C++로 작성할 때 잘 동작합니다. 그러나 CodeBlocks, Xcode에 쓸 때 char과 관련된 오류가 발생하며 그 이유를 알 수 없습니다. 문자열로 사용할 char* name 같은 것을 선언 할 수 있다고 생각했습니다.포인터로 함수를 전달하는 포인터

이 프로그램은 다중 레벨 상속에 관한 것입니다.

전환에서 : 기능에 금리 및 기간을 전달,이 함수를 호출, 내가 갖는이 통지의 장소에서

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

type.` 계정에 따라 출력을 표시합니다 char *에 대한 문자열 리터럴은 더 이상 사용되지 않습니다.

도 내가 갖는 조건 경우의 장소에서

: 리터럴 문자열에 대한 비교

결과는 지정되지 않습니다 (대신 사용 strncmp)

+0

이 코드에는 오류가 * 많이 * 있습니다. 당신이 질문을하지 않았으므로 당신을 도울 수 없습니다. 이러한 오류 메시지의 의미를 이해합니까? 그렇다면 무엇이 그들을 고치지 못하게합니까? 그렇지 않다면, 당신은 그렇게 말해야합니다 - 그렇지 않으면 우리는 당신에게 필요한 도움이 무엇인지 모릅니다. –

+1

Turbo C++는 구식입니다. 마지막 발표는 2006 년입니다. 강사가 2014 년에 C++을 가르치기 위해 그것을 왜 사용하는지는 나에게는 설명 할 수 없습니다. –

+0

@DavidSchwartz 안녕하세요. 함수 매개 변수를 전달하는 char 및 pincers 사용에 대한 명확하지 않은 이미지가 있습니다. 몇 가지 대답을 읽은 후에. 예 : 'const'를 삭제 한 후 * if compare를 할당하면 효과가 발생합니다. –

답변

1

문자열 리터럴 경우이다.

그러나 어떤 경우에

함수 정의

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는 왼쪽 피연산자 다른 문은 또한 유효하지 않은 경우 ACCOUNTTYPE이

char *accountType; 

if(accountType=="A"){ 

로 선언 및 기타 있기 때문에 char *을 입력 가지고있는 동안 char ** 입력있다. 여기서 포인터를 비교하려고합니다.

그것은 더 올바르게

char accountType; 

로 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); 
로 메인에서 호출 될 경우 훨씬 더 간단하고 것

또한 실제로 3 개의 if-else 문이 동일한 복합 문을 갖고 있다면 rew 하나의 if 문으로 표현하기

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

그냥 사소한 점 :이 함수를'interestCal (char accountType, int depositAmt, int period)'로 선언하는 것이 더 합리적 일 것입니다. – markgz

+0

@Vlad from Moscow 아 그렇군요. 그것의 일했다. 왜 우리 바보 같은 선생님이 항상 포인터를 사용하는지 모르겠다. 함수에서 char을 전달할 때 –

2

사용 std::string 대신의 이전 C 문자열은 문자 배열입니다.

평등 연산자는 C 문자열의 경우 배열의 주소를 비교하기 만하지만 std::string의 경우에는 문자열 비교를 수행하기 위해 오버로드됩니다. (따라서 귀하의 elseif 코드는 std::string을 사용해야합니다).

C 문자열을 사용하는 경우 const char*을 사용하고 절대로 char*을 사용해야합니다.

이 솔루션은 (그리고 엄지 손가락의 좋은 규칙으로 기억) 간단하다 : 그 neccesary, 언제나 std::string를 사용,이 경우 대신 C 등가물의 기능을 C++를 사용 드문 경우를 제외하고 . 그것은 당신의 삶을 편하게하기위한 것입니다.

-1

아했습니다. 대답에 따라 약간의 변경을 한 후에 const을 삭제합니다. 변화하는 경우 (ACCOUNTTYPE == 'A') (* ACCOUNTTYPE == 'A')에 일정 charater arrrays 종류가 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'를 사용합니다. 왜 위 코드가 잘못된 것입니까? ? –

관련 문제