(多个cin的输入验证)有没有办法压缩这段代码? [英] (Input validation for multiple cin) is there any way to compress this code?

查看:86
本文介绍了(多个cin的输入验证)有没有办法压缩这段代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在此程序中添加一个输入验证功能,用户可以在其中为4个变量赋值。我在每次提示后都有一个if语句,供用户输入一个值。我是否必须在每次提示后都包含此编码,或者是否有办法简化此操作?不紧急,但很好奇。

I want to add an input validation feature to this program in which the user assigns values to 4 variables. I currently have an if statement after each prompt for the user to enter in a value. Do I have to include this coded after every prompt or is there a way to simplify this? Not urgent, but curious.

int main()
{
	double ux, uy, vx, vy, angle_rad, angle_deg, angle;
	string error = "Error: please enter valid components for the vectors.";
	cout << "Please enter the x-component of vector u: ";
	cin >> ux;
	if (cin.fail())
	{
		cout << endl << error << endl << endl;
		return 1;
	}
	cout << endl << "Please enter the y-component of vector u: ";
	cin >> uy;
	if (cin.fail())
	{
		cout << endl << error << endl << endl;
		return 1;
	}
	cout << endl << "Please enter the x-component of vector v: ";
	cin >> vx;
	if (cin.fail())
	{
		cout << endl << error << endl << endl;
		return 1;
	}
	cout << endl << "Please enter the y-component of vector v: ";
	cin >> vy;
	if (cin.fail())
	{
		cout << endl << error << endl << endl;
		return 1;
	}





我的尝试:



我尝试在所有提示结束时只放置一个字符串错误,但是一旦收到无效输入,它将在显示字符串错误之前显示其他cout显示。



What I have tried:

I have tried placing only one string error at the end of all the prompts, however as soon as an invalid input is received, it will still show the other cout displays before displaying the string error.

推荐答案

如果要打破无效输入,则必须检查每个输入的条件。但是你可以为每个打印错误信息的输入使用一个函数:



If you want to break upon an invalid input you have to check a condition at each input. But you can use a function for each input that prints the error message:

#include <float.h>

bool getDouble(double &val, const char *msg, double min = DBL_MIN, double max = DBL_MAX)
{
    bool bValid = true;
    cout << endl << msg;
    cin >> val;
    if (cin.fail())
    {
        cout << endl << "Error: please enter valid components for the vectors." << endl << endl;
        bValid = false;
    }
    else if (val < min)
    {
        cout << endl << "Error: Value is too small" << endl << endl;
        bValid = false;
    }
    else if (val > max)
    {
        cout << endl << "Error: Value is too large" << endl << endl;
        bValid = false;
    }
    return bValid;
}
</float.h>



然后为每个输入调用此函数:


Then call this function for each input:

if (!getDouble(ux, "Please enter the x-component of vector u: "))
    return 1;
if (!getDouble(uy, "Please enter the y-component of vector u: "))
    return 1;
// ...


另请注意,您的方案中有一些 OOP 的空间。您可以定义一个二维向量类,并为其重载输入(可能)输出运算符。
Also note there is room for some OOP in your scenario. You could define a bidimensional vector class and overload the input (and possibly) output operators for it.


这篇关于(多个cin的输入验证)有没有办法压缩这段代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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