2014-09-30 8 views
-1

내 코드는 컴파일 할 수 있지만 요청한 문자를 반환하지 않으며 true 문에서 입력 한 후에 오류 메시지가 나오기 때문에 else 문을 따르지 않습니다. 성명서. 초보자 C + + 그래서 어떤 도움을 주셔서 감사합니다."잘라내 기는 int에서 char로"어떤 결과도 생성하지 않습니다.

// Python Challenge 2.cpp : This program will take a line of text from the user and then translate each letter 2 over in the alphabet. 
// 

#include "stdafx.h" 

#include <iostream> 

using namespace std; 
char chChar; 
char chChar2; 
char chChar_a; 
char chChar_b; 
int main() 
{ 
    //This takes the one letter input from the user: 
    cout << "Type in a lowercase letter: "; 
    cin >> chChar; 

    //for letters a-x 
    if ((int)chChar >= 97 && (int)chChar <= 120) 
     char chChar2 = (int)chChar + 2; 
     cout << "is now: " << chChar2 << endl; 

     //for the letter y 
     if ((int)chChar == 121) 
     { 
      char chChar_a = '97'; 
      cout << "is now: " << chChar_a << endl; 
     } 

     //for the letter z 
     if ((int)chChar == 122) 
     { 
      char chChar_b = '98'; 
      cout << "is now: " << chChar_b << endl; 
     } 

    //for everything else 
    else 
     cout << "Error: type in a lowercase letter." << endl; 

     return 0; 
} 
+0

if 문에 대괄호가 없습니다. – MicroVirus

+0

'char chChar_a = '97';이게 올바르게 보이지 않습니다 .. –

+2

'char'을'int'로 캐스팅 할 필요가 없습니다. 97 '은 97 (또는 바람직하게는'a ')이어야하고'98 '은 98 (또는'b '가 바람직 함)이어야합니다. 120을 'x'로, 121을 'y'로, 122를 'z'로 바꿀 수 있습니다. – molbdnilo

답변

0

귀하의 if 문은 정확하지 않은 : 당신이 그것을 후에 { }를 사용하여 블록을 생성하는 것을 잊었다.

//for letters a-x 
if ((int)chChar >= 97 && (int)chChar <= 120) 
{ 
    char chChar2 = (int)chChar + 2; 
} 
// Always runs the next part 
cout << "is now: " << chChar2 << endl; 
... 

을 그리고 마지막 else는 전에 if에 연결되어 : 약자로

코드는 않습니다

//for the letter z 
if ((int)chChar == 122) 
{ 
    char chChar_b = '98'; 
    cout << "is now: " << chChar_b << endl; 
} 
else 
{ 
    cout << "Error: type in a lowercase letter." << endl; 
} 

적절한 브라켓 { }를 추가,이 문제를 해결하려면. 그들이이와 함께, 귀하의 고정 된 코드가 보일 것입니다,


그래서 C에서 의미가 없습니다 : 들여 쓰기가 너희를 속이는하지 않습니다 - 괄호없이 if는 조건부로 다음 문, 그리고 블록을 실행 같은이에서 시작

//This takes the one letter input from the user: 
cout << "Type in a lowercase letter: "; 
cin >> chChar; 

//for letters a-x 
if ((int)chChar >= 97 && (int)chChar <= 120) 
{ 
    char chChar2 = (int)chChar + 2; 
    cout << "is now: " << chChar2 << endl; 

    //for the letter y 
    if ((int)chChar == 121) 
    { 
     char chChar_a = '97'; 
     cout << "is now: " << chChar_a << endl; 
    } 

    //for the letter z 
    if ((int)chChar == 122) 
    { 
     char chChar_b = '98'; 
     cout << "is now: " << chChar_b << endl; 
    } 
} 
//for everything else 
else 
{ 
    cout << "Error: type in a lowercase letter." << endl; 
} 

return 0; 

, 당신은 더 문제에 대한 코드를 디버깅 할 수 있습니다.

+0

정말 고마워요. 나는 그것이 내 대괄호를 다뤄야 만한다는 것을 알고 있었지만 다른 배치를 테스트하고 어디서 잘못되었는지 이해하지 못했습니다. 당신의 솔루션을 보는 것은 미래에이 에러를 피하는 방법을 가르쳐주었습니다. 고맙습니다. –

+0

지금은 y와 z에 대한 내 문장이 작동하지 않습니다. 지금 고치려고. –

+0

y와 z가 작동하도록하려면 z의 if 문 앞에 else를 추가해야했습니다. 이제 z 문은 else if ((int ch char == 122))를 읽습니다. 모든 것이 제대로 작동하고 있습니다. –

관련 문제