C++ 如何检查输入是否为整数/字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13440831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I check if input is an integer/string?
提问by ghie
My problem here is I don't know how to insert a rule wherein if a user inputted a number on the string, it will cout
a warning saying it's not valid, same with if a user inputted a string/char on the grades. How? I've been trying it but the formula won't work.
我的问题是我不知道如何插入规则,如果用户在字符串上输入一个数字,它会cout
警告说它无效,与用户在成绩上输入字符串/字符相同。如何?我一直在尝试,但公式不起作用。
int x, cstotal = 100, extotal = 150;
double scorecs, exscore, labtotala, labtotalb, total;
string mystr = "";
cout << "Compute for: " << "\n" << "1. Laboratory Grade " << "\n" << "2. Lecture Grade" << "\n" << "3. Exit" << "\n";
cout << "Please enter a number: ";
cin >> x;
switch (x) {
case 1:
cout << "Compute for laboratory grade." << "\n";
cout << "Enter Student Name: ";
cin >> mystr;
cout << "Good day, " << mystr << " . Please provide the following grades: " << "\n";
cout << "CS Score: ";
cin >> scorecs;
cout << "Exam Score: ";
cin >> exscore;
labtotala = scorecs / cstotal * 0.6;
labtotalb = exscore / extotal * 0.4;
total = labtotala + labtotalb;
cout << "Your Laboratory Grade is " << total * 100 << "\n";
system("pause");
break;
case 2:
cout << "Compute for lecture grade." << "\n";
cout << "Enter Student Name: ";
cin >> mystr;
cout << "Good day, " << mystr << " . Please provide the following grades: " << "\n";
cout << "CS Score: ";
cin >> scorecs;
cout << "Exam Score: ";
cin >> exscore;
labtotala = scorecs / cstotal * 0.7;
labtotalb = exscore / extotal * 0.3;
total = labtotala + labtotalb;
cout << "Your Lecture Grade is " << total * 100 << "\n";
system("pause");
break;
回答by Aamir
回答by Some programmer dude
How about something like this:
这样的事情怎么样:
std::string str;
std::cin >> str;
if (std::find_if(str.begin(), str.end(), std::isdigit) != str.end())
{
std::cout << "No digits allowed in name\n";
}
The above code loops through the whole string, calling std::isdigit
for each character. If the std::isdigit
function returns true for any character, meaning it's a digit, then std::find_if
returns an iterator to that place in the string where it was found. If no digits were found then the end
iterator is returned. This way we can see if there was any digits in the string or not.
上面的代码遍历整个字符串,调用std::isdigit
每个字符。如果该std::isdigit
函数对任何字符返回 true,这意味着它是一个数字,则std::find_if
返回一个迭代器到字符串中找到它的那个位置。如果没有找到数字,end
则返回迭代器。这样我们就可以查看字符串中是否有任何数字。
The C++11 standard also introduced new algorithm functionsthat can be used, but which basically does the above. The one that could be used instead is std::any_of
:
C++11 标准还引入了可以使用的新算法函数,但基本上完成了上述工作。可以使用的一个是std::any_of
:
if (std::any_of(str.begin(), str.end(), std::isdigit))
{
std::cout << "No digits allowed in name\n";
}
回答by poorva
cout << "\n Enter number : ";
cin >> ch;
while (!cin) {
cout << "\n ERROR, enter a number" ;
cin.clear();
cin.ignore(256,'\n');
cin >> ch;
}
回答by Rahul Tripathi
Use the .fail()
method of the stream. Something like below:-
使用.fail()
流的方法。类似于以下内容:-
cin >> aString;
std::stringstream ss;
ss << aString;
int n;
ss >> n;
if (!ss.fail()) {
// int;
} else {
// not int;
}
回答by Amir Sasani
you could use cin.fail()
method! When cin
fails it will be true
and you could us a while
loop to loop until the cin
is true
:
你可以使用cin.fail()
方法!当cin
失败时true
,你可以使用一个while
循环来循环直到cin
is true
:
cin>>d;
while(cin.fail()) {
cout << "Error: Enter an integer number!"<<endl;
cin.clear();
cin.ignore(256,'\n');
cin >> d;
}
回答by feras abo-aloun
//this program really work in DEV c++
#include <iostream>
using namespace std;
int main()
{
char input;
cout<<"enter number or value to check"<<endl;
cin>>input;
for(char i='a';i<='z';i++)
{
if(input==i)
{
cout<<"character"<<endl;
exit(0);
}
}
for(int i=0;i<=1000;i++)
{
if(input==i)
{
cout<<"number"<<endl;
}
}
}