我有一个学校作业,其目标是从文本文件中读取数据,并输出使用该信息进行的计算。我的输入和输出大部分已经下降,但我们的教授希望我们也包括错误检查。我已经对字符串进行了错误检查,现在我正在进行整数/双打。唯一的问题是当我尝试使用他提供的示例错误输入文件时,我的while循环根本不会发射。
bool isWord(string s)
{
for (int i = 0; i < s.length(); ++i)
{
if (::isdigit(s[i])){
return false;
}
}
return true;
}
int main()
{
ifstream inData;
ofstream outData;
inData.open("inData.txt");
outData.open("outData.txt");
//check for Error
if (inData.fail()){
cout << "Error opening file!" << endl;
exit(1);
}
if (outData.fail()){
cout << "Error opening file!" << endl;
exit(1);
}
double rWidth;
double rLength;
double cRad;
double number = 0;
string fName;
string lName;
string word;
int age;
int i = 0;
int savings;
int count = 0;
int people = 0;
int tAge = 0;
int tSavings = 0;
string names[256];
double tLength = 0;
double tWidth = 0;
double tArea = 0;
double perimeter = 0;
double tRad = 0;
double tCarea = 0;
double tCirc = 0;
while(inData >> rWidth >> rLength >> cRad >> fName >> lName >> age >> savings){
cout << rWidth << " " << rLength << " " << cRad << " " << fName << " " << lName << " " << age << " " << savings << "\n";
stringstream fNameStream(fName);
stringstream lNameStream(lName);
while (fNameStream >> word)
{
if (isWord(fName))
{
names[count++] = word;
}
else
{
names[count++] = "John";
}
}
while (lNameStream >> word)
{
if (isWord(lName))
{
names[count++] = word;
}
else
{
names[count++] = "Doe";
}
}
istringstream widthStream(rWidth);
while (widthStream >> number)
{
if (widthStream >> number)
{
widthStream >> rWidth;
}
else
{
rWidth = 0.0;
}
}
tLength = tLength + rLength;
tWidth = tWidth + rWidth;
perimeter = perimeter + (2*(rWidth+rLength));
tArea = tArea + (rWidth*rLength);
tRad = tRad + cRad;
tCarea = tCarea + pow(cRad, 2) * M_PI;
tCirc = tCirc + (2*M_PI*cRad);
tAge = tAge + age;
tSavings = tSavings + savings;
}
cout << tLength << "\n";
cout << tWidth << "\n";
cout << perimeter << "\n";
cout << tArea << "\n";
cout << tCarea << "\n";
cout << tCirc << "\n";
cout << tAge << "\n";
cout << tSavings < "\n";
cout << count << "\n";
while (i < count)
{
cout << names[i] << " ";
i++;
}
outData << "Rectangle: \n";
outData << "The total length= " << tLength << ", width= " << tWidth << ", area= " << tArea << "\n";
outData << "Perimeter= " << perimeter << "\n\n";
outData << "Circle:\n";
outData << "The total radius= " << tRad << ", area= " << tCarea << ", circumference= " << tCirc << "\n\n";
outData << "People:\n" << "Total number of people= " << count << "\n" << "Total Age= " << tAge << "\nTotal Savings= " << tSavings;
inData.close();
outData.close();
system("pause");
return 0;
}
有条不紊地格式化了。
当我使用时,该程序运行良好和花花公子:
10.45 8.76
13.78
Jake Melon 45
7600
128 76.9
11
Mike Sander 56
800
15.9 43
6400
David James 32
87000.54
但是当我使用时:
10.45 aaaa
13.78
Jake Melon 45
7600
128 76.9
;
Mike Sander 23
800
15.9 43
w
David James i
87000.54
我的主要while循环(我读取数据的地方)由于某种原因没有触发,我的所有值都保持为0.我们将非常感谢您的帮助!我很抱歉这么长的文字。
答案 0 :(得分:0)
错误恢复很困难,并且没有标准方法(考虑编译器如何从语法错误中恢复)。
您的b
循环需要一个外循环。您将想要一个可以帮助您在输入格式上重新同步的设计。同样,这不是一件容易的事。
一种简单的方法是计算文本行并显示存在故障的文本行并结束程序。从中可以显示更多信息,例如出错的字段。一些程序员假设最糟糕的是,一旦发现错误,其余数据就不可信任。
我建议安装一个简单的错误通知并安全终止(清理,刷新到磁盘等)。努力让你的程序的其余部分正常工作,然后提出更好的错误处理策略。