0

使用 C++ 我制作了这段代码来确定用户何时输入一个值,如果它是大写或小写值,或者根本不是一个值,那部分对我来说很容易,我还做了它,所以输出也将转换为小写输入到大写输入和大写输入到小写输入我的 proffesort 只允许我们使用 if else 语句意味着没有数组或向量,我不知道如何排除我的代码转换如果它是一个数字值我完全迷失在使用这个 ascii 表中。请帮助下面是我的代码

#include <iostream>
using namespace std;
int main()
{
char Letter; //defines the char variable letter for the user to input a value

cout<< "Please input a letter :)!" << endl;
cin>> Letter;

    if ( Letter>= 65 && Letter <= 90) //checks the acsii dec values to see if the input is a
        cout<< "This is an uppercase letter!"<<endl;//lowercase letter
else  if (Letter >= 97 && Letter <= 122)//checks to see if the acsii dec value is 
          cout<< "This is a lower case letter!"<<endl;//uppercase
      else 
          cout<<"This is not a letter :("<<endl;//outputs this if the acsii value is not a letter

if (Letter >=65&& Letter <= 90)
{
    Letter+=32;
    cout<< Letter<<" "<<"This is the Lowercase of your uppercase letter!\n";
}
else  (Letter >= 97 && Letter <=122);
{
    Letter-=32;
    cout<< Letter<<" "<<"This is the Uppercase of your lowercase letter!\n";
}

system("pause");
return 0;
4

2 回答 2

0

到目前为止,您的代码仅适用于字母,如果用户输入不是字母,它不会做任何事情,我不明白您想从代码中排除什么?你的意思是说你想使用其他东西而不是 assci 值。

在这种情况下,您可以使用以下代码,这将避免您需要担心或记住 ascii 数字的完整 ASCII 值

 if ( Letter>= 'A' && Letter <= 'Z') //checks the acsii dec values to see if the input is a
    cout<< "This is an uppercase letter!"<<endl;//lowercase letter
else  if (Letter >= 'a' && Letter <= 'z')//checks to see if the acsii dec value is 
      cout<< "This is a lower case letter!"<<endl;//uppercase
  else 
      cout<<"This is not a letter :("<<endl;//outputs this if the acsii value is not a letter

 if (Letter >='A'&& Letter <= 'Z')
 {
   Letter+='a'-'A';
    cout<< Letter<<" "<<"This is the Lowercase of your uppercase letter!\n";
 }
 else  (Letter >= 'a' && Letter <='z');
 {
   Letter-='a'-'A';
   cout<< Letter<<" "<<"This is the Uppercase of your lowercase letter!\n";
  }
于 2013-10-23T05:08:19.083 回答
0

你的错误在这里:

else  (Letter >= 97 && Letter <=122);

我猜你想写:

else if (Letter >= 97 && Letter <=122);
    ^^^^

顺便说一句,如果只是重新排序代码以提高局部性,您可以使代码更小:

#include <iostream>
using namespace std;
int main()
{
  char Letter; //defines the char variable letter for the user to input a value

  cout<< "Please input a letter :)!" << endl;
  cin>> Letter;

  if ( Letter>= 65 && Letter <= 90) {
    cout<< "This is an uppercase letter!"<<endl;
    Letter+=32;
    cout<< Letter<<" "<<"This is the Lowercase of your uppercase letter!\n";
  } else  if (Letter >= 97 && Letter <= 122) {
    cout<< "This is a lower case letter!"<<endl;
    Letter-=32;
    cout<< Letter<<" "<<"This is the Uppercase of your lowercase letter!\n";
  } else 
    cout<<"This is not a letter :("<<endl;

   return 0;
}
于 2013-10-23T05:00:00.033 回答