This is simple enough but I'm confused on how to change the getList() member function. Currently it's written assuming that only numbers are in the input text file. You'll need to re-write it so that it works with the format, That is, a number followed by a comma char, followed by a string (can I use the getline() function to get the remainder of the line and put it into a string description member variable -- your array called days[] )?
#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
usingnamespace std;
constint 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
float computeAvg(); // Compute Average and return it.
int findHighest(); //find highest and return it.
int findLowest(); //find highest and return it.
void printReport(); //print report to the console.
private:
int length; // Holds the number of elements in the array
float values[MAX_LENGTH]; // The array of values
string days[MAX_LENGTH]; // array of days.
};
int main()
{
ifstream tempData; // Defines a data file
// Fill in the code to define an object called list of the class FloatList
FloatList FL;
cout << fixed << showpoint;
cout << setprecision(2);
tempData.open("temperatures.txt");
// Fill in the code that calls the getList function.
FL.getList(tempData);
// Fill in the code that calls the printReport function.
FL.printReport();
tempData.close();
system("pause"); // pause window until some key pressed.
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& infile){
float value;
char ch;
while(!infile.eof()){
infile >> value;
infile >> ch;
getline(infile, days[length]);
values[length++] = value;
}
}
// 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=0; i<length; i++)
cout <<values[i] << endl;
}
// Fill in the code for the implementation of the destructor
FloatList::~FloatList(){
cout <<"Goodbye ...the FloatList object is being destroyed."<<endl;
}
float FloatList::computeAvg(){
float sum = 0;
for(int i=0; i<length; i++)
sum += values[i];
return sum/length;
}
int FloatList::findHighest(){
int max_index = 0;
for(int i=0; i<length; i++){
if(values[i] > values[max_index])
max_index = i;
}
return max_index;
}
int FloatList::findLowest(){
int min_index = 0;
for(int i=0; i<length; i++){
if(values[i] < values[min_index])
min_index = i;
}
return min_index;
}
void FloatList::printReport(){
cout <<"The average of the "<<length <<" temperatures is "<< computeAvg()<<" Fahrenheit."
<<endl;
int index = findLowest();
cout <<"The lowest temperature is "<< values[index]<<" Fahrenheit on"<<days[index] <<endl;
index = findHighest();
cout <<"The highest temperature is "<<values[index] <<" Fahrenheit on"<<days[index] <<endl;
}
54, Saturday December 6
54, Sunday December 7
44, Monday December 8
56, Tuesday December 9
50, Wednesday December 10
51, Thursday December 11
55, Friday December 12
59, Saturday December 13
59, Sunday December 14
60, Monday December 15
In member function 'void FloatList::getList(std::ifstream&)':
49 [Error] 'tempData' was not declared in this scope
50 [Error] 'Day_of_week' was not declared in this scope
51 [Error] 'Month' was not declared in this scope
52 [Error] 'Day_num' was not declared in this scope
At global scope:
65 [Error] redefinition of 'void FloatList::getList(std::ifstream&)'
45 [Error] 'void FloatList::getList(std::ifstream&)' previously defined here
I've added several lines of code to the getList() method, but it still looks broken. Should I discard my current getList() and replace it with this one and start over?
#include <iostream>
#include <fstream>
#include <iomanip>
usingnamespace std;
constint 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.
float computeAvg() const; // Member function to compute the average of
// the list of floats.
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
};
// INFILE is the name of the text file used for input.
#define INFILE "temperatures.txt"
int main()
{
ifstream tempData; // Defines a data file
// Fill in the code to define an object called list of the class FloatList
FloatList list;
cout << fixed << showpoint;
cout << setprecision(2);
tempData.open(INFILE);
if (!tempData)
{
cerr << "Sorry, but the data file ["
<< INFILE
<< "] is missing."
<< endl;
return -1;
}
// Fill in the code that calls the getList function.
list.getList( tempData );
// Fill in the code that calls the printList function.
list.printList();
// Print the average of the FloatList with a label
cout << fixed << showpoint << setprecision(2);
cout << "The average temperature is " << list.computeAvg() << endl;
// close the open data file
tempData.close();
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& infile)
{
while ( true )
{
infile >> values[length];
if ( !infile ) // if we've reached the "end of file", break
break;
// if've we make it here, increment the size (ie, the length)
length++;
// check to make sure that the array is not already fully populated
if (length == MAX_LENGTH)
{
cerr << "Sorry, but the array is already fully populated!"
<< endl
<< "No more additions are possible."
<< endl;
break;
}
}
}
// Member function to compute the average of
// the list of floats.
float FloatList::computeAvg() const
{
float sum = 0;
for (int k = 0; k < length; k++)
sum += values[k];
if (length > 0)
return sum / length;
return 0;
}
// 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
{
cout << "FloatList: [";
int k;
for (k = 0; k < length-1; k++)
cout << values[k] << ", ";
cout << values[k] << "]" << endl;
}
// Fill in the code for the implementation of the destructor
FloatList::~FloatList()
{
cout << "Goodbye ...the FloatList object is being destroyed." << endl;
}
and start over? if not I probably need to create the string array days[] to display the results. This is what a friend of mine had said to do:
The above is a number (int or floating-point), followed by a comma delimiter followed by a LINE of text. Therefore, to input this line of data into your program, do this,
infile >> (into an int or a double for the temperature value)
infile >> comma character (this just "consumes" the char; we don't care about it since it's not data)
get the remainder of the line into a string description of the date