如何让我的程序读取错误

How to get my program to read thru errors

本文关键字:读取 取错误 程序 我的      更新时间:2023-10-16

我有一项学校作业,目标是从文本文件中读取数据,并输出用这些信息进行的计算。我已经把输入和输出大部分放下了,但我们的教授希望我们也包括错误检查。我已经对字符串进行了错误检查,现在我正在处理ints/doubles。唯一的问题是,当我尝试使用他提供的示例错误输入文件时,我的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 << "nn";
outData << "Circle:n";
outData << "The total radius= " << tRad << ", area= " << tCarea << ", circumference= " << tCirc << "nn";
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。我们将非常感谢您的帮助!我很抱歉收到这么长的短信。

错误恢复很困难,而且没有标准方法(想想编译器如何从语法错误中恢复)。

while循环需要一个外循环。您将希望提出一种设计,以帮助您重新同步输入格式。再说一遍,这不是一项容易的任务。

一个简单的方法是对文本行进行计数,并显示存在故障的文本行,然后结束程序。从中可以显示更多信息,例如出现错误的字段。一些程序员认为最坏的情况是,一旦发现错误,剩余的数据就不可信了。

我建议设置一个简单的错误通知并安全地终止(清理、刷新到磁盘等)。努力让程序的其余部分正常工作,然后想出更好的错误处理策略。