Reputation: 11
The goal of my program is to load data from a file into the sales array and then display each cities sales for each day before moving onto the next city. I am having problems passing my two dimensional SalesArray to my other functions. I am pretty sure it is because of the way I am declaring it in global and in main. I am also having a problem organizing it so it displays each city and days accordingly.
Any help would be great. Thank you
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <time.h>
using namespace std;
double SalesArray;
void GetSales();
void DisplaySales(double SalesArray[5][4]);
int main(){
DisplayHeading();
GetSales();
DisplaySales(double(SalesArray)[5][4]) ;
cout << endl;
system("pause");
return 0;
}
void GetSales(){
double SalesArray[5][4];
ifstream indata;
indata.open("sales.txt");
for (int row = 0; row < 5; row++){
for (int col = 0; col < 4; col++){
indata >> SalesArray[row][col] ;
}
}
//Close the File
//indata.close();
//cout << endl;
}
void DisplaySales(double SalesArray[5][4]){
for (int row = 0; row < 5; row++){
for (int col = 0; col < 4; col++){
cout << fixed << setprecision(2) << SalesArray[row][col] << endl;
}
}
// Array for City
const int SIZE = 5;
string city[SIZE] = {"New York" , "LA" ,
"Chicago" , "Springfield" ,
"Prophetstown" };
for (int count = 0; count < SIZE; count ++){
cout << city[count] << endl;
}
// Array for Days
const int SALES = 4;
string days[SALES] = {"Friday" , "Saturday" ,
"Sunday" , "Monday"
};
for (int count = 0; count < SALES; count ++){
cout << days[count] << endl;
}
}
Upvotes: 1
Views: 158
Reputation: 2250
Yes, remove the local declaration of SalesArray
in GetSales();
even better, make it an argument and pass it in just like DisplaySales()
.
Also, you should pass in all of SalesArray
, not the element at [5][4]
.
DisplaySales( SalesArray );
Note... the element at [5][4]
is beyond the end of the array. In C
, you declare the size N
, but only index to N-1
.
Upvotes: 1
Reputation: 464
The global you are declaring is a single double, not an array. To declare an array it should be something like double SalesArray[5][4];
In main() you are calling DisplaySales() wrong. Try something like this DisplaySales(SalesArray);
I agree with the other answers though, you should not declare it as a global and a local or pass the global in as a parameter to another function. Just pick one strategy.
Upvotes: 0
Reputation: 2432
The SalesArray you've declared inside GetSales()
is different to the one in main, once you leave that function it's gone. The simplest fix for that is to change the function prototype to GetSales(double SalesData[5][4])
and get rid of the function-scoped SalesArray.
Are you looking to display the data in a grid kind of format? If so, you need something like
//Display days along top axis
cout << "\t";
for (int count = 0; count < SALES; count ++){
cout << days[count] << "\t";
}
cout << endl;
for (int row = 0; row < 5; row++){
//display city for that row
cout << city[row] << "\t";
// display each
for (int col = 0; col < 4; col++){
cout << fixed << setprecision(2) << SalesArray[row][col] << "\t";
}
cout << endl;
}
You might have to play about with widths for it to display entirely correctly, but I think that's roughly what you need.
Upvotes: 0
Reputation: 7227
Why do you have multiple declarations of the variable SalesArray
? So you have a double SalesArray
in the global, then inside GetSales()
you have another double SalesArray[5][4]
. Also in this function, you store the value from the file to this local variable, hence the value is lost after the function finishes.
It's either you use global variable double SalesArray[5][4]
, or remove your global variable and pass the SalesArray[5][4]
to the GetSales
function, so that you can retrieve it later.
Upvotes: 0