A problem with your implementation is that it's possible that every 13th thing read in by ifstream is a newline character. Please post your output, along with your
exact target output.
You seem to be using ifstream correctly.
Except the while !infile line; this will print an indefinite loop of "error". I'd make this only print once by making it an if statement instead of a while loop. You might want to also make it a non-void function (int for instance), and do this:
if (!infile.open()) cout << "error"; return 1;
So that it exits the function. There may be a way to immediately exit a void function as well, but I haven't learned that yet (so this is probably not the only solution).
It seems that you're missing a line of the text file. At the end, you print out 6 values for a certain timber type, but there are only 6 lines of the file, and one looks like the timber type. You'll need to get that fixed to use what I suggest here.
But to store the values, I'd use separate arrays for clarity and so decimal places won't be added to the fields that are whole numbers. (Though this is merely personal preference.)
Here's how:
1 2 3 4 5 6 7 8 9
|
int TimberType[12]; int BendingStrength[12]; double ParallelTension[12];
int PerpendicularTension[12]; double ParallelCompression[12]; double PerpendicularTension[12]; int Shear[12];
// Use a for loop (1 to 12) to read in ALL timber types, then another to read in ALL
// bending strengths, etc.
// Then, you don't even need a switch clause. You could do something like this:
cin >> timberType; // Keeping this from your implementation
cout << "name of characteristic: " << correctarrayname[timberType] << endl;
// And so on for all 6 fields
|
I think using the switch clause is a huge unnecessary chunk of code.