如何在 C++ 中将用户输入限制为数字和字母
How to limit input from user to numbers and letters only in C++
程序只需要用户输入数字and/or字母。否则,程序必须终止。我不知道如何限制只能输入数字和字母。
这是我的程序:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input, reversed = "";
cout << "Enter string: ";
cin >> input;
for (int i = input.length() - 1; i < input.length(); i--)
{
reversed += input[i];
}
cout << reversed << endl;
if (reversed == input) cout << "It is a palindrome!";
else cout << "No, it is not a palindrome!";
return 0;
}
实现您想要的效果的最简单方法是检查每个字符。正如@Pete Becker 指出的那样,您可以使用 isalnum 来检查字符是数字还是字母字符:
#include <iostream>
#include <string>
int main () {
std::string s = "Hello W>orld";
for (auto c : s) {
if (!isalnum(c)) {
std::cout << "Found : '" << c << "'" << std::endl;
}
}
return 0;
}
输出:
Found : ' '
Found : '>'
正如@Someprogrammerdude 在评论中提到的那样,使用 std::all_of 和适当的二进制谓词(lambda 函数),您可以按如下方式轻松完成。
其次,为了检查字符串 input
到它的反向字符串,只需使用 std::string
的 the reverse iterators 创建一个临时字符串并检查它。这样,您就不需要另一个变量了。
希望评论能帮助您了解更多选项。
SEE LIVE
#include <iostream>
#include <cctype> // std::isdigit and std::isalpha and std::isalnum
#include <string>
#include <algorithm> // std::all_of
int main()
{
std::string input; std::cin >> input;
const auto check = [](const char eachCar)->bool{ return std::isalnum(eachCar); };
/* change return statement of lambda to
std::isdigit(eachCar) ---> for only digits
std::isalpha(eachCar) ---> for only letters
*/
if(std::all_of(input.cbegin(), input.cend(), check))
{
if(input == std::string(input.crbegin(), input.crend()))
std::cout << "It is a palindrome!";
else std::cout << "No, it is not a palindrome!";
}
return 0;
}
输入:
123kk321
输出:
It is a palindrome!
假设输入没有space,你可以这样做:
char c; string s; bool chk = true;
while (cin >> c)
{
if (('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
s.push_back(c);
else chk = false;
}
if (chk == true) cout << "Valid string";
else cout << "Invalid string";
当输入中没有剩余字符时,上面的代码将终止。
程序只需要用户输入数字and/or字母。否则,程序必须终止。我不知道如何限制只能输入数字和字母。
这是我的程序:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input, reversed = "";
cout << "Enter string: ";
cin >> input;
for (int i = input.length() - 1; i < input.length(); i--)
{
reversed += input[i];
}
cout << reversed << endl;
if (reversed == input) cout << "It is a palindrome!";
else cout << "No, it is not a palindrome!";
return 0;
}
实现您想要的效果的最简单方法是检查每个字符。正如@Pete Becker 指出的那样,您可以使用 isalnum 来检查字符是数字还是字母字符:
#include <iostream>
#include <string>
int main () {
std::string s = "Hello W>orld";
for (auto c : s) {
if (!isalnum(c)) {
std::cout << "Found : '" << c << "'" << std::endl;
}
}
return 0;
}
输出:
Found : ' '
Found : '>'
正如@Someprogrammerdude 在评论中提到的那样,使用 std::all_of 和适当的二进制谓词(lambda 函数),您可以按如下方式轻松完成。
其次,为了检查字符串 input
到它的反向字符串,只需使用 std::string
的 the reverse iterators 创建一个临时字符串并检查它。这样,您就不需要另一个变量了。
希望评论能帮助您了解更多选项。 SEE LIVE
#include <iostream>
#include <cctype> // std::isdigit and std::isalpha and std::isalnum
#include <string>
#include <algorithm> // std::all_of
int main()
{
std::string input; std::cin >> input;
const auto check = [](const char eachCar)->bool{ return std::isalnum(eachCar); };
/* change return statement of lambda to
std::isdigit(eachCar) ---> for only digits
std::isalpha(eachCar) ---> for only letters
*/
if(std::all_of(input.cbegin(), input.cend(), check))
{
if(input == std::string(input.crbegin(), input.crend()))
std::cout << "It is a palindrome!";
else std::cout << "No, it is not a palindrome!";
}
return 0;
}
输入:
123kk321
输出:
It is a palindrome!
假设输入没有space,你可以这样做:
char c; string s; bool chk = true;
while (cin >> c)
{
if (('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
s.push_back(c);
else chk = false;
}
if (chk == true) cout << "Valid string";
else cout << "Invalid string";
当输入中没有剩余字符时,上面的代码将终止。