1

I made a program to calculate the average monthly rain fall and the total rain fall from data using a .txt file. When the program calculates the date the average rainfall output always show a number with two decimal places like 3.27 but I want this output: 3.27000 or the output will be 2.87 instead of 2.87429 I tried set precision, didn't work. How can I a number with 5 decimal places?

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



int main()
 {
    ifstream inputFile;
    int numberOfMonths = 0;
    double rainFallPerMonth;
    double totalRainFall = 0.0;

    double averageMonthlyRainFall;
    
    string startingMonth;
    string endingMonth;

    
    inputFile.open("Rainfall.txt");

    inputFile >> startingMonth;
    inputFile >> endingMonth;
    
        
         while (inputFile >> rainFallPerMonth)
        { totalRainFall += rainFallPerMonth;
            numberOfMonths ++;
         }
         

         averageMonthlyRainFall = totalRainFall / numberOfMonths;

         inputFile.close();

         cout << "During the months of " << startingMonth << "-" << endingMonth << " the total";
         cout << "\nrainfall was " << totalRainFall << " inches and the average monthly";
         cout << fixed << showpoint << setprecision(5);
         cout << "\nrainfall was " << averageMonthlyRainFall  << " inches.";

         return 0;
    }
Daniel
  • 47
  • 4

0 Answers0