The programme does not read the data from the file

Hi, my programme is not reading the data from the file as it is supposed to do for some reason. When I compile and run the programme, it does not show any data from the file "RedwoodBaySeismicEventList.Data" at all. So, I was wondering if anyone could help me figure out the solution for the problem. The code for the programme is below:

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;

//Define a class to represent each Seismic Activity measurement event...

class RedwoodBaySeismicData  {
  string LocationID;   //This is the station that owns this piece of data…
  string Date;     //The date that the values were collected…
  string Time;    //The time of day that the values were collected…
  double SeismicSize;  //Magnitude of Earthquake
  

public:
  RedwoodBaySeismicData();
  RedwoodBaySeismicData(string, string, string, double);

  //The first group of member functions are used to change values...
  //Functions are defined in the form of inlines.

  void SetID(string ID)     { LocationID = ID; }
  void SetDate(string Day)   { Date = Day; }
  void SetTime(string Hour)   { Time = Hour; }
  void SetSeismicSize(double Magnitude) {SeismicSize = Magnitude; }
  

  //The second group allows controlled access to individual value in the class...

  string GetID()    { return LocationID; }
  string GetDate()   {return Date; }
  string GetTime()   {return Time; }
  double GetSeismicSize() { return SeismicSize;}  
  
 

};



//The default constructor sets the private variable members to a harmless value...

RedwoodBaySeismicData::RedwoodBaySeismicData()
{
 LocationID = "";
 Date = "";
 Time = "";
 SeismicSize = 0.0;
 
}

//This initializing  constructor allows you to create a object with value already set...

 RedwoodBaySeismicData::RedwoodBaySeismicData(string ID, string Day, string Hour, double Magnitude)
{
 LocationID = ID;
 Date = Day;
 Time = Hour;
 SeismicSize = Magnitude;
 
}
 
 class RedwoodBayDataList  {
 vector<RedwoodBaySeismicData> RedwoodBayDataSpots;  //Create a collection of location information...
    string FileName ;
public:
 RedwoodBayDataList()  {FileName = "RedwoodBaySeismicEventList.data";}

    
    void PostSeismicInfo();
    void WeeklyReport();
};
 
 
 

//Allow the user to post the values for each location...

void RedwoodBayDataList::PostSeismicInfo()
{
 int K;
 string Line, ID, Date, Hour;
 double Magnitude;
 fstream InFile("RedwoodBaySeismicEventList.data", ios::in);
 //...then get the values for each location...
 RedwoodBaySeismicData Buffer;
 
 cout << "\n\t=============================================================" << endl;  //This section is the report...it's really all about the formatting...
    cout << "\tWeekly Report" << endl;
    cout << "\t===============================================================" << endl;
    cout << "\tLocation\tDate\tHour\tMagnitude" << endl;
    cout << "\t===============================================================" << endl;
 

 for(size_t K = 0; K < RedwoodBayDataSpots.size(); ++K)  {
          
  getline(InFile, Line);
  ID = Line.substr(0,4);
  Date =  Line.substr(6,7);
  Hour = Line.substr(9,3);
  istringstream buf(Line.substr(5,2));
        buf>> Magnitude;
   
  RedwoodBayDataSpots[K].SetID(ID);
  RedwoodBayDataSpots[K].SetDate(Date);
  RedwoodBayDataSpots[K].SetTime(Hour);
  RedwoodBayDataSpots[K].SetSeismicSize(Magnitude);  
  cout << "\t" << RedwoodBayDataSpots[K].GetID() << "\t" << RedwoodBayDataSpots[K].GetDate() << "\t" << RedwoodBayDataSpots[K].GetTime() << "\t"  << RedwoodBayDataSpots[K].GetSeismicSize();
}
  cout << "\t===============================================================\n\n" << endl;

 
}
 
 


//Declare the functions...

void RunIT();
void Menu();
 

int main()
{
 RunIT();
}

//This function implements a simple command processor...

void RunIT()
{
 RedwoodBayDataList RedwoodBayDataSpots;
 string Command = "";

 while(Command != "Quit")  {
  Menu();
  cout << "Command: ";
        getline(cin, Command);

  
     if(Command == "Report")
   RedwoodBayDataSpots.PostSeismicInfo();
 }
}

//This function tells the user what he or she can do...

void Menu()
{
 cout << "Choices...................................................." << endl;
 cout << "-----------------------------------------------------------" << endl;
 cout << "\tReport...displays the  report." << endl;
 cout << "\tQuit.....exits the program." << endl;
 cout << "-----------------------------------------------------------" << endl;
}


I would really appreciate any suggestions.
Line 97:
You go into a loop for each item in the vector. Unfortunately, the items only get added into the vector within the loop, meaning it never gets excecuted because there isn't anything in the vector initially...
This should loop while there is items in the file instead.

Line 106:
If you fixed line 97, you'll encounter an bug here because the vector is empty.
You'll need to change this to load the data into the Buffer variable you created earlier, then add this into the vector before outputting from the vector. (AKA change lines 106, 107, 108 and 109, but line 110 is fine)

