为什么我的代码只出现一行?

问题描述 投票:0回答:1

我正在尝试从我的文件(salsa2.txt)中读取并且只显示其中的一行?我的文本文件如下所示:

Sweet and Spicy
1856.43   386.54   785.34  1043.60
Chunky
 847.50  1290.54  1506.80   899.65
Pico de Gallo
1092.65   689.54   987.65   1024.35
Salsa Con Queso
5698.54  7699.54  2345.67   2956.87
Restaurant Style
6758.43  8493.99  2098.54   3905.64

这是我的代码:

const int salsa_type = 5;   //Number of salsa types for rows
const int qurt = 4;         //Number of quarters for columns

bool readFile(string namesArray[], float ary2d[][qurt])                      //bool to return false or true if the data is less than 0
{
    float data;
    int i = 0;
    ifstream inFile;
    inFile.open("salsa2.txt");

    if(!inFile)
    {
        cout << "Error opening data file!\n";
        return false;                                   //returns false then returns to main to end program
    }

    cout << "Reading from data file. \n" << endl;

        for (int r = 0; r < salsa_type; r++)
        {
            getline(inFile, namesArray[r]);

            for (int c = 0; c < qurt; c++)
            {
                inFile >> data;
                ary2d[r][c] = inputValidation(data);        //Checking input before adding into index and will also move to the inputValidation prototype to double check
            }
        }

    inFile.close();

    return true;                                        //returns true then traces back into main function to complete calculations
}
.....
void readData(string namesArray[], float ary2d[][qurt], const float row_total[],
       int row, float col_total[], float total)
{

    cout << fixed << setprecision(2);

    cout << setw(45) << "Chips & Salsa\n" << endl;
    cout << setw(206) << "Quarter 1     Quarter 2    Quarter 3    Quarter 4    Type Totals" << endl;        //Printing the number of columns on the top

    for (int r = 0; r < row; r++)
    {
        cout << endl << namesArray[r] << ": ";

        for (int c = 0; c < qurt; c++)
        {
            cout << setw(13) << ary2d[r][c];                                                    //Has the data set spaced out evenly
        }

        cout << setw(15) << row_total[r];
        cout << endl;
    }

    cout << endl;

    cout << "Quarter totals: ";                                                     //Aligning row totals on the right side of the "table"

    //print total output for numbers
    for(int i=0; i < qurt; i++)
        cout << fixed << setw(13) << col_total[i];                                                   //Aligning column totals across the bottom of the "table"

    cout << endl;
    cout << endl << setw(73) << "Overall Total: " << setw(11) << total << endl;                                 //Aligning total to match the total for columns
}

应该是什么样的减去整体总数等等。

Sweet and Spicy        1856.43   386.54   785.34   1043.60
Chunky                  847.50  1290.54  1506.80    899.65
Pico de Gallo          1092.65   689.54   987.65   1024.35
Salsa Con Queso        5698.54  7699.54  2345.67   2956.87
Restaurant Style       6758.43  8493.99  2098.54   3905.64
c++ arrays file
1个回答
0
投票

我尝试了一种不同的方法来解决它并且它有效。所以,我写道:

    for (int r = 0; r < salsa_type; r++)
 { 
       getline(inFile, namesArray[r]); 
       for (int c = 0; c < qurt; c++) 
      { 
         inFile >> data; ary2d[r][c] = inputValidation(data); 
      } 
     inFile.clear(); 
     inFile.ignore(1000,'\n');
 } 
© www.soinside.com 2019 - 2024. All rights reserved.