如何检查输入是否为整数/字符串? [英] How do I check if input is an integer/string?

查看:178
本文介绍了如何检查输入是否为整数/字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我不知道如何插入规则,如果用户在字符串上输入一个数字,它会 cout 一个警告,说它无效,如果用户在等级上输入字符串/字符,则相同。怎么样?我一直在尝试,但公式不会工作。

  int x,cstotal = 100,extotal = 150; 

double scorecs,exscore,labtotala,labtotalb,total;

string mystr =;

cout<< 计算:< \\\
< 1.实验室级<< \\\
< 2.讲座班<< \\\
< 3.退出<< \\\
;
cout<< 请输入数字:;
cin>> X;
switch(x){
case 1:

cout< 计算实验室级。 << \\\
;
cout<< 输入学生姓名:;
cin>> mystr;


cout<< Good day,< mystr< 。请提供以下年级:< \\\
;
cout<< CS Score:;
cin>>评分

cout<< 考试成绩:;
cin>> exscore;


labtotala = scorecs / cstotal * 0.6;
labtotalb = exscore / extotal * 0.4;
total = labtotala + labtotalb;
cout<< 您的实验室成绩是<总* 100 < \\\
;
system(pause);
break;
case 2:
cout<< 计算等级。 << \\\
;
cout<< 输入学生姓名:;
cin>> mystr;
cout<< Good day,< mystr< 。请提供以下年级:< \\\
;
cout<< CS Score:;
cin>>评分
cout<< 考试成绩:;

cin>> exscore;
labtotala = scorecs / cstotal * 0.7;
labtotalb = exscore / extotal * 0.3;
total = labtotala + labtotalb;
cout<< 你的讲座成绩是<总* 100 < \\\
;
system(pause);
break;


解决方案

cin 设置 failbit 获取无效类型的输入。

  int x; 
cin>> X;

if(!cin){
//输入不是整数
}

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

  if(cin.fail()){
//输入无效
}
pre>

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.

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;

解决方案

cin sets a failbit when it gets input of an invalid type.

int x;
cin >> x;

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

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

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

这篇关于如何检查输入是否为整数/字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