Reading in lines in the middle of data file

I'm trying to create a program that reads in data for 3 separate days and analyzes it. The data file has 72 lines (24 for each day). I figured out how to read the data for the first day (lines 1-24), but I cannot figure out how to read the data for days 2 and 3 (lines 25-48 and lines 49-72), separately. I have the following:

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
#include <iostream>  // Needed for normal cin & cout statements
#include <fstream>   // Needed to read or write files on disk
#include <cstdlib>   // Needed for exit
#include <cmath>     // Needed for certain math functions
#include <string>    // Needed to read strings
using namespace std;
int main()
{
    // Initializes variables
    double sunlight, power_output, temp, Total(0), Total2(0);
    
    // Opens data file
    ifstream infile;
    infile.open("data.txt");
    
    if(!infile)
       {
        cout<<"Unable to open file: data.txt\n";  // Displays error message if data file cannot be opened
        system("pause");  // Pauses command window
        exit (0);  // Exits program
       }
    else
        {
        for(int k=1; k<=24; k++)  //Counter to read first 24 lines
                   {
                   if(infile>>Data1>>Data2>>Data3)  // Reads in 3 data items
                        {                                             
                         if(sunlight > 1)  // Ignores Data 1 if it is 1 or 0
                             Total+=Data1; // Adds up Data1 for first 24 lines                                 
                        }
    
                   while(k>24 && k<=48)  // Analyzes data for lines 25 to 48
                         Total2+=Data1;  // Adds up Data1 for lines 25 to 48
                   }
        }                                     
    system("pause");  //Pauses console window
    return (0);  // Ends program
}
You need to read and discard the first 24 lines if you want to start directly from the 25th
How do i do that? Do I get rid of k and start with a new variable?
It looks like your file is organized as:
1
2
3
4
5
1 2 3
4 5 6
7 8 9
10 11 12
...
24
25
70 71 72
...


You should have a function that handles each day's worth of data in the file with a single function:
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
struct OneDaysData_t
    {
    double sunlight, power_output, temperature;
    double total_1, total_2;  // what are these?
    };

istream& operator >> ( istream& ins, OneDaysData_t& data )
    {
    for (int k=1; k <= 24; k++)
        {
        infile >> data.sunlight
               >> data.power_output
               >> data.temperature;
        // more stuff here...
        }
    return ins;
    };

int main()
    {
    OneDaysData_t monday, tuesday, wednesday, friday;

    ifstream weather_data_file( "data.txt" );
    ...

    weather_data_file >> monday;
    weather_data_file >> tuesday;
    weather_data_file >> wednesday;
    weather_data_file >> friday;  // (well, this is thursday, we'll read friday next:)
    weather_data_file >> friday;

    ...

    // http://www.cplusplus.com/forum/articles/11153/
    cout << "Press ENTER to quit." << flush;
    cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
    return 0;
    }

The code that you posted above should not compile, because you never declare Data1, Data2, and Data3. What are they?

What are Total and Total2?

You need to be specific when you name things. This tells you a lot about what your program should be doing, and what kind of data it is handling.

  » "infile" is not a useful name because it tells you nothing about the file you are using. Only use
    generic names when handling stuff that could be anything.
  » "sunlight" is a very good name because it tells you exactly what it is: whether sunlight was measured.

Likewise, you assume a lot of knowledge about your data in your commentary on lines 29, 23, and 33. How do you know that stuff? (The data your program reads from the file could change!)

Likewise, your commentary is often not very helpful. For example, on line 28 (besides using an uninitialized variable -- sunlight could be -32.7 for all you know -- since you never set its value!), on line 28 you say "Ignores Data 1 if it is 1 or 0". That's nonsense. What you should say is something like "No sunlight means that there is no power output." [Are you working with solar cells or something?]

Hope this helps.
Topic archived. No new replies allowed.