【问题标题】:I can't get my program to read the values from my input file correctly (2D array)我无法让我的程序正确读取输入文件中的值(二维数组)
【发布时间】:2013-12-27 00:20:05
【问题描述】:

我的程序错误地读取了这个值.. 当我尝试从这个 infile 中获取值时也存在同样的问题:

14、14、8、0.4、16、2.0、1.7、7、4.7、0.23、0.44、290、350

16、16、10、0.5、17、2.2、1.8、8、5.4、0.27、0.5、310、370

18、18、11、0.5、18、2.2、2.0、9、6.0、0.30、0.56、320、380

20、20、12、0.5、19、2.3、2.2、9.5、6.4、0.32、0.59、330、390

22、22、13、0.5、20、2.4、2.4、10、6.7、0.33、0.63、340、410

24、24、14、0.5、21、2.5、2.5、11、7.4、0.37、0.69、350、420

27、27、16、0.6、22、2.6、2.8、11.5、7.7、0.38、0.72、370、450

30、30、18、0.6、23、2.7、3.0、12、8.0、0.40、0.75、380、460

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

45、45、27、0.6、27、3.1、3.8、15、10.0、0.50、0.94、440、520

50、50、30、0.6、29、3.2、3.8、16、10.7、0.53、1.00、460、550

进入这段代码

float a [12][13]; //begins the array so the input file can be read

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<12)
 {
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];

【问题讨论】:

    标签: c++ input multidimensional-array ifstream file-io


    【解决方案1】:
    float a [6][13]; // Begins the array to read the values from the hardwood file
    
    int i;
    for (i=0; i<6; i++)
    {
       for (int j=0; j<13; j++)
           hard>>a[i][j];
    }
    

    如果每个数字之间没有逗号,上述方法可能会起作用,但是,您的文件(至少是上面发布的那个)具有用逗号分隔的所有值..我相信这会破坏您的能力读入正确的值..(也不知道为什么要声明int i;

    试试下面的代码作为替代..

    float a [6][13]; // Begins the array to read the values from the hardwood file
    
    float value;
    char comma;
    for (i=0; i<6; i++)
    {
       for (int j=0; j<13; j++){
           hard>>std::ws>>value; //get value from file ignoring whitespace
           a[i][j] = value;
           hard>>std::ws>>comma; //ignore commas and whitespace
       }
    }
    

    你问题的第二部分确实有同样的问题......你没有考虑逗号......你以前喜欢过......

    float a [12][13]; //begins the array so the input file can be read
    
    int i, j;
    
    for (i=0; i<12; i++)
    {
        for (int j=0; j<13; j++)
            soft>>a[i][j];
    }
    

    你应该...

    float a [12][13]; // Begins the array to read the values from the hardwood file
    
    float value;
    char comma;
    for (i=0; i<12; i++)
    {
       for (int j=0; j<13; j++){
           soft>>std::ws>>value; //get value from file ignoring whitespace
           a[i][j] = value;
           if(j != 12){ //dont ignore the comma for last entry on line bc no comma there
           soft>>std::ws>>comma; //ignore commas and whitespace
           }
       }
    }
    

    【讨论】:

    • 太棒了,它适用于读取程序的那一部分,然后我尝试将它转移到程序的另一部分,它是相似的,但输入文件有更多的值,但它不起作用,这个是程序的那部分的样子!浮动 [12][13]; //开始数组,以便可以读取输入文件 int i, j; for (i=0; i>a[i][j]; } int m=0; while(a[m][0]!= type && m
    • 等等,这还没有正确出来,我将编辑我的问题。对不起!
    • 很高兴提供帮助,我很乐意提供更多帮助,但有几件事,首先,您应该接受一个答案来奖励帮助您的人,因为我确实解决了您的原始问题,其次,它会是如果您要单独发布该问题或在第一个问题的代码下方添加代码,则更好地帮助您解决另一个完全不同的问题,以便查看此问题的人可以看到原始问题是什么
    • 我已经对我的回答进行了修改以帮助您解决第二个问题
    • 谢谢,对不起,我对此很陌生,所以不知道做什么和不做什么!刚刚在那里完成,再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 2017-10-18
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多