9 Replies - 3099 Views - Last Post: 08 August 2007 - 07:12 PM Rate Topic: -----

#1 trl10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-August 07

Read sales data from a file into an Array

Posted 06 August 2007 - 05:27 PM

I have asked my teacher to look at my code and this was his response.
The problem is you are passing an empty array.
You need to put data in the array first. You really need to write the readData function, to get data from the file and populate the array. My problem is that I can't figure out how to get the data into the array. I will post the partial code and then if more is needed, I will be glad to post all that I have done.
This is what I get for my output: The stored data is NaN
#include <iostream>
#include <fstream>

using namespace std;

//Constant array size declaration.
const int LOCATIONS = 20;

//Function Prototypes
void readData(double a[], double& data);

int main()
{ 
//Variable declarations
ifstream in; 
char in_file[256];
double salesData[LOCATIONS], totalSales, data, sales;


//Prompts the user for the input file name.
cout << "Enter the input file name:\n";
cin >> in_file;

// Opens the stream and connects to the file.
in.open(in_file);

//Checks to see if the input file opened properly.
//Displays an error message if file not opened.
if(in.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}
 readData(salesData, data);
 
 cout << endl << "The stored data is " << sales << endl;
 
 in.close( );
 return 0;
}

//Read data function.
void readData(double a[], double& data)
{
   double sales;

   for (int count = 1; count < data; count++)
   sales = data;
}

I'm attaching a sample file that we are to test with.

Attached File(s)

  • Attached File  sales.txt (85bytes)
    Number of downloads: 201


Is This A Good Question/Topic? 0
  • +

Replies To: Read sales data from a file into an Array

#2 PennyBoki  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 55
  • View blog
  • Posts: 2,345
  • Joined: 11-December 06

Re: Read sales data from a file into an Array

Posted 06 August 2007 - 05:42 PM

your readData function doesn't look good, please say with words what is your plan with that function, and how do you want to implement it, tell me what is &data, a[], sales and the purpose of your for loop, so that we can get started.
Was This Post Helpful? 0
  • +
  • -

#3 trl10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-August 07

Re: Read sales data from a file into an Array

Posted 06 August 2007 - 05:57 PM

View PostPennyBoki, on 6 Aug, 2007 - 05:42 PM, said:

your readData function doesn't look good, please say with words what is your plan with that function, and how do you want to implement it, tell me what is &data, a[], sales and the purpose of your for loop, so that we can get started.

My readData is suppose to read the data from a text file and store it into an array (I'm not understanding how to do that). &data is suppose to be the values stored in a[]. a[] is the array. The for loop I was just trying from an example that the teacher gave me. Here is what I had to begin with
//Read data function.
void readData(double a[], double& data)
{
   int next, index = 0;
   a[index] = next;
   index++;  
}

Some of this I was taking from examples out of my textbook and trying to manipulate it to work. I have three or four different programs with bits and pieces and I think sales was one that I used instead of the a. I'm starting to really confuse myself.
Was This Post Helpful? 0
  • +
  • -

#4 PennyBoki  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 55
  • View blog
  • Posts: 2,345
  • Joined: 11-December 06

Re: Read sales data from a file into an Array

Posted 06 August 2007 - 06:14 PM

OK I'll comment your last code:

Quote

//Read data function.
void readData(double a[], double& data)
{
int next, index = 0;
a[index] = next;
index++;
}


the effect of the function above is this:

Quote

int next, index = 0;
a[index] = next;

you've declared two variables next and index which are ints.
you set value 0 to index.

Quote

a[index] = next;
index++;

you are trying to assign the value of next to a[index], and because index has 0 that means you are assigning a value to the first element of the array which is a[0], and that's all cool, but the problem is in next which is declared but has no value, so you are assigning the value of next which has none to a[0].
And then you just increase index by 1 where it becomes 1.

at the end of your function you have this:
a[0]=???
index=1
Was This Post Helpful? 0
  • +
  • -

#5 trl10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-August 07

Re: Read sales data from a file into an Array

Posted 06 August 2007 - 06:35 PM

I'm not understanding what you are trying to say.
Was This Post Helpful? 0
  • +
  • -

#6 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: Read sales data from a file into an Array

Posted 07 August 2007 - 09:37 AM

Try This:

I let your ReadData function do all the work.. no use in passing it an array and a bunch of other useless parameters when all you want in the end is total sales (so it seems)... i only passed it totalSales(referenced) so that you can use that amount somewhere else in the program.. otherwise your function can just display that as well.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

//Constant array size declaration.
const int LOCATIONS = 20;

//Function Prototypes
void readData(double& totalSales);

void main()
{ 
	double totalSales = 0;

	readData(totalSales);
				cout << "\n\n\nTotal Sales: " << totalSales << endl;
}



//Read data function.
void readData(double& totalSales)
{
	double a[LOCATIONS] = {0};
	int nNumRecords = 0;
	ifstream in; 
	string strFileName;
	int numRecords = 0;
	double sales;

	//Prompts the user for the input file name.
	cout << "Enter the input file name: ";
	cin >> strFileName;

	// Opens the stream and connects to the file.
	in.open(strFileName.c_str());

	//Checks to see if the input file opened properly.
	//Displays an error message if file not opened.
	while(in.fail( ))
	{
		in.clear();
		system("cls");
		cout << "Cannot Open File: " << strFileName << endl << "\n\nPlease Re-Enter the input file name: ";
		cin  >> strFileName;
		in.open(strFileName.c_str());
	}
	
	system("cls");

	//Read in and Display Data
	cout << "***Data Read From File***" << endl << endl;
	while(!in.eof())
	{
		in >> a[nNumRecords];
		sales = a[nNumRecords];
		nNumRecords++;
		totalSales += sales;
	}

	cout << "Number Of Records In File: " << a[0] << endl << endl;

	for(int nIndex = 1; nIndex < nNumRecords; nIndex++)
	{
		cout << "Record Number " << nIndex << ": " << a[nIndex] << endl;
	}


	totalSales = (totalSales - nNumRecords);

	in.close();
}



From what i gathered it seems as though you want to read all of the sales into an array then calculate the total sales.. this does exactly that... if its not what you are looking for it at least shows you how to read into the array from the file.

***also added file name/existance error checking for you***


Input:
10
62458 81598 98745 53460 35678
89920 78960 124569 43550 45679

Output:
*** Data Read From File***

Number Of Records: 10

Record Number 1: 62485
Record Number 2: 81598
Record Number 3: 98745
Record Number 4: 53460
Record Number 5: 35678
Record Number 6: 89920
Record Number 7: 78960
Record Number 8: 124569
Record Number 9: 43550
Record Number 10: 45679


Total Sales: 714616

This post has been edited by Topher84: 07 August 2007 - 10:34 AM

Was This Post Helpful? 0
  • +
  • -

#7 trl10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-August 07

Re: Read sales data from a file into an Array

Posted 07 August 2007 - 09:09 PM

Quote

I let your ReadData function do all the work.. no use in passing it an array and a bunch of other useless parameters when all you want in the end is total sales (so it seems)... i only passed it totalSales(referenced) so that you can use that amount somewhere else in the program.. otherwise your function can just display that as well.

Thanks, but I do need all those functions. Could you break one down(a function) for me?


I manipulated the code around and got it to work. I need to write seperate function for the totalSales, I need one for the avgSales, highSales, and lowSales. I can't get it to read the data from my functions to do this. Any suggestions?
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);

