I need help editing my code.

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[] )?

Final CODE finished with corrections
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
 #include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
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
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;
}


Last edited on
Nobody?
hi
you could have the text file laid out as
EX:
:something1 :something2 :something3 :something4

using getline(tempData,value,':');
this would read the file and store the data accordingly
Last edited on
also, what does the data in the .txt file look like? I have to see it so I can change the code in the fashion of how the file's data is arranged.
Last edited on
here is the information in the text file:

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
so, you want all the data or just the temp or the date too?
also If you want it all I got the code here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void FloatList::getList(ifstream& infile)
{
   float value;
   string Day_of_week,Month,dummy;
   double Day_num;
   while(!infile.eof()){
    getline(infile,dummy,' ');
    infile>>value;
    getline(infile,Day_of_week,' ');
    getline(infile,Month,' ');
    infile>>Day_num;
   values[length++] = value;
   }
}

though, I'm not sure that it will work 100% correctly...
Last edited on
jasonwynn10 that's so cool. I tried something similar to that earlier and it did work but this works fine. thank you so much for helping! :D
I'm not sure what I'm doing wrong now. I changed the code:
1
2
3
4
5
6
7
void FloatList::getList(ifstream& infile){
   float value;
   while(!infile.eof()){
    infile >> value;
   values[length++] = value;
   }
}


to this:

1
2
3
4
5
6
7
8
9
10
11
void FloatList::getList(ifstream& infile)
{
   float value;
   while(!infile.eof()){
    getline(tempData,value,',');
    getline(tempData,Day_of_week,' ');
    getline(tempData,Month,' ');
    getline(tempData,Day_num,'\n');
   values[length++] = value;
   }
}


and I'm getting errors

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
reread the code I posted, I've updated it.
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#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.
   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

ugh help??? please
Last edited on
Sorry I couldn't help more, I was busy with my work. I see that you've fixed it and did a good job at that! Good luck!
Hey that's fine I appreciate the help. That's why asking questions is such a beneficial thing to do!
Topic archived. No new replies allowed.