내 코드는 컴파일 할 수 있지만 요청한 문자를 반환하지 않으며 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;
}
if 문에 대괄호가 없습니다. – MicroVirus
'char chChar_a = '97';이게 올바르게 보이지 않습니다 .. –
'char'을'int'로 캐스팅 할 필요가 없습니다. 97 '은 97 (또는 바람직하게는'a ')이어야하고'98 '은 98 (또는'b '가 바람직 함)이어야합니다. 120을 'x'로, 121을 'y'로, 122를 'z'로 바꿀 수 있습니다. – molbdnilo