// This program reads floating point data from a data file and places those
// values into the private data member called values (a floating point array)
// of the FloatList class. Those values are then printed to the screen.
// The input is done by a member function called GetList. The output
// is done by a member function called PrintList. The amount of data read in
// is stored in the private data member called length. The member function
// GetList is called first so that length can be initialized to zero.
//I cannot figure out how to print or use the getList function. fstream is very //new to me and have been struggling for hours.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int MAX_LENGTH = 50; // MAX_LENGTH contains the maximum length of our list
class FloatList // Declares a class that contains an array of floating
// point numbers
{
public:
void getList(ifstream&); // Member function that gets data from a file
void printList() const; // Member function that prints data from that
// file to the screen.
FloatList(); // constructor that sets length to 0.
~FloatList(); // destructor
private:
int length; // Holds the number of elements in the array
float values[MAX_LENGTH]; // The array of values
};
int main()
{
ifstream tempData; // Defines a data file
// Fill in the code to define an object called list of the class FloatList
FloatList List;
// Fill in the code that calls the getList function.
List.getList(tempData);
// Fill in the code that calls the printList function.
//List.printList();
return 0;
}
FloatList::FloatList()
{
// Fill in the code to complete this constructor that
// sets the private data member length to 0
length = 0;
}
// Fill in the entire code for the getList function
// The getList function reads the data values from a data file
// into the values array of the class FloatList
void FloatList::getList(ifstream& ifs)
{
// Fill in the entire code for the printList function
// The printList function prints to the screen the data in
// the values array of the class FloatList
void FloatList::printList() const
{
for(int i = length; i < MAX_LENGTH - 1; i++)
cout << values[i];
}
// Fill in the code for the implementation of the destructor
FloatList::~FloatList()
{
Please do the following, so we can help you properly:
-Put [code ] code here [/code] tags around your code, so it will have suntax highlighting.
-Please apply some indentation: http://en.wikipedia.org/wiki/Programming_style#Indentation
- Please "uncomment" your actual question and information so it's clearly distinct from the code.
Hello there! The getList() function takes an ifstream reference, which is a file input stream. The code there is correct. It calls getList with the opened file stream so getList can take input from it.
Uncomment the List.printList(); line to print the list. I would suggest that you change the cout << values[i]; line to cout << value[i] << '\n'; otherwise your output would all be on one line.
You should also add tempData.close() just after the List.printList() to close the file after you're done with it.
Hope I could help.
(Please remove blank lines and indent the lines so people can understand your code better. Also put them in code tags.)
The for loop in printList is wrong, for(int i = length; i < MAX_LENGTH - 1; i++) should be for(int i = 0; i < length; i++)
Edit: Also in getList you dont increment length so it stays 0 and you continually overwrite values[0].