By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,378 Members | 3,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,378 IT Pros & Developers. It's quick & easy.

Read sales data from a file into an array

P: 5
Hi Everyone,
I'm still trying to learn C++ and this is my final project. We have to read sales data from a file into an array, process data in a partially filled array via functions, pass an array as a paameter to a function. I have most f the cde wrote, except that I can't get my readData function to work. I can't figure out hwto get it to read from the file and into an array. Here is the partial code
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. //Constant array size declaration.
  7. const int LOCATIONS = 20;
  8.  
  9. //Function Prototypes
  10. void readData(double a[], double& data);
  11.  
  12. int main()
  13. //Variable declarations
  14. ifstream in; 
  15. char in_file[256];
  16. double salesData[LOCATIONS], totalSales, data;
  17.  
  18.  
  19. //Prompts the user for the input file name.
  20. cout << "Enter the input file name:\n";
  21. cin >> in_file;
  22.  
  23. // Opens the stream and connects to the file.
  24. in.open(in_file);
  25.  
  26. //Checks to see if the input file opened properly.
  27. //Displays an error message if file not opened.
  28. if(in.fail( ))
  29. {
  30. cout << "Input file opening failed.\n";
  31. exit(1);
  32. }
  33.  readData(salesData, data);
  34.  
  35.  cout << endl << "The stored data is " << data << endl;
  36.  
  37.  in >> data;
  38.  in.close( );
  39.  return 0;
  40. }
  41.  
  42. //Read data function.
  43. void readData(double a[], double& data)
  44. {  
  45.    ifstream in;
  46.    int next, index;
  47.    while (in >> next)
  48.    a[index] = next;
  49.    index++;  
  50. }
Any suggestions wuld be greatly appreciated. I will be happy to send my full code if it is needed. Thanks.
Aug 7 '07 #1
Share this Question
Share on Google+
6 Replies


RRick
Expert 100+
P: 463
The problem with reading the data file is on line 46. You are declaring a local ifstream inside the subroutine. This means you have two ifstreams declared in your program. One that has opened a file (in main) and one that hasn't ( in readdata). Watch out here, even though they have the same name, they are different. This is what "scope" is all about.

The easiest solution is to pass the ifstream form main to the readData sub. Be sure to pass it as a reference because I'm not sure what will happen if you pass it by value. Add a third parameter that looks like:

readData( ...., ..., ifstream & in)
Aug 7 '07 #2

gpraghuram
Expert 100+
P: 1,275
hI,
In the readdata function where r u opening the file.
U have declared the object but where r u opening the file?

Raghuram
Aug 7 '07 #3

P: 5
The problem with reading the data file is on line 46. You are declaring a local ifstream inside the subroutine. This means you have two ifstreams declared in your program. One that has opened a file (in main) and one that hasn't ( in readdata). Watch out here, even though they have the same name, they are different. This is what "scope" is all about.

The easiest solution is to pass the ifstream form main to the readData sub. Be sure to pass it as a reference because I'm not sure what will happen if you pass it by value. Add a third parameter that looks like:

readData( ...., ..., ifstream & in)
With this function setup can you use arrays and ifstream& i.e.
Expand|Select|Wrap|Line Numbers
  1.  readData(const double a[], data, ifstream& in)
This is what I was unclear on as well.
Aug 7 '07 #4

P: 5
hI,
In the readdata function where r u opening the file.
U have declared the object but where r u opening the file?

Raghuram
That's where I was stuck, I wasn't sure if I had my function setup correctly.
Aug 7 '07 #5

P: 5
I have my data reading in, but now I can't get my functions to work. Here is a sample of one of my functions.
[code]double sumArray(double& totalSales)
{
ifstream in;
double a[LOCATIONS] = {0}, sales;
int data = 0;

//Reads in and displays data.
while(in >> a[data])
{
for(int index = 1; index < data; index++)
{
totalSales = totalSales + a[index];
index++;
}
}

totalSales = (totalSales - data);
I have to have a sumArray, avgSales, highSales, and lowSales. Any suggestions?
Aug 8 '07 #6

P: 5
Thanks to all that offered help. I got it solved.
Aug 9 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.