C++ 如何在变量中只允许数字?
我的代码有一些问题,下面是我遇到问题的代码的一部分 这段代码的要点是,若用户输入的不是数字的东西,例如字符p,程序应该要求用户再次输入,这部分工作正常 若用户输入数字和字符的组合,程序应要求用户再次输入。例如,n12或12n无效C++ 如何在变量中只允许数字?,c++,io,numbers,iostream,cin,C++,Io,Numbers,Iostream,Cin,我的代码有一些问题,下面是我遇到问题的代码的一部分 这段代码的要点是,若用户输入的不是数字的东西,例如字符p,程序应该要求用户再次输入,这部分工作正常 若用户输入数字和字符的组合,程序应要求用户再次输入。例如,n12或12n无效 #include <iostream> using namespace std; int main () { int n; while(1) { cout<<"Enter a number, 0 f
#include <iostream>
using namespace std;
int main ()
{
int n;
while(1)
{
cout<<"Enter a number, 0 for exit: ";
cin>>n;
if(!cin)
{
cout<<"You didnt enter a valid number\n";
cin.clear();
cin.ignore(1000,'\n');
continue;
}
cout<<"Number is: "<<n<<endl;
if(n==0) return 0;
}
}
第一个字符出现的部分,如n12不会导致问题,但问题出现在第一个数字和第二个数字出现的部分,如12n,这是无效的,但我的代码打印出数字12,然后说该数字无效
#include <iostream>
using namespace std;
int main ()
{
int n;
while(1)
{
cout<<"Enter a number, 0 for exit: ";
cin>>n;
if(!cin)
{
cout<<"You didnt enter a valid number\n";
cin.clear();
cin.ignore(1000,'\n');
continue;
}
cout<<"Number is: "<<n<<endl;
if(n==0) return 0;
}
}
2°
3°
4°您可以使用,并且可以从其他标准库中使用,这是正常的,不是过度使用,就像这样,您将输入存储在字符串中,然后检查该字符串的每个字符isdigit函数是否成功,这意味着在这种情况下,输入纯粹是数字输入:
#include <iostream>
#include <cctype>
#include <string>
int main()
{
std::string str;
std::cin >> str;
(std::all_of(str.begin(), str.end(), [] (char c) { return isdigit(c); })) ? std::cout << "It's a number!\n" : std::cout << "isdigit() failed\n";
return 0;
}
您可以使用其他标准库,这是正常的,不是过度使用,像这样,您将输入存储在字符串中,然后检查该字符串的每个字符isdigit函数是否成功,这意味着在这种情况下,输入纯粹是数字输入:
#include <iostream>
#include <cctype>
#include <string>
int main()
{
std::string str;
std::cin >> str;
(std::all_of(str.begin(), str.end(), [] (char c) { return isdigit(c); })) ? std::cout << "It's a number!\n" : std::cout << "isdigit() failed\n";
return 0;
}
循环数字,如果数字不是数字,请打印您没有输入的有效数字,否则请打印数字。我们将以字符串形式获取输入,并使用ctype.h验证字符串的字符是否为数字:
#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;
int main()
{
string n;
bool is_valid = true;
while (1)
{
cout << "Enter a number, 0 for exit: ";
cin >> n;
for (size_t i = 0; i < n.length(); i++) {
if (!isdigit(n[i])) {
cout << "You didnt enter a valid number\n";
is_valid = false;
break;
}
}
if (is_valid) cout << "Number is: " << n << endl;
if (n == "0") return 0;
}
}
循环数字,如果数字不是数字,请打印您没有输入的有效数字,否则请打印数字。我们将以字符串形式获取输入,并使用ctype.h验证字符串的字符是否为数字:
#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;
int main()
{
string n;
bool is_valid = true;
while (1)
{
cout << "Enter a number, 0 for exit: ";
cin >> n;
for (size_t i = 0; i < n.length(); i++) {
if (!isdigit(n[i])) {
cout << "You didnt enter a valid number\n";
is_valid = false;
break;
}
}
if (is_valid) cout << "Number is: " << n << endl;
if (n == "0") return 0;
}
}
您需要检查用户输入的整行内容,而不仅仅是其中的一部分。当遇到不属于当前正在读取的数据类型的字符时,istream::operator>>停止读取。这就是为什么像12n这样的输入被分别处理为12和n 您不能仅使用功能来解决此问题。最好使用诸如std::getline和std::stoi/std::strtol之类的东西来处理这个问题,例如:
您需要检查用户输入的整行内容,而不仅仅是其中的一部分。当遇到不属于当前正在读取的数据类型的字符时,istream::operator>>停止读取。这就是为什么像12n这样的输入被分别处理为12和n 您不能仅使用功能来解决此问题。最好使用诸如std::getline和std::stoi/std::strtol之类的东西来处理这个问题,例如:
cin>>n后;您可以执行getline,然后检查这是否为空字符串这不是问题,而是cin的一个定义良好的行为。@nbro:如果您不知道如何实现所需的字符串,这可能仍然是一个问题。@derM确实,这就是为什么我编辑了我的评论,用引号将“问题”一词括起来。@noobcoder如果你的问题当然解决了,你是否考虑过接受答案,以便将问题标记为已解决?在cin>>n;您可以执行getline,然后检查这是否为空字符串这不是问题,而是cin的一个定义良好的行为。@nbro:如果您不知道如何实现所需的字符串,这可能仍然是一个问题。@derM确实,这就是为什么我编辑了我的评论,用引号将“问题”一词括起来。@noobcoder如果你的问题当然解决了,你是否考虑过接受答案,以便将问题标记为已解决?
Georgioss-MacBook-Pro:~ gsamaras$ g++ -Wall -std=c++0x main.cpp
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out
4
It's a number!
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out
f
isdigit() failed
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out
12n
isdigit() failed
#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;
int main()
{
string n;
bool is_valid = true;
while (1)
{
cout << "Enter a number, 0 for exit: ";
cin >> n;
for (size_t i = 0; i < n.length(); i++) {
if (!isdigit(n[i])) {
cout << "You didnt enter a valid number\n";
is_valid = false;
break;
}
}
if (is_valid) cout << "Number is: " << n << endl;
if (n == "0") return 0;
}
}
#include <iostream>
#include <string>
using namespace std;
bool to_int(const string &str, int &i)
{
size_t pos;
i = stoi(str, &pos);
return (str.c_str()[pos] == '\0');
}
int main ()
{
string line;
int n;
do
{
cout << "Enter a number, 0 for exit: ";
getline(cin, line);
if (!to_int(line, n))
{
cout << "You didn't enter a valid number\n";
continue;
}
cout << "Number is: " << n << endl;
if (n == 0) break;
}
while (1);
return 0;
}
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
bool to_int(const string &str, int &i)
{
char c;
istringstream iss(str);
iss >> i;
return !(iss.fail() || iss.get(c));
}
int main ()
{
string line;
int n;
do
{
cout << "Enter a number, 0 for exit: ";
getline(cin, line);
if (!to_int(line, n))
{
cout << "You didn't enter a valid number\n";
continue;
}
cout << "Number is: " << n << endl;
if (n == 0) break;
}
while (1);
return 0;
}