473,890 Members | 1,825 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read sales data from a file into an array

5 New Member
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
6 2946
RRick
463 Recognized Expert Contributor
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
1,275 Recognized Expert Top Contributor
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
trl10
5 New Member
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
trl10
5 New Member
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
trl10
5 New Member
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
trl10
5 New Member
Thanks to all that offered help. I got it solved.
Aug 9 '07 #7

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

Similar topics

4
1866
by: pisscot | last post by:
Lind Apr 13, 6:32 am show options Newsgroups: comp.lang.c From: piss...@gmail.com (Lind) - Find messages by this author Date: 13 Apr 2005 06:32:15 -0700 Local: Wed,Apr 13 2005 6:32 am Subject: how to read it out using c++ Reply | Reply to Author | Forward | Print | Individual Message | Show original | Remove | Report Abuse
3
4409
by: Wei-Chao Hsu | last post by:
There are some data files look like 1.) data1.txt ---------------- 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 2.) data2.txt
19
2531
by: ranjeet | last post by:
Hay Guys can you all suggest me the points on the below issue Problem : The thing is that I have the data some thing like this. 1486, 2168, 3751, 9074, 12134, 13944, 17983, 19173, 21190, 21820, 1730, 2640, 3450, 4870, 6126, 7876, 15644, 17817, 20294, 21902, 2070, 3025, 4333, 5854, 7805, 9231, 10597, 16047........................... soo onnnnnn
9
5220
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still facing problems: Assume that FILE* filePointer; unsigned char lineBuffer;
6
2068
by: xdeath | last post by:
Hi guys, i've currently got an assignment, whereby, im supposed to create 2 classes, a Vehicle superclass, and a Taxi subclass. Vehicle class needs to have Reg Number, model, price, and Taxi is supposed to inherit those from it. I have done so, but now, it requires that all the data be stored in an array. The final program, should be able to : 1) Add new taxi's into the program (which retains even after program exit) 2) Search for a...
0
5805
by: lovecarole | last post by:
hi, i am the student who should write a program about reading wav file and do the DFT. actually i don't know how to read data of the wav song and save it into the array... if i want to read 17640 every times., and i set byte read=new byte, i have the wav format paper, but i don't know how to read the data correctly into the byte array, and used for doing DFT.... here's my code, and i am little hurry...thanks for the helps first~
7
3067
by: bowlderster | last post by:
Hello, all. This is the text file named test.txt. 1041 1467 7334 9500 2169 7724 3478 3358 9962 7464 6705 2145 6281 8827 1961 1491 3995 3942 5827 6436 6391 6604 4902 1153 1292 4382 9421 1716 2718 2895 I wanna to read the data to an array, as the follows:
13
10471
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another set of integers stored in an array.It would be a great help if you could provide some code for it.I tried the function fscanf but by that I am able to read only the first integer of the text file.Please help me.
5
11288
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a couple areas that have ASCII text that I need to extract. At the end of the 2580 bytes, I can read the report like a standard text file. It should have CR/LF at the end of each line. What is the best way for me to read this report using C#. It is...
0
9812
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11212
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10446
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6032
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4655
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4255
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3263
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.