如何验证用户将字符串输入到std :: cin中
How can I verify user inputted a string into std::cin?
students[#]
是结构的数组, students[#].name
是struct中的字符串,我想确保当用户输入该名称被确认为字符串时。<<<<<<<<<<<<<<<<<<<</p>
有人可以告诉我(简单?(完成这项工作的方法吗?
我尝试了以下内容。
bool checkString(const char *str)
{
bool isLetters = true;
while (*str != ' ')
{
if (*str < '65' || *str > '122')
{
//checking to see if it's over or below ASCII alpha characters
isLetters = false;
}
if (*str < '97' && *str > '90')
{
// checking to see if it's between capital and
//lowercase alpha char's
isLetters = false;
}
++str;
}
return isLetters;
}
main
{
//...
for (int i = 0; i < numStudents; ++i)
{
valid = true;
do
{
valid = true;
cout << "Enter NAME for student #" << i + 1 << ":" << endl;
cin >> students[i].name;
if (cin.fail() || !checkString(students[i].name.c_str()))
// tried to see if i could convert it to a c-style
//string to check char by char
{
cin.clear();
cin.ignore();
cout << "Please enter a valid input." << endl;
valid = false;
}
} while (!valid);
//...
return 0;
}
`
ascii代码是整数,不要将它们放在引号中。
bool checkString(const char *str)
{
bool isLetters = true;
while (*str != ' ')
{
if (*str < 65 || *str > 122)
{
//checking to see if it's over or below ASCII alpha characters
isLetters = false;
}
if (*str < 97 && *str > 90)
{
// checking to see if it's between capital and
//lowercase alpha char's
isLetters = false;
}
++str;
}
return isLetters;
}
您仍在做艰难的方法,该功能isalpha
是为此任务设计的
#include <cctype>
bool checkString(const char *str)
{
bool isLetters = true;
while (*str != ' ')
{
if (!isalpha(*str)))
isLetters = false;
++str;
}
return isLetters;
}
然后,当您意识到这不是字母时,您可以立即返回,进行检查
没有意义#include <cctype>
bool checkString(const char *str)
{
while (*str != ' ')
{
if (!isalpha(*str)))
return false;
++str;
}
return true;
}
您必须使用ASCII值检查每个char是否是字母。
bool checkString(string stdunt)
{
bool isLetters = true;
for (int i = 0; i < stdunt.length(); i++)
{
if (stdunt[i] < 65 || stdunt[i] > 122)
{ return false; }
if (stdunt[i] < 97 && stdunt[i] > 90)
{ return false; }
}
return isLetters;
}
int main()
{
int numStudents=5;
string students[numStudents];
for (int i = 0; i < numStudents; i++)
{
bool valid = true;
do{
valid = true;
cout << "Enter NAME for student #" << i + 1 << ":" << endl;
cin >> students[i];
// cout <<students[i];
if (cin.fail() || !checkString(students[i]))
{
cin.clear();
cin.ignore();
cout << "Please enter a valid input." << endl;
valid = false;
}
} while (!valid);
}
cout << "Complete";
return 0;
}
`
相关文章:
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 在一个模板函数中,若输入的类型是enum类,我该如何使用std::underlying_type
- 是否有类似std::lower_bound的函数,而不需要排序/分区输入
- 输入std::数组时出现问题
- ";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
- std::getline没有在while循环中重新请求用户输入
- 在输入句子时终止 std::out_of_range
- 检查输入 std::array 指针数据是否等于某个常量数组
- std::unordered_map 如果输入大小已知,如何优化批量插入
- C++ 限制或使用户输入不可见 (std::cin)
- 为什么 std::cin 不提示用户输入?
- 为什么从 std::cin 读取输入后右括号字符的值有时不是")"?
- 为什么不能通过在错误输入后设置 std::cin.clear() 来使用 std::cin?
- 我想知道为什么"std::unique_ptr<int> foo(新 int)"是合法的,因为"std::<int>unique_ptr"要求输入参数类型应该是"int"?
- 为什么这个程序接受整数和字母数字输入并打印它们? C++ 中的 std::string 也采用整数值吗?
- 当使用 std::CIN 输入结构时,它会出错
- C++ 入门 5th 1.4.4 为什么不能立即输入 std::cout 内容
- 如何将二进制十六进制文字输入 std::istream
- 为什么在gcc-4.9.1-4ubuntu2中没有手动输入std::vector
- 输入Std::cin与直接赋值Std::string变量不一样