how to get my input file to work

Hello, i am a real beginner at c++ and am stuck on getting my input values read, just wondered if anyone could help me??

Here is my input file;
14 16 18 20 22 24 27 30 35 40 45 50
8 10 11 12 13 14 16 18 21 24 27 30
0.4 0.5 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.6 0.6 0.6
16 17 18 19 20 21 22 23 25 26 27 29
2.0 2.2 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.1 3.2
1.7 1.8 2.0 2.2 2.4 2.5 2.8 3.0 3.4 3.8 3.8 3.8

And here is my code so far:


void TimberType ()
{
ifstream inFile;
inFile.open("Table.txt");

while (!inFile)
{
cout << "error" << endl;
}

int row = 6, column = 12;

float Table [6][12];

for (int i=0; i < row && inFile; i++)
{
for (int j=0; j < column && inFile; j++)
{
inFile >> Table[i][j];
}
}
cout << "Please enter timber type " << endl;
cout << "C14 = 1 C16 = 2 C18 = 3 C20 = 4" << endl;
cout << "C22 = 5 C24 = 6 C27 = 7 C30 = 8" << endl;
cout << "C35 = 9 C40 = 10 C45 = 11 C50 = 12" << endl;
{
cin >> timberType;
switch (timberType)
{
case '1':
column = 0;
cout << "You have selected timber type C14" << endl;
break;
case '2':
column = 1;
cout << "You have selected timber type C16" << endl;
break;
case 3:
column = 2;
cout << "You have selected timber type C18" << endl;
break;
case 4:
column = 3;
cout << "You have selected timber type C20" << endl;
break;
case 5:
column = 4;
cout << "You have selected timber type C22" << endl;
break;
case 6:
column = 5;
cout << "You have selected timber type C24" << endl;
break;
case 7:
column = 6;
cout << "You have selected timber type C27" << endl;
break;
case 8:
column = 7;
cout << "You have selected timber type C30" << endl;
break;
case 9:
column = 8;
cout << "You have selected timber type C35" << endl;
break;
case 10:
column = 9;
cout << "You have selected timber type C40" << endl;
break;
case 11:
column = 10;
cout << "You have selected timber type C45" << endl;
break;
case 12:
column = 11;
cout << "You have selected timber type C50" << endl;
break;

}

inFile.close();


cout << "Bending Strength: "<< bendStr << "N/mm^2" << endl;
cout << "Tension in Parallel: "<< tensPara << "N/mm^2" << endl;
cout << "Tension in Perpendicular: "<< tensPerp << "N/mm^2" <<endl;
cout << "Compression in Parallel: "<< compPara << "N/mm^2" << endl;
cout << "Compression in Perpendicular: "<< compPerp << "N/mm^2" << endl;
cout << "Shear: "<< shear << "N/mm^2" << endl;

}
}

________________________________________________________________________________

Ideally if the user were to enter 1 for C14
the values from column one would be displayed.

Thank you
while (!inFile)
{
cout << "error" << endl;
}

If inFile does return an error value then the program is going to be stuck in an infinite loop. A better choice would be to add a cin.get(); so the user can create the missing file. Either that or use "if" instead of "while" and skip the entire program if the inFile is corrupt.

In order to display the values from a single column you would print the values from the specified column, starting from the first rown down to the last:

for(int i=0; i<row; i++)
cout << Table[i][column] << endl;
//or if you want to print them out on the same line use " " instead of endl
closed account (DEUX92yv)
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.
in reply to trojansdestroy

Here is the output i gain....

Bending Strength: 0N/mm^2
Tension in Parallel :0N/mm^2
Tension in Perpendicular:0N/mm^2
Compression in Parallel:0N/mm^2
Compression in Perpendicular:0N/mm^2
Shear:0N/mm^2

Target Output is ....
For C14:

Bending Strength: 14N/mm^2
Tension in Parallel :8N/mm^2
Tension in Perpendicular:0.4N/mm^2
Compression in Parallel:16N/mm^2
Compression in Perpendicular:2.0N/mm^2
Shear:1.7N/mm^2

and
if (!infile.open()) cout << "error"; return 1;

doesn't work for me?!

finally preferably i'd like to use the switch case statement. but no worries if not possible
@trojansdestroy

To exit a void function you can simply use "return". You forgot a pair of brackets here:

if (!infile.open()) {cout << "error"; return 1;}

Edit:
@OP

Well, you are not assigning anything to any of those 6 variables that's why they're all 0 (I reckon you declared them in the global scope).
bendStr should be Table[0][Column], tensPara - Table[1][Column], tensPerp - Table[2][Column] and so on. You could completely drop the variables and show the info directly from the table.

1
2
3
4
5
6
7
8
9
10
    
char info[6][30]=
    {
        {"Bending Strength: "},
        {"Tension in Parallel: "},
        {"Tension in Perpendicular: "},
        {"Compression in Parallel: "},
        {"Compression in Perpendicular: "},
        {"Shear: "},
    };


for(int i=0; i<row; i++)
cout << info[i] << Table[i][column] << "N/mm^2";
Last edited on
OMG IT WORKS
I COULD KISS YOU GOTHICFIGHTER
okay maybe i got a bit too excited....

im getting values now,

but they dont change dependent on the timberType entered
Try just using timberType-1 instead of column after reading the whole array.

1
2
3
4
5
6
7
8
9
10
11
12
cin >> timberType;
switch (timberType)
{
case '1':
timberType--;
cout << "You have selected timber type C14" << endl;
break;
case '2':
//rest of switch...

for(int i=0; i<row; i++)
cout << info[i] << Table[i][timberType] << "N/mm^2";
closed account (DEUX92yv)
For an inline if statement, aren't the brackets unnecessary? Or is it bad practice to put multiple actions within an inline statement? Because I've been doing the latter (without brackets) without any problem for a while now. Is that specific to void functions?
You have to use brackets if you want to execute more than one instruction in a control structure.

if (!infile.open()) cout << "error"; return 1;

Here return 1 gets executed even if infile is good so you must use brackets.
Topic archived. No new replies allowed.