CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2010
    Posts
    13

    Reading in only first couple lines of input 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 only want to read in data for the first day (first 24 lines), but I cannot figure out how to.
    -------------------------------------------------------------------------------------------------------
    #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 Data1, Data2, Data3, Total(0);
    int k;

    // 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(k=1; k<=24; k++) //Counter to read first 24 lines
    {
    while(infile>>Data1>>Data2>>Data3) // Reads in 3 data items
    {
    if(Data1 > 1) // Ignores Data1 if it is 1 or 0
    Total=Total+Data1; // Adds up Data1 for first 24 lines
    }
    }
    cout<<Total<<endl;
    }
    system("pause"); //Pauses console window
    return (0); // Ends program
    }
    Last edited by chara76; April 11th, 2010 at 12:17 PM.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Reading in only first couple lines of input file

    This:
    Code:
    while (infile >> Data1 >> Data2 >> Data3) // Reads in 3 data items
    {
        if (Data1 > 1) // Ignores Data1 if it is 1 or 0
            Total = Total + Data1; // Adds up Data1 for first 24 lines
    }
    should be:
    Code:
    if (infile >> Data1 >> Data2 >> Data3) // Reads in 3 data items
    {
        if (Data1 > 1) // Ignores Data1 if it is 1 or 0
            Total += Data1; // Adds up Data1 for first 24 lines
    }
    else
    {
        // report missing data?
        break;
    }
    Notice that I posted in [code][/code] bbcode tags and indented the example code. Next time, please do the same.

    EDIT:
    Also, try and declare variables near first use. It is bad practice to declare Data1, Data2, Data3, Total and k at the start the main function when you could have declared them before the loop. In fact, you can even declare k in the for loop, i.e.,
    Code:
    for (int k = 0; k < 24; ++k)
    Likewise, you could have declared Data1, Data2 and Data3 in the body of the for loop, though declaring them just before the loop is acceptable.
    Last edited by laserlight; April 11th, 2010 at 12:28 PM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 2010
    Posts
    13

    Re: Reading in only first couple lines of input file

    Thanks, that worked great. I'll make sure to post the right way next time.

  4. #4
    Join Date
    Apr 2010
    Posts
    13

    Re: Reading in only first couple lines of input file

    How would I then read the data for the next day (lines 25-48)? I tried the following:

    Code:
    for(int k=1; k<=24; k++)
                       {
                       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
                       }

  5. #5
    Join Date
    Mar 2010
    Posts
    11

    Re: Reading in only first couple lines of input file

    Use another loop, something like
    Code:
    for(int k=25; k<=48; k++)
    Also, you have to add to 'total' variable, not overwrite its data.
    Code:
    Total2 += Data1;

  6. #6
    Join Date
    Apr 2010
    Posts
    13

    Re: Reading in only first couple lines of input file

    Where would I add the new loop? I tried Adding it after the first for loop, but that did not work.

  7. #7
    Join Date
    Mar 2010
    Posts
    11

    Re: Reading in only first couple lines of input file

    Basically, the same loop as first one, but change the variable 'Total' to 'Total2'.

    Consider using arrays instead of variables like 'Total*' in case you wanna read next 3 days, or even more.

  8. #8
    Join Date
    Apr 2010
    Posts
    13

    Re: Reading in only first couple lines of input file

    I'm still new to c++. Could you give an example of how to use an array?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured