这是我编写的程序。由于某种原因,它将最后一个元素值填充到数组的所有元素中。该程序应该只是打开并读取文件。对行求和。对列求和,然后对所有元素求和。
#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.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 total sales for salsa 1 is 15622.6 Sales for quarter 1 is 19528.2 Sales for quarter 2 is 19528.2 Sales for quarter 3 is 19528.2 Sales for quarter 4 is 19528.2 The total sales for the company last year is 78112.8
感谢帮助人员。我有点困在程序的其余部分。
答案 0 :(得分:1)
发生了什么事
while (!file.eof())
循环直到设置EOF标志。除非程序尝试从文件中读取值并找到文件的末尾,否则将不会设置此标志。通常,这意味着在程序尝试从文件中读取值并失败之前,因为没有更多的值可读取,因此EOF标志不会被设置。
{
for (int i = 0; i < NUM_TYPE; i++)
{
for (int j = 0; j < NUM_SALE; j++)
{
这些循环将尝试从文件中读取20个值
file >> salsaT;
从文件中读取一个值。没有检查以确保从文件中读取了一个值。如果失败,则C ++ 11之前的salsaT
中的值不变。在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
}
}