1

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;
    }
}
1
  • 1
    Please do not post whole file. Try to put minimum code snippet that will show your problem. Remember to indent it. In your program you are declaring array incorrectly because you declare one value did you miss '*'? Read about pointers and arrays Commented Dec 10, 2013 at 0:52

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.