c ++从文本文件的数组中读取

时间:2013-12-10 02:12:08

标签: c++ arrays input

我正在尝试从.txt

中写的这个表中读取值
14, 14.00, 2.00, 1.70
16, 16.00, 2.20, 1.80
18, 18.00, 2.20, 2.00
20, 20.00, 2.30, 2.20
22, 22.00, 2.40, 2.40
24, 24.00, 2.50, 2.50
27, 27.00, 2.60, 2.80
30, 30.00, 2.70, 3.00
35, 35.00, 2.80, 3.40
40, 40.00, 2.90, 3.80
45, 45.00, 3.10, 3.80
50, 50.00, 3.20, 3.80

要求用户从第一列中选​​择一个值,程序应该在以下内容中分配相应的值: 第二列到fmk,第三列到fc90k,第四列到fvk

我使用另一个类似问题的回答来编写程序以进行报复。并且它对输入'14'正常工作,但随后为其他行提供了意外的数字。 我对c ++很新,所以任何帮助都会非常感激。感谢。

cout << "\n\n Please enter the joist strenth class you would like the program to test for,\n eg. for C14, type 14,\n    for C24, type 24 \n     " <<endl;
cin >> beamclass;

cout << "\n\n\n\n\n Thank you, the program shall now compute the beam design using your inputs \n\n\n" << endl;

// F I L E   &   A R R A Y

//this section opens the file, and converts the table into an array
ifstream infile;
infile.open("H:/My Documents/Computing 2/infile.txt");

float a [12][4]; // array starts here
int i;
float value;
char comma;
for (i=0; i<12; i++)
{
for (int j=0; j<4; j++)
{
        infile >> std :: ws >> value;   // 'ws' makes it ignore white space 
        a[i][j] = value;
        if (j != 12) //don't ignore comma for last entry on line bc no comma there
        {
            infile >> std :: ws >> comma; //ignores commas and whitespace
        }
}
}

//this section assigns the desired values from the array to the value name for the following equations

int x = 0;
while (a[x][0]!= beamclass && x<12)
{
    x++ ; 
}

fmyk = a[x][1];
fc90k = a[x][2];
fvk = a[x][3];

cout << "The value of fmky is      " << fmyk << "\n\n" << endl;
cout << "The value of fc90k is     " << fc90k << "\n\n" <<endl;
cout << "The value of fvk is       " << fvk << "\n\n" << endl; 

0 个答案:

没有答案