Afterward you'll want to fix up your substr functions, the second paramater is the length, not the ending index.
Hi The Palm Tree Magician. Thanks for your response. I was wondering if you could tell me how come "there is not anything in the vector initially". I thought because I am reading the data from the file and putting it in the vector the vector would store those datas.
I think I have figured out the problem with the earlier code. So, now I am reading the data from the file using the while loop. Also, as per the advice of "The Palm Tree Magician", I am loading the data into the Buffer before outputting from the vector. However, the programme still does not run. Whenever I run the programme, the dialog box appears and it says "un.exe" is not working . I am using Dev C++ by the way.
Also, here is my new code for the programme:

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

#include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    #include <sstream>
    using namespace std;
    
    //Define a class to represent each Seismic Activity measurement event...
    
        class RedwoodBaySeismicData  
        {
          string LocationID;   //This is the station that owns this piece of data…
          string Date;     //The date that the values were collected…
          string Time;    //The time of day that the values were collected…
          double SeismicSize;  //Magnitude of Earthquake
          
        
        public:
          RedwoodBaySeismicData();
          RedwoodBaySeismicData(string, string, string, double);
        
          //The first group of member functions are used to change values...
          //Functions are defined in the form of inlines.
        
          void SetID(string ID)     { LocationID = ID; }
          void SetDate(string Day)   { Date = Day; }
          void SetTime(string Hour)   { Time = Hour; }
          void SetSeismicSize(double Magnitude) {SeismicSize = Magnitude; }
          
        
          //The second group allows controlled access to individual value in the class...
        
          string GetID()    { return LocationID; }
          string GetDate()   {return Date; }
          string GetTime()   {return Time; }
          double GetSeismicSize() { return SeismicSize;}  
          
         
        
        };
        
        
        
        //The default constructor sets the private variable members to a harmless value...
        
        RedwoodBaySeismicData::RedwoodBaySeismicData()
        {
         LocationID;
         Date;
         Time;
         SeismicSize = 0.0;
         
        }
        
        //This initializing  constructor allows you to create a object with value already set...
        
         RedwoodBaySeismicData::RedwoodBaySeismicData(string ID, string Day, string Hour, double Magnitude)
        {
         LocationID = ID;
         Date = Day;
         Time = Hour;
         SeismicSize = Magnitude;
         
        }
     
class RedwoodBayDataList 
        {
             vector<RedwoodBaySeismicData> RedwoodBayDataSpots;  //Create a collection of location information...
             string FileName ;
        public:
             RedwoodBayDataList()  {FileName = "RedwoodBaySeismicEventList.data";}
            
                
             void PostSeismicInfo();
            
        };
         
     
     
        //Allow the user to post the values for each location...
    
            void RedwoodBayDataList::PostSeismicInfo()
        {
             int K;
             string Line, ID, Date, Hour;
             double Magnitude;
             fstream InFile("RedwoodBaySeismicEventList.data", ios::in);
             //...then get the values for each location...
             RedwoodBaySeismicData Buffer;
     
             cout << "\n\t=============================================================" << endl;  //This section is the report...it's really all about the formatting...
             cout << "\tWeekly Report" << endl;
             cout << "\t===============================================================" << endl;
             cout << "\tLocation\tDate\tHour\tMagnitude" << endl;
             cout << "\t===============================================================" << endl;
     
             
                      
              while (!InFile.eof())            
                  
         { 
              getline(InFile, Line);
              ID = Line.substr(0,4);
              Date =  Line.substr(6,7);
              Hour = Line.substr(9,3);
              istringstream buf(Line.substr(5,2));
              buf>> Magnitude;
            
              Buffer.SetID(ID);    //Use these values to load the Buffer record...
              Buffer.SetDate(Date); 
              Buffer.SetTime(Hour);
              Buffer.SetSeismicSize(Magnitude);
              RedwoodBayDataSpots.push_back(Buffer);
      
              for(K = 0 ; K < RedwoodBayDataSpots.size(); ++K)  
              {
              RedwoodBayDataSpots[K].SetID(ID);
              RedwoodBayDataSpots[K].SetDate(Date);
              RedwoodBayDataSpots[K].SetTime(Hour);
              RedwoodBayDataSpots[K].SetSeismicSize(Magnitude);  
cout << "\t" << RedwoodBayDataSpots[K].GetID() << "\t" << RedwoodBayDataSpots[K].GetDate() << "\t" << RedwoodBayDataSpots[K].GetTime() << "\t"  << RedwoodBayDataSpots[K].GetSeismicSize();
              }
              cout << "\t===============================================================\n\n" << endl;
              }
              InFile.close();
    
    
          }
        
    
    //Declare the functions...
    
          void RunIT();
          void Menu();
         
       
          int main()
          {
          RunIT();
          }
    
    //This function implements a simple command processor...
    
          void RunIT()
          {
          RedwoodBayDataList RedwoodBayDataSpots;
          string Command;
    
          while(Command != "Quit")  {
          Menu();
          cout << "Command: ";
          getline(cin, Command);
     
           if(Command == "Report")
           RedwoodBayDataSpots.PostSeismicInfo();
           }
           }
    
    //This function tells the user what he or she can do...
    
          void Menu()
          {
          cout << "Choices...................................................." << endl;
          cout << "-----------------------------------------------------------" << endl;
          cout << "\tReport...displays the  report." << endl;
          cout << "\tQuit.....exits the program." << endl;
          cout << "-----------------------------------------------------------" << endl;
          }


I would greatly appreciate any suggestions
i tried ur code, its working fine, try to call ur exe from the cmd prompt by setting the path to directory where ur exe is available.
Topic archived. No new replies allowed.