2009-11-19 5 views
0

올바르게 작동하는 다음 코드가 있습니다. 내가 다른 문 아무것도 추가 그러나 후 항상 이전이 제대로 입력이 무엇인지에 따라 평가하는 모든 다른C++ 문자열 조작 - stament가있는 경우

wgetstr(inputWin, ch); //get line and store in ch variable 
     str = ch;   //make input from char* to string 


     if(str=="m" || str=="M"){ 
      showFeedback("Data Memory Updated"); 
     } 
     if(str=="p" || str=="P"){ 
      showFeedback("Program Memory Updated"); 
     } 
     if(str=="g" || str=="G"){ 
      showFeedback("Accumulator, Program Counter, Zero Result Updated"); 
     } 
     if(str=="e" || str=="E"){ 
      showFeedback("Editing Mode Enabled"); 
     } 
     if(str=="c" || str=="C"){ 
      showFeedback("Program Copied Into Program Memory"); 
     } 
     if(str=="r" || str=="R"){ 
      showFeedback("Executing Program"); 
     } 
     if(str=="x" || str=="X"){ 
      showFeedback("Program Exited"); 
     } 

로 평가됩니다. ie "m"을 입력하면 showeFeedback ("Data Memory Updated")이 계속 호출되지만, 다음 else 문을 추가하면 입력 한 내용에 관계없이 항상 잘못된 명령 입력이 발생합니다.

else{ 
      showFeedback("Invalid Command Entered"); 
     } 
+0

이것은 사용자의 문제와 관련이 없지만 str을 대문자 (또는 소문자)로 변환하여 조건에 해당 문자를 모두 포함하지 않도록 할 수 있습니다. –

답변

9

사람들의 모든 별도의 경우 명령문입니다. 추가 한 다른 항목은 마지막 항목으로 만 이동합니다. 첫 번째 항목을 제외한 모든 항목을 else if으로 변경하면 예상대로 작동합니다.

+0

당신이 옳습니다. 내가 어떻게했는지 모르겠다. 나는 그 놈이 아니야. 감사. – user69514

5

첫 번째 것을 제외한 모든 항목에 대해 else를 사용해야합니다.

그래서 기존의 코드에 간단한 변경 :

 if(str=="m" || str=="M"){ 
      showFeedback("Data Memory Updated"); 
     } 
     else if(str=="p" || str=="P"){ 
      showFeedback("Program Memory Updated"); 
     } 
     else if(str=="g" || str=="G"){ 
      showFeedback("Accumulator, Program Counter, Zero Result Updated"); 
     } 
     else if(str=="e" || str=="E"){ 
      showFeedback("Editing Mode Enabled"); 
     } 
     else if(str=="c" || str=="C"){ 
      showFeedback("Program Copied Into Program Memory"); 
     } 
     else if(str=="r" || str=="R"){ 
      showFeedback("Executing Program"); 
     } 
     else if(str=="x" || str=="X"){ 
      showFeedback("Program Exited"); 
     } 
     else 
     { 
      showFeedback("Invalid Command Entered"); 
     } 
1

else를 추가 할 때 if (str == "x"|| str == "X") 행에 대해 - 그래서 X가 아닌 다른 것이 else 문을 치게됩니다.

나는 당신이 원하는 것은 물론 ifs를 "else if"로 변환하는 것이라고 생각한다. 물론 첫 번째 것은 제외한다.

3

또 다른 접근 방법이 필요 이런 종류의 정확히 존재하는 switch 문을 사용하는 것입니다 ..

예 :

char str = ch[0]; 

switch (str) 
{ 
    case 'm': 
    case 'M': { showFeedback("Data Memory Updated"); break; } 
    case 'p': 
    case 'P': { showFeedback("Program Memory Updated"); break; } 
    .... 
    default: { showFeedback("Invalid Command Entered"); } 
    /* default case is choosen if noone of the above is selected */ 
} 

편집 : 그냥 코멘트에 의심을 설명하기, char str = ch[0] 수단 은 문자열의 첫 번째 문자를 취해 여기에 넣습니다..

당신이 (== 또는 !=과) 직접적인 비교를하고 전체 문자열을 확인하려면

는 adeguate되지 않습니다 : 당신은 두 문자열이 동일한 경우 0을 반환 strcmp(char* str1, char* str2) 기능을 사용해야합니다.

+0

그래, 그걸 시도했지만 "mem"과 같은 것을 입력하면 어떤 이유에서 건 'm'을 호출 할 것이다. – user69514

+0

"어떤 이유"는 문자열의 첫 번째 문자를 검사하는 것이 나머지에 대해서는 아무 것도 가정하지 않기 때문에 " m "또는"mem "또는"mwhatever "는 동일합니다. 문자열이 'm'문자로 시작하는 경우 * 일종의 * – Jack

+0

스위치가 문자열에 작동하지 않습니다. –

0

toupper를 사용하여 문자열을 대문자로 변환하여 소문자 추측과 비교할 필요가 없으므로 문자를 조금 더 빠르게 만들 수 있습니다.