【问题标题】:Not sure how to exclude number values from my output不确定如何从我的输出中排除数值
【发布时间】:2013-11-01 05:05:25
【问题描述】:

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

【问题讨论】:

  • 无需假设 ASCII。使用std::(is|to)(lower|upper)std::isalpha 进行字母检查。
  • 我真的不能使用那些标准我只能使用我已经输入到我的代码中的标题,我的老师说我应该使用 ascii 我只允许使用 if else 语句和条件跨度>
  • 就像我在输入 1 或 2 时为相应的 ascii 代码值实现 -= 和 += 32 的代码一样,我得到的 ascii 值已更改为,但我不确定如何避免这种情况

标签: c++ char int ascii


【解决方案1】:

你的错误在这里:

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

【讨论】:

  • 我可以给你业力或任何东西或任何东西来帮助我吗?我对这个网站很陌生:)
  • 欢迎使用 stackoverflow。我建议你看看:stackoverflow.com/about 这是重新开始的好方法
  • 是的,对不起,我没有足够的代表来支持你:p 如果可以的话,我肯定会:)
【解决方案2】:

到目前为止,您的代码仅适用于字母,如果用户输入不是字母,它不会做任何事情,我不明白您想从代码中排除什么?你的意思是说你想使用其他东西而不是 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";
  }

【讨论】:

  • 并且可以将重复的条件合二为一
  • 发生的事情是我要求用户输入一个理想的字母,然后我会使用 dec ascii 值将该值打印到定义为大写或小写值的屏幕上,然后使用ascii 表,以便将输入值转换为相反的值。例如,如果用户输入“a”,我会将其转换为“A”但是通过我编写的代码,如果他们输入 1,我将获得 1 +=32 和 -=32 的 ascii 值但是感谢其他人的帮助,我已经修好了:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-23
  • 2021-02-02
  • 1970-01-01
  • 2023-04-04
相关资源
最近更新 更多