我有一个作业,可以根据文件中的输入来查找运输物品的成本。一个部分要求我在“文本文件中存在无效字符”时终止程序。
预期输出:
*************** File Read Error ***************
==> Reading of one or more package
==> parameters failed!!
==> Terminating program!!!
***********************************************`
我的输出:
--------------------------------------------------
The package attributes are:
Package type .................bulk
Total length .................45 inches
Weight .......................2111390112 pound(s)
--------------------------------------------------
The package rate factors are:
Package Base Rate ............4.75 dollars
Length Factor Used ...........2.5
Weight Factor Used ...........4.5
--------------------------------------------------
这是我的代码:
if (typeid(length) != typeid(int) || typeid(width) != typeid(int) || typeid(height) != typeid(int) || typeid(weight) != typeid(int)) // tests the type of these variables
{
cout << string(15,'*') << " File Read Error " << string(15,'*') << endl;
cout << "==> Reading of one or more package" << endl;
cout << "==> parameters failed!!" << endl;
cout << "==> Terminating program!!!" << endl;
cout << string(47,'*') << endl << endl;
return 1;
}
这是文本文件:
// invalid character in height, width, length or weight
bulk
20
25
A15
25
本质上,该程序在看到A15时应终止,因为它有一个字符并且只需要一个整数。