donnytrona
donnytrona

Reputation: 1

c++ infile extraction checking if int

I'm fairly new to C++ and have no idea where to start with this or if its even possible, but i'm trying to check to see if length, width, radius, etc are actually doubles and if they are not return 0.

int main() {

ifstream inFile;
ofstream outFile;

string in;
double length, width, tLength = 0, tWidth = 0, areaRec, tAreaRec = 0, parameter, tParameter = 0; 
double radius, tRadius = 0, areaCirc, tAreaCirc = 0, circumference, tCircumference = 0;
double  savings, tSavings = 0; 
int people = 0, age, tAge = 0;

string name1, name2;

inFile.open("inData_with_error.txt");

outFile.open("outData_Normal.txt");
outFile << fixed << showpoint << setprecision(2);

if (!inFile) {
    cout << "ERROR: Unable to open file!" << endl;
    inFile.close();
}
else {
    cout << "Caculating..." << endl;

    while (inFile >> length >> width >> radius >> name1 >> name2 >> age >> savings) {
        cout << length << ", " << width << ", " << radius << ", " << name1 << ", " << name2 << ", " << age << ", " << savings << endl;

        tLength = tLength + length;
        tWidth = tWidth + width;

        parameter = length * 2 + width * 2;
        areaRec = length * width;
        tParameter = tParameter + parameter;
        tAreaRec = tAreaRec + areaRec;

        tRadius = tRadius + radius;

        areaCirc = pow(radius, 2) * PI;
        circumference = (radius * 2) * PI;
        tAreaCirc = tAreaCirc + areaCirc;
        tCircumference = tCircumference + circumference;

        people = people + 1;

        tAge = tAge + age;
        tSavings = tSavings + savings;
    }
}
cout << "Done" << endl;

outFile << "Rectangle:" << endl  << "Total length = " << tLength << ", " << "Total width = " << tWidth << ", " << "Total area = " << tAreaRec << ", " << "Total parameter = " << tParameter << endl << endl

<< "Circle:" << endl << "Total radius = " << tRadius << ", " << "Total area = " << tAreaCirc << ", " << "Total circumference = " << tCircumference << endl << endl

<< "Person: " << endl << "Total number of persons = " << people << endl << "Total age = " << tAge << endl << "Total savings = " << tSavings << endl;

inFile.close();
outFile.close();
system("pause");
return 0;

}

For example if my data with error is the below i want to catch the char and strings and return a 0 but have no idea how to tackle this question. Can someone lead me into the right direction?

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

Upvotes: 0

Views: 66

Answers (1)

R Sahu
R Sahu

Reputation: 206607

After the end of the while loop, you can add a test to check to whether the while loop got terminated due to EOF or due to an error in reading the data.

while (inFile >> length >> width >> radius >> name1 >> name2 >> age >> savings) { ... }

if ( inFile.eof() )
{
   // No errors in reading data
}
else
{
   // Error in reading data.
   // Deal with error
}

Upvotes: 1

Related Questions