如何让我的程序读取2D数组输入文件

时间:2013-12-07 11:29:19

标签: c++ input multidimensional-array ifstream

以下是我输入文件中的几行:

  

35,35,21,0.6,25,2.8,3.4,13,8.7,0.43,0.81,400,480
  40,40,24,0.6,26,2.9,3.8,14,9.4,0.47,0.88,420,500

这是我尝试读取输入文件的代码片段:

cout<< "Please enter 1 if the timber is soft or 2 if the timber is hard."<<endl;
cin>> classs;

if (classs == 1)
{ 
    ifstream soft;
    soft.open ("Softwood.txt");

cout <<"Please enter the strength class of the timber, excluding the letter." <<endl;
cin >> type;

double a [12][13];

int i, j;

for (i=0; i<12; i++)
{
    for (int j=0; j<13; j++)
        soft>>a[i][j];
}

int m=0;

while(a[m][0]!= type)
{
    m++;
}

bendingStrength = a[m][1];
tensionParallel = a[m][2];
tensionPerpindicular = a[m][3];
compressionParallel = a[m][4];
compressionPerpindicular = a[m][5];
shearStrength = a[m][6];
elasticityParallel = a[m][7];
elasticityParallelFive = a[m][8];
elasticityPerpindicular = a[m][9];
shearModulus = a[m][10];
density = a[m][11];
meanDensity = a[m][12];

出于某种原因,它只是在此时停止while(a[m][0] = type),我不明白为什么。任何帮助将非常感激!谢谢!

1 个答案:

答案 0 :(得分:0)

如果未输入匹配值type,则会给出错误,因为m的值将超过数组中的行数。尝试使用以下代码:

while(a[m][0]!= type && m<12)
{
    m++;
}