| | |
Using functions with arrays
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2007
Posts: 3
Reputation:
Solved Threads: 0
Hi Everyone,
I have to read data from a file into an array. I have that part figured out. I can't get my function to work. I have to write a sumArray, avgSales, highSales, and lowSales. Here is what I got so far. Could someone please help me with one of the functions. I think I can get the rest if I have a god example to follow. Thanks in advance.
My total sales output is $0.00, so I know that it's not reading in correctly. Please guide me.
I have to read data from a file into an array. I have that part figured out. I can't get my function to work. I have to write a sumArray, avgSales, highSales, and lowSales. Here is what I got so far. Could someone please help me with one of the functions. I think I can get the rest if I have a god example to follow. Thanks in advance.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <fstream> using namespace std; //Constant array size declaration. const int LOCATIONS = 20; //Function Prototypes void readData(int a[], int size, double& data); double sumArray(double& totalSales); int main() { cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); ifstream in; char inFile[256]; double a[LOCATIONS] = {0}, sales, totalSales = 0; int data = 0; //Prompts the user for the input file name. cout << "Enter the input file name: "; cin >> inFile; // Opens the stream and connects to the file. in.open(inFile); //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); //Closes file explicitly. in.close( ); } //Finds and prints the total sales amount. sumArray(totalSales); cout << "The total sales are $" << totalSales << endl; //Reads in and displays data. while(in >> a[data]) { sales = a[data]; data++; } cout << "Number Of Locations In File: " << a[0] << endl << endl; for(int index = 1; index < data; index++) { cout << "Location Number " << index << ": $" << a[index] << endl; } } 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++; return(totalSales); } } }
Last edited by Ancient Dragon; Aug 8th, 2007 at 2:29 am. Reason: added language to code tags to get line numbers
line 29: open() wants a const char* for the filename and you passed a std::string object. I'm supprised your compiler did not object and spew out some nasty error messages. It should be this:
line 39: unnecessary -- if open() failed the stream can not be closed.
line 73: input file was never opened so lines 73-82 will probably never get executed.
lines 75-80 should not be inside the while loop beginning at line 73 because total sales will get counted several times. Move that for loop outside and below the while loop.
Actually, in sumArray() you don't need that double array at all because you don't do anything with it. A better solution is to read the array into memory once in main() then pass the array to sumArray() function. Just add another parameter to sumArray().
in.open(inFile.c_str());line 39: unnecessary -- if open() failed the stream can not be closed.
line 73: input file was never opened so lines 73-82 will probably never get executed.
lines 75-80 should not be inside the while loop beginning at line 73 because total sales will get counted several times. Move that for loop outside and below the while loop.
Actually, in sumArray() you don't need that double array at all because you don't do anything with it. A better solution is to read the array into memory once in main() then pass the array to sumArray() function. Just add another parameter to sumArray().
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jun 2007
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
line 29: open() wants a const char* for the filename and you passed a std::string object. I'm supprised your compiler did not object and spew out some nasty error messages. It should be this:in.open(inFile.c_str());//This is the way the book showed us, it works fine. It reads and prints the data. I'm not sure how to use what you have suggested.
line 39: unnecessary -- if open() failed the stream can not be closed.//Our teacher advised us to always close a file.
line 73: input file was never opened so lines 73-82 will probably never get executed.//So how do I go abut doing that. Do I have it all set up wrong?
lines 75-80 should not be inside the while loop beginning at line 73 because total sales will get counted several times. Move that for loop outside and below the while loop.// I did that because I thought that it would read from the text file while there was data to be read.
Actually, in sumArray() you don't need that double array at all because you don't do anything with it. A better solution is to read the array into memory once in main() then pass the array to sumArray() function. Just add another parameter to sumArray(). //Could you please provide an example on how to do this. My book seems vague and I'm really struggling.
here's what the functions would look like
and here is how to call it from main () on line 44. And don't forget to change the function prototype on line 12 too.
BTW: you should name arrays something other than one-letter names.
C++ Syntax (Toggle Plain Text)
double sumArray(double a[LOCATIONS], double& totalSales) { }
and here is how to call it from main () on line 44. And don't forget to change the function prototype on line 12 too.
C++ Syntax (Toggle Plain Text)
sumArray(a, totalSales);
BTW: you should name arrays something other than one-letter names.
Last edited by Ancient Dragon; Aug 8th, 2007 at 9:29 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Using functions and arrays to create a program for grades (C++)
- arrays and functions (C++)
- C - Functions and arrays (C)
- Struct in Functions (C)
- problems w/ program involving structures and arrays (C++)
- Array problem (C)
Other Threads in the C++ Forum
- Previous Thread: class
- Next Thread: LoadLibrary() and getcwd()
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






