1

我的问题是我不知道如何插入一个规则,如果用户在字符串上输入了一个数字,它会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;
4

6 回答 6

13

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

int x;
cin >> x;

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

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

if (cin.fail()) {
    // input was not valid
}
于 2012-11-18T14:08:18.870 回答
4

像这样的东西怎么样:

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";
}

上面的代码循环遍历整个字符串,调用std::isdigit每个字符。如果std::isdigit函数对任何字符返回 true,即它是一个数字,则std::find_if返回一个迭代器到字符串中找到它的那个位置。如果没有找到数字,end则返回迭代器。这样我们就可以看到字符串中是否有任何数字。

C++11 标准还引入了新的算法函数,可以使用,但基本做到以上。可以使用的一种是std::any_of

if (std::any_of(str.begin(), str.end(), std::isdigit))
{
    std::cout << "No digits allowed in name\n";
}
于 2012-11-18T14:08:16.313 回答
3
    cout << "\n Enter number : ";
    cin >> ch;
    while (!cin) {
        cout << "\n ERROR, enter a number" ;
        cin.clear();
        cin.ignore(256,'\n');
        cin >> ch;
    }
于 2013-12-01T11:27:04.033 回答
1

使用.fail()流的方法。如下所示:-

   cin >> aString;

  std::stringstream ss;
  ss << aString;
  int n;
  ss >> n;

  if (!ss.fail()) {
   // int;
  } else {
  // not int;
   }
于 2012-11-18T14:06:38.447 回答
1

你可以用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;
}
于 2016-05-30T15:24:13.087 回答
0
//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;   
    }
}
}
于 2020-01-08T01:10:24.593 回答