首页 > 解决方案 > C ++ 2d数组用文件中的最后一个元素值填充所有元素

问题描述

所以这是我编写的程序。出于某种原因,它用最后一个元素值填充数组的所有元素。该程序应该只是打开并读取文件。对行求和。对列求和,然后是所有元素的总和。

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
const int NUM_TYPE = 5; // num of types of salsa
const int NUM_SALE = 4; // num of quarters per salsa

void salsaIn(float salsa[][NUM_SALE]);
void salsaType(float salsa[][NUM_SALE], int NUM_TYPE, int NUM_SALE);
void salsaQ(float salsa[][NUM_SALE], int NUM_TYPE, int NUM_SALE);
void totalSalsa(float salsa[][NUM_SALE], int NUM_TYPE, int NUM_SALE);
void displayArray(float salsa[][NUM_SALE], int NUM_TYPE, int NUM_SALE);

int main()
{
    float salsa[NUM_TYPE][NUM_SALE];

    salsaIn(salsa);
    displayArray(salsa, NUM_TYPE, NUM_SALE);
    salsaType(salsa, NUM_TYPE, NUM_SALE);
    salsaQ(salsa, NUM_TYPE, NUM_SALE);
    totalSalsa(salsa, NUM_TYPE, NUM_SALE);

    return 0;
}
//reads the salsa.txt file
void salsaIn(float salsa[][NUM_SALE])
{
    float salsaT = 0;
    ifstream file("salsa.txt");

    if (file.is_open())
    {
        cout << "Successful. Reading data." << endl;
        while (!file.eof())
        {
            for (int i = 0; i < NUM_TYPE; i++)
            {
                for (int j = 0; j < NUM_SALE; j++)
                {

                    file >> salsaT;
                    if (salsa < 0)
                    {
                        cout << "error, value less than 0, set to 0"<<endl;
                        salsaT = 0;
                    }

                    salsa[i][j] = salsaT;
                }
            }
        }

    }
    file.close();

    return;
}
//calculates and displays sales for each salsa type
void salsaType(float salsa[][NUM_SALE], int NUM_TYPE, int NUM_SALE)
{
    for (int row = 0; row < NUM_TYPE; row++)
    {
        float total = 0;

        for (int col = 0; col < NUM_SALE; col++)
        {
            total += salsa[row][col];
        }
        //display the total sales for each salsa type.
        cout << "total sales for salsa " << (row + 1) << " is " << total
                << endl;

        return;
    }

}
void displayArray(float salsa[][NUM_SALE], int NUM_TYPE, int NUM_SALE)
{
    for (int row = 0; row < NUM_TYPE; row++)
    {
        float total = 0;

        for (int col = 0; col < NUM_SALE; col++)
        {
            cout << setw(5) << salsa[row][col] << endl;
        }
        //display the total sales for each salsa type.
    }

    return;
}
//calculates and displays the sales of each quarter.
void salsaQ(float salsa[][NUM_SALE], int NUM_TYPE, int NUM_SALE)
{
    for (int col = 0; col < NUM_SALE; col++)
    {
        float totalq = 0;

        for (int row = 0; row < NUM_TYPE; row++)
            totalq += salsa[row][col];

        cout << "Sales for quarter " << (col + 1) << " is " << totalq << endl;
    }

    return;

}
//calculates and displays the sales for the company last year.
void totalSalsa(float salsa[][NUM_SALE], int NUM_TYPE, int NUM_SALE)
{
    float totalS = 0;
    for (int row = 0; row < NUM_TYPE; row++)
    {

        for (int col = 0; col < NUM_SALE; col++)

            totalS += salsa[row][col];

    }

    cout << "The total sales for the company last year is " << totalS << endl;
}

这是程序的最终输出。

成功的。读取数据。
3905.64
3905.64
3905.64
3905.64
3905.64
3905.64
3905.64
3905.64
3905.64
3905.64
3905.64
3905.64
3905.64
3905.64
3905.64
3905.64
3905.64
3905.64
3905.64
3905.64
Salsa 1 的总销售额为 15622.6
第 1 季度的销售额为 19528.2
第 2 季度的销售额为 19528.2
第 3 季度的销售额为 19528.2
第 4 季度的销售额为 19528.2
公司去年总销售额为78112.8

谢谢你们的帮助。我有点卡在程序的其余部分上。

标签: c++

解决方案


这是怎么回事:

while (!file.eof())

循环直到设置 EOF 标志。在程序尝试从文件中读取值并找到文件末尾之前,不会设置此标志。这通常意味着在程序尝试从文件中读取一个值并因为没有更多值要读取而失败之前不会设置 EOF 标志。

{
    for (int i = 0; i < NUM_TYPE; i++)
    {
        for (int j = 0; j < NUM_SALE; j++)
        {

这些循环将尝试从文件中读取 20 个值

            file >> salsaT;

从文件中读取一个值。没有检查以确保从文件中读取了值。如果失败,salsaTC++11 之前的值不变。在 C++11 之后它归零。如果您不是为 C++11 编译,那么您重复的最后一个数字。

            if (salsa < 0) // unrelated bug here
            {
                cout << "error, value less than 0, set to 0"<<endl;
                salsaT = 0;
            }

            salsa[i][j] = salsaT;
        }
    }
}

从文件或循环的每次迭代中读取 20 个值while (!file.eof())。如果文件中正好有 20 个值,则读取所有 20 个值。可能没有设置EOF,所以while进入循环。另外 20 个值被读取到同一个数组中,但这次没有要读取的值。该数组很可能包含 20 次成功读取的最后一个值的重复。

解决方案:

while循环!

for (int i = 0; i < NUM_TYPE; i++)
{
    for (int j = 0; j < NUM_SALE; j++)
    { // read 20 values from file
        if (file >> salsaT)
        { // if we got a value
            if (salsaT < 0)
            {
                cout << "error, value less than 0, set to 0"<<endl;
                salsaT = 0;
            }
        }
        else
        { // Bad or no value. try to clean up.
            if (file.eof())
            {
                cout << "File ended too soon"<<endl;
            }
            else
            {
                cout << "error, bad value in file"<<endl;
                file.clear();
                file.ignore(numeric_limits<streamsize>::max(), '\n');
            }
            salsaT = 0;
        }
        salsa[i][j] = salsaT; // store
    }
}

推荐阅读