#include<iostream>
using namespace std;
void main()
{
char fname[11];
int x = 0;
cout << "Please enter the name: ";
cin >> fname;
while (fname[x] != '\0')
{
int i=int(fname[x]);
if (i>=97)
cout << fname[x];
x++;
}
else
cout << "Invalid characters";
system("pause");
}
我尝试使用上述代码验证字符输入。 但是做不到。
这些代码有什么问题?
最佳答案
- 在 C++ 中是
int main
而不是void main
, - 你应该使用
std::string
而不是 char 数组(更安全、更方便), - 用于测试单个字符是否为字母,
<cctype>
中定义了函数, 比如isalpha()
, - 没有“if”就不能有“else”。
尝试画出你想要实现的流程图。
关于C++验证疑惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4306688/