gpt4 book ai didi

C++ 二维数组用文件中的最后一个元素值填充所有元素

转载 作者:行者123 更新时间:2023-11-28 04:25:02 24 4
gpt4 key购买 nike

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

#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;
}

这是程序的最终输出。

Successful. Reading data.3905.643905.643905.643905.643905.643905.643905.643905.643905.643905.643905.643905.643905.643905.643905.643905.643905.643905.643905.643905.64total sales for salsa 1 is 15622.6Sales for quarter 1 is 19528.2Sales for quarter 2 is 19528.2Sales for quarter 3 is 19528.2Sales for quarter 4 is 19528.2The total sales for the company last year is 78112.8

谢谢大家的帮助。我有点卡在程序的其余部分了。

最佳答案

发生了什么:

while (!file.eof())

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

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

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

            file >> salsaT;

从文件中读取一个值。没有检查以确保从文件中读取了值。失败时,salsaT BEFORE C++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;
}
}
}

从文件或 while (!file.eof()) 循环的每次迭代中读取 20 个值。如果文件中恰好有 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
}
}

关于C++ 二维数组用文件中的最后一个元素值填充所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54528122/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com