user3087985
user3087985

Reputation: 1

How to read data from file into an array

I have this program to take properties from a data file and input them into calculations into the program. this is my code at the moment but it isn't taking any values into it.. any help is appreciated

float woodcharacStrength(){
    myInfile.open ("strength_classes.txt");     //inputs external file that contains characteristic values for forces parallel to grain.

    for (row = 0; row<3; row++)
        for (col = 0; col<18; col++)            //condition to only read certain rows and columns of the input file
        {
myInfile >> arraylocation[row][col];        //used to define each value of the array
        }

switch(woodType){
    case 'A':
    case 'a': ftk = arraylocation[0][0]; fck = arraylocation[1][0];break;
    case 'B':
    case 'b': ftk = arraylocation[0][1]; fck = arraylocation[1][1];break;
    case 'C':
    case 'c': ftk = arraylocation[0][2]; fck = arraylocation[1][2];break;
    case 'D':
    case 'd': ftk = arraylocation[0][3]; fck = arraylocation[1][3];break;
    case 'E':
    case 'e': ftk = arraylocation[0][4]; fck = arraylocation[1][4];break;
    case 'F':
    case 'f': ftk = arraylocation[0][5]; fck = arraylocation[1][5];break;
    case 'G':
    case 'g': ftk = arraylocation[0][6]; fck = arraylocation[1][6];break;
    case 'H':
    case 'h': ftk = arraylocation[0][7]; fck = arraylocation[1][7];break;
    case 'I':
    case 'i': ftk = arraylocation[0][8]; fck = arraylocation[1][8];break;
    case 'J':
    case 'j': ftk = arraylocation[0][9]; fck = arraylocation[1][9];break;
    case 'K':
    case 'k': ftk = arraylocation[0][10]; fck = arraylocation[1][10];break;
    case 'L':
    case 'l': ftk = arraylocation[0][11]; fck = arraylocation[1][11];break;
    case 'M':
    case 'm': ftk = arraylocation[0][12]; fck = arraylocation[1][12];break;
    case 'N':
    case 'n': ftk = arraylocation[0][13]; fck = arraylocation[1][13];break;
    case 'O':
    case 'o': ftk = arraylocation[0][14]; fck = arraylocation[1][14];break;
    case 'P':
    case 'p': ftk = arraylocation[0][15]; fck = arraylocation[1][15];break;
    case 'Q':
    case 'q': ftk = arraylocation[0][16]; fck = arraylocation[1][16];break;
    case 'R':
    case 'r': ftk = arraylocation[0][17]; fck = arraylocation[1][17];break;
}

    cout <<"The ftk value is: "<< ftk<< endl<<"The fck value is: "<< fck<<endl;

    return ftk;
    return fck;

    myInfile.close();
}

Upvotes: 0

Views: 95

Answers (2)

Thomas Matthews
Thomas Matthews

Reputation: 57688

Suggestions:
1) Look up std::toupper or std::tolower so you don't have to use both upper and lower case letters in your case statements.

2) Create a index by subtracting letters:

unsigned int index = std::toupper(woodType) - 'A';
ftk = arraylocation[0][index];
fck = arraylocation[1][index];

3) A function can only return one value: ftk or fck. If you want to return more than one value, pass them by reference or put them in a structure and return a copy of the modified structure.

4) No execution flows after a return statement, so your code will never execute the 2nd return statement or the fclose.

Upvotes: 1

Josh Engelsma
Josh Engelsma

Reputation: 2646

for (row = 0; row<3; row++) //you have no open and close braces for this for loop

Upvotes: 1

Related Questions