我的代碼可以編譯,但它不會返回字符ask,也不會跟隨else語句,因爲它會在輸入true之後關閉錯誤消息if聲明。在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 2014-09-30 21:49:29
'char chChar_a ='97';'這看起來不正確.. – 2014-09-30 21:49:44
您不需要將'char'轉換爲'int'來對它們進行算術運算,而' 97'應該是97(或者最好是'a'),'98'應該是98(或者最好是'b')。您也可以用'x'取代120,用'y'取代121,用'z'取代122。 – molbdnilo 2014-09-30 21:54:53