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;
}

Dani AI

Generated

A few concrete fixes and a short, safe approach to meet the assignment requirements and the example output shown by . The current code has three common problems: days and count are uninitialized (undefined behavior), the loop uses inFile.peek() != EOF which is brittle, and the program prints lines instead of converting them to numbers and accumulating a sum. is right to say the text lines must be converted to numbers; prefer C++ parsing (stringstream or std::stod) over C atof for clearer error handling.

Recommended algorithm:

  • Initialize double sum = 0.0; int count = 0;.
  • Open the file and check success.
  • Read with std::getline in a loop. Skip empty/whitespace-only lines.
  • Parse each line to a double (use std::stringstream to test success).
  • If parse succeeds, sum += value; ++count;.
  • After the loop compute average = (count ? sum / count : 0.0) and print using std::fixed << std::setprecision(4) so the output matches YY.YYYY.

Example (concise, robust) implementation:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>

int main() {
    std::ifstream in("pa7-temp.txt");
    if (!in) { std::cerr << "File could not be opened\n"; return 1; }

    double sum = 0.0;
    int count = 0;
    std::string line;
    while (std::getline(in, line)) {
        std::stringstream ss(line);
        double t;
        if (ss >> t) { sum += t; ++count; }
    }

    double avg = (count ? sum / count : 0.0);
    std::cout << "There were " << count << " days of temperature data and the average temperature was "
              << std::fixed << std::setprecision(4) << avg << "." << std::endl;
}

Troubleshooting notes: avoid integer division (use double for the average), handle empty files explicitly, and be prepared for non-numeric lines (the code above skips them). std::stod can be used too, but it throws on bad input; stringstream provides silent, safe checks for simple assignments.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.