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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:18:50  来源:igfitidea点击:

How do I check if input is an integer/string?

c++

提问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 couta 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

cinsets a failbitwhen it gets input of an invalid type.

cinfailbit当它获得无效类型的输入时设置 a 。

int x;
cin >> x;

if (!cin) {
    // input was not an integer
}

You can also use cin.fail()to check if the input was valid:

您还可以使用cin.fail()来检查输入是否有效:

if (cin.fail()) {
    // input was not valid
}

回答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::isdigitfor each character. If the std::isdigitfunction returns true for any character, meaning it's a digit, then std::find_ifreturns an iterator to that place in the string where it was found. If no digits were found then the enditerator 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 cinfails it will be trueand you could us a whileloop to loop until the cinis true:

你可以使用cin.fail()方法!当cin失败时true,你可以使用一个while循环来循环直到cinis 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;   
    }
}
}