Start New Discussion within our Software Development Community

Okay so i am really new to coding(about 2 weeks) and need help with an assignment.the asignment goes like this

"Write a program that will read in a file named pa7-temp.txt (located in the current directory) that contains a set of temperatures, and then prints out how many were in the file and the average temperature. This program must read the input data in one line at a time as strings.
The output should look like this (with XX and YY.YYYY being replaced with correct numbers):
There were XX days of temperature data and the average temperature was YY.YYYY."

Yes i know i have unused variables but i'm pretty sure i need them i just not sure how to implement them how i need.

This is what i have so far(and it's not much)

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;


int main()
{
    int days;
    string line;
    int count;

    ifstream inFile("pa7-temp.txt", ios::in);
    if (!inFile)
    {
        cerr << "File could not be opened" << endl;
        exit(1);
    }

    while (inFile.peek() != EOF)
    {
        getline(inFile, line);
        cout << line;
    }

    cout << "There were" << days <<" days of temperature data & the average" 
    << " temperature was" << days/count << "." << endl;
}

You need a identifier such that it denotes the end of a temperature data. For simplicity you can use newline ( '\n'). Your txt file will look like :

23
25
30
29

Now you input them as string. Change them to float, add them together and thn divide by total number of data present in file. You may change getline( inFile, line ); to getline(inFile, line, '\n'). '\n' is delimiter. You may use atof()or strtof() for changing to float.

This article has been dead for over six months. Start a new discussion instead.