检查字符是否属于数组(初学者C++)



我需要编写一个程序,要求输入三个字符,然后依次检查第一个、第二个和第三个字符是否属于字母表(小写或大写(。到目前为止,我只有这个

char v1, v2, v3;
cout << "Enter 3-character identifier: ";
cin >> v1 >> v2 >> v3;
if ((v1 != 'a')&&(v1!='b'))
cout << v1 << v2 << v3 <<" is an invalid input, check first charactern";
else if (v2 != 'a')
cout << v1 << v2 << v3 << "Invalid input, check second charactern";
else (v3 != 'a');
cout << v1 << v2 << v3 << "Invalid input, check third charactern";

我正在测试是否可以将这个字符与字母表进行比较,询问它是否不等于每个大写字母和小写字母,但这听起来很糟糕,所以我在"b"处停了下来。我似乎无法将字符与数组或字符串进行比较(显示错误(,这是我C++知识的范围。我似乎也不能工作;否则";函数仅在前两个条件为false时应用。非常感谢您的帮助!

您可以使用isalpha()函数进行相同操作。由于您提到您只学习了if/else等基本概念,因此您可以尝试这种方式,因为我在isalpha()函数中只使用了if/else条件来连续检查所提供的输入是否分别为字符。

#include<iostream>
#include<cctype>
using namespace std;
int main()
{
char a,b,c;
cin>>a>>b>>c;
if(isalpha(a) && isalpha(b) && isalpha(c) == 1)
{
cout<<"All of them are characters."<<endl;
}
else if(isalpha(a) == 1 && isalpha(b) ==1 && isalpha(c) == 0)
{
cout<<"The third input is not a valid alphabet.";
}
else if(isalpha(a) == 0 && isalpha(b) == 1 && isalpha(c) == 1)
{
cout<<"The first input is not a valid alphabet.";
}
else if(isalpha(a) == 1 && isalpha(b) == 0 && isalpha(c) == 1)
{
cout<<"The second input is not  valid alphabet.";
}
else if(isalpha(a) == 0 && isalpha(b) == 0 && isalpha(c) == 1)
{
cout<<"The first and second inputs are not valid alphabets.";
}
else if(isalpha(a) == 0 && isalpha(b) == 1 && isalpha(c) == 0)
{
cout<<"The first and third inputs are not valid alphabets.";
}
else if(isalpha(a) == 1 && isalpha(b) == 0 && isalpha(c) == 0)
{
cout<<"The second and third inputs are not valid alphabets.";
}
else if(isalpha(a) == 0 && isalpha(b) == 0 && isalpha(c) == 0)
{
cout<<"All of them are not valid alphabets.";
}
return 0;
}

不要忘记包含<cctype>头文件。请在此处查看输出文件。

相关内容

  • 没有找到相关文章

最新更新