Input File Assignment Confusion

Feb 22, 2014 at 7:50am
Hi guys,

We're going over input files in my C++ class but I'm taking it online with limited direct help. We currently have an assignment where we are required to write a program that reads daily temperature data from an input file and analyzes that data to display the average high, average low and average of the averages. I thought I knew how to get started but even now I'm in a slump because I can't even get the data to appear when I run the program. Here's the assignment:

http://tinypic.com/r/2ij3rec/8

http://tinypic.com/r/ouxkx0/8

This is what I have so far. I'm not asking for anyone to do the assignment for me, as I'll also be tested on this later on, but some guidance would be very much appreciated. At least if someone could direct me along the right way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{

    ifstream file_1;

    file_1.open("mayTemperatureData.txt"); 

    system("PAUSE");
    return EXIT_SUCCESS;
}
Last edited on Feb 22, 2014 at 7:51am
Feb 22, 2014 at 7:52am
I also have the temperature data saved as mayTemperatureData in .txt format in the same folder as the cpp program
Feb 22, 2014 at 8:20am
You might want to try reading something from the file.
Feb 22, 2014 at 8:27am
That's also something I was unsure of how to do. From the example our professor gave us, his text document had three rows with three different numbers. The first two were integers and were declared as such whereas the last one was float. He labeled them i, j, and x so that left me to think that do I need to declare each row of my own text?
Feb 22, 2014 at 8:58am
This what I have thus far:

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

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
    char filename[50];
    ifstream file_1;
    cin.getline(filename, 50);
    file_1.open(filename);

    if(!file_1.is_open()){
        cout << "Could not find document." << endl;
        exit(EXIT_FAILURE);
    }

    char word[50];
    file_1 >> word;
    while (file_1.good()){
        cout << word << " ";
        file_1 >> word;
    }

    system("PAUSE");
    return EXIT_SUCCESS;
}


If I create a variable for the avg. min, max, and avgs. and set up a cout display, will the original .txt be displayed as well?
Last edited on Feb 22, 2014 at 8:58am
Feb 22, 2014 at 12:55pm
your cin.getline() function will return the whole line from your text document, but you open your document after you try and get the first line?

Also you could do if(!file_1.eof()) meaning if file_1 is not at the end of file do something until it reaches end of file.
Topic archived. No new replies allowed.