不确定如何从输出中排除数值

时间:2013-10-23 04:41:16

标签: c++ char int ascii

使用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;

2 个答案:

答案 0 :(得分:0)

你的错误在这里:

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

我想你想写:

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

顺便说一下,如果只是重新排序代码以改善locallity,则可以缩小代码:

#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;
}

答案 1 :(得分: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";
  }