So this is my program that i have written. For some reason, it fills all of the elements of the array with the last elements value. The program is supposed to just open and read the file. Sum the rows. sum the columns then some the total of all of the elements.
#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;
}
This is the final output of the program.
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
Thanks for the help guys. I'm kinda of stuck on the rest of the program.