This post has been edited by trl10: 07 August 2007 - 10:18 PM

Was This Post Helpful? 0
  • +
  • -

#8 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: Read sales data from a file into an Array

Posted 08 August 2007 - 08:15 AM

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

//Constant array size declaration.
const int LOCATIONS = 20;

//Function Prototypes
void readData(double& totalSales, double& avgSales, double& lowSales, double& highSales);

void main()
{ 
	double totalSales = 0;
	double avgSales   = 0;
	double lowSales   = 0;
	double highSales  = 0;

	readData(totalSales,avgSales,lowSales,highSales);

	cout << "\n\n\nTotal Sales:   " << totalSales << endl;
	cout << "Average Sales: " << avgSales << endl;
	cout << "High Sale:	 " << highSales << endl;
	cout << "Low Sale:	  " << lowSales << endl;
}



//Read data function.
void readData(double& totalSales, double& avgSales, double& lowSales, double& highSales)
{
	double a[LOCATIONS] = {0};
	int nNumRecords = 0;
	ifstream in; 
	string strFileName;
	int numRecords = 0;
	double sales;

	//Prompts the user for the input file name.
	cout << "Enter the input file name: ";
	cin >> strFileName;

	// Opens the stream and connects to the file.
	in.open(strFileName.c_str());

	//Checks to see if the input file opened properly.
	//Displays an error message if file not opened.
	while(in.fail( ))
	{
		in.clear();
		system("cls");
		cout << "Cannot Open File: " << strFileName << endl << "\n\nPlease Re-Enter the input file name: ";
		cin  >> strFileName;
		in.open(strFileName.c_str());
	}
	
	system("cls");

	//Read in and Display Data
	cout << "***Data Read From File***" << endl << endl;
	while(!in.eof())
	{
		in >> a[nNumRecords];
		sales = a[nNumRecords];
		nNumRecords++;
		totalSales += sales;
	}

	cout << "Number Of Records In File: " << a[0] << endl << endl;

	for(int nIndex = 1; nIndex < nNumRecords; nIndex++)
	{
		cout << "Record Number " << nIndex << ": " << a[nIndex] << endl;
	}

	highSales = a[1];
	for(int nIndex = 1; nIndex < nNumRecords; nIndex++)
	{
		if(a[nIndex] > a[nIndex-1] && a[nIndex] > highSales)
		{
			highSales = a[nIndex];
		}
	}

	lowSales = a[1];

	for(int nIndex = 1; nIndex < nNumRecords; nIndex++)
	{
		if(a[nIndex] < a[nIndex+1] && a[nIndex] < lowSales)
		{
			lowSales = a[nIndex];
		}
	}


	totalSales = (totalSales - nNumRecords);
	avgSales = (totalSales/nNumRecords);

	in.close();
}



The function now does total/avg/high/low of sales :ph34r:

Input:
10
62458 81598 98745 53460 35678
89920 78960 124569 43550 45679

Output:
*** Data Read From File***

Number Of Records: 10

Record Number 1: 62485
Record Number 2: 81598
Record Number 3: 98745
Record Number 4: 53460
Record Number 5: 35678
Record Number 6: 89920
Record Number 7: 78960
Record Number 8: 124569
Record Number 9: 43550
Record Number 10: 45679


Total Sales: 714616
Average Sales: 64965.1
High Sale: 124569
Low Sale: 35678

This post has been edited by Topher84: 08 August 2007 - 08:16 AM

Was This Post Helpful? 0
  • +
  • -

#9 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: Read sales data from a file into an Array

Posted 08 August 2007 - 11:47 AM

Here is the link to the snippet i wrote which should help you even more:

http://www.dreaminco...snippet1163.htm

This post has been edited by Topher84: 08 August 2007 - 11:58 AM

Was This Post Helpful? 0
  • +
  • -

#10 trl10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-August 07

Re: Read sales data from a file into an Array

Posted 08 August 2007 - 07:12 PM

Thanks so much for the help. I've played around with it and got it the way I need it. I am going to try and do a bubble sort for extra credit. I have 2 hours. Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1