Question

[C++]

Using Files—Total and Average Rainfall
Write a program that reads in from a file a starting month name, an ending month name, and then the monthly rainfall for each month during that period. As it does this, it should
sum the rainfall amounts and then report the total rainfall and average rainfall for the period. For example, the output might look like this: During the months of March–June the total rainfall was 7.32 inches and the average monthly rainfall was 1.83 inches. Data for the program can be found in the Rainfall.txt file.
Hint: After reading in the month names, you will need to read in rain amounts until the EOF is reached, and count how many pieces of rain data you read in.

<<ISSUE: My "average monthly rainfall" value returns 5 decimal numbers but it has to be two decimals as ideal output below. Please help to solve this issue.>>

(ideal output)

THE CORRECT OUTPUT OF THE TEST CASE 1 During the months of June - December the total 2 rainfall was 20.12 inches and the aver(current output)

YOUR CODES OUTPUT 1 During the months of June - December the total 2 rainfall was 20.12 inches and the average monthly 3 rai

(Rainfall.txt file)

January
May
1.35 2.15 3.03 4.41 5.41

(Current code)

#include
#include
#include
using namespace std;

int main()
{
   ifstream inputFile;
   string sname, ename;
   double val, total=0.0, avg, c=0.0;
   inputFile.open("Rainfall.txt");
  
   inputFile >> sname;
   inputFile >> ename;
  
   if(!inputFile)
   {
   cout << "Rainfall.txt file does not exist in folder" << endl;
   cout << "Please insert Rainfall.txt file in the folder" << endl;
   }
   while(inputFile >> val)
   {
   total=total+val;
   c++;
   }
   avg = total/c;
   inputFile.close();

  
   cout << "During the months of " << sname << "-" << ename << " the total" << endl;
   cout << "rainfall was " << total << " inches and the average monthly" << endl;
   cout << "rainfall was " << avg << " inches." << endl;
  
   return 0;
}

1 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include <fstream>

0 0
Add a comment Improve this question Transcribed image text
Answer #1

This can be achieved by using 'setprecision()'. This is found inside the '#include <iomanip>' and is used to control the decimal place. The basic syntax of this is: cout << fixed << setprecision(2) << ans;

This would print the ans with 2 decimal place. 'Fixed' is used to round the value. For eg. the number 6.58571 would become 6.59.

Note: I added some more data since using the data provided the average was already in 2 decimal place.

CODE:

#include <iostream>

#include <fstream>

#include <iomanip>

using namespace std;

int main()

{

ifstream inputFile;

string sname, ename;

double val, total=0.0, avg, c=0.0;

inputFile.open("Rainfall.txt");

inputFile >> sname;

inputFile >> ename;

if(!inputFile)

{

cout << "Rainfall.txt file does not exist in folder" << endl;

cout << "Please insert Rainfall.txt file in the folder" << endl;

}

while(inputFile >> val)

{

total=total+val;

c++;

}

avg = total/c;

inputFile.close();

cout << "During the months of " << sname << "-" << ename << " the total" << endl;

cout << "rainfall was " << total << " inches and the average monthly" << endl;

cout << "rainfall was " << fixed << setprecision(2) << avg << " inches." << endl;

return 0;

}

Code Picture:

1 2 3 #include <iostream> #include <fstream> #include <iomanip> using namespace std; int main() 4. 5 6 7 { 8 ifstream inputFi27 28 29 30 31 32 cout << During the months of << sname << - << ename << the total << endl; cout << rainfall was <<

OUTPUT:

During the months of January-May the total rainfall was 46.1 inches and the average monthly rainfall was 6.59 inches.

Hope you found this helpful!

Add a comment
Know the answer?
Add Answer to:
[C++] Using Files—Total and Average Rainfall Write a program that reads in from a file a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use the file processing example program shown at the bottom. Modify the program to enter the...

    Use the file processing example program shown at the bottom. Modify the program to enter the grades, and count number of grades. Also get total and average grade. Use the following data for grades: 79, 99, 85, 97, 88, 95, 100, 87, 94 EXAMPLE OUTPUT Total: 9 grades Total grade sum: 824 Average grade: 92 Letter grade: A / Using Break, and Continue #include "stdafx.h" #include using namespace std; int main() { int i = 0; for (int x =...

  • Write a program that scores the total rainfall for each of 12 months into an array...

    Write a program that scores the total rainfall for each of 12 months into an array double. A sample array definition is shown below double thisYear[] = {1.6, 2.1, 1.7, 3.5, 2.6, 3.7, 3.9, 2.6, 2.9, 4.3, 2.4, 3.7}; The program should have the four functions to calculate the following 1. The total rainfall for the year 2. The average monthly rainfall 3. The month with most rain 4. The month with least rain Output: What to deliver? Your .cpp...

  • Your next programming assignment at the Research Center is to write a program that reads data...

    Your next programming assignment at the Research Center is to write a program that reads data from a file and produces a report of rainfall for that period of time. The program should read the starting month name from the data file, then read an ending month name, followed by reading in monthly rainfall amounts for each of the months from the starting month through the ending month. As it reads the monthly totals, it should sum (accumulate) the rainfall...

  • The following is a program from Starting out with C++ by Toni Gaddis. I am getting the following error messages pertain...

    The following is a program from Starting out with C++ by Toni Gaddis. I am getting the following error messages pertaining to the lines: cin>>month[i].lowTemp; and months[i].avgTemp = (months[i].highTemp + month[i].lowTemp)/2; The error messages read as follows: error C2039: 'lowTemp': is not a member of 'std::basic_string<_Elem,_Traits,_Ax>' and it continues. The second error message is identical. The program is as follows: Ch. 11, Assignment #3. pg. 646. Program #4. //Weather Statistics. //Write a program that uses a structure to store the...

  • My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all th...

    My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all the upper, lower case and digits in a .txt file. The requirement is to use classes to implement it. -----------------------------------------------------------------HEADER FILE - Text.h--------------------------------------------------------------------------------------------- /* Header file contains only the class declarations and method prototypes. Comple class definitions will be in the class file.*/ #ifndef TEXT_H #define...

  • Write a Python program that allows the user to enter the total rainfall for each of...

    Write a Python program that allows the user to enter the total rainfall for each of 12 months into a list. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest rainfall amounts. Data: January: 8.9 inches February: 11.1 inches March: 13.4 inches April: 6.9 inches May: 8.7 inches June: 9.1 inches July: 15.9 inches August: 4.4 inches September: 3.1 inches October: 5.6 inches November: 7.8...

  • Write a program that uses nested loops to collect data and calculate the average rainfall over...

    Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should prompt the user for the number of years. Be sure to ask for each months average rainfall for each year. The outer loop will iterate once for each year while the inner loop will iterate 12 times(one time per month) prompting for the months average rainfall on each iteration. Display the number of months, the total inches...

  • C++ Redo PROG8, a previous program using functions and/or arrays. Complete as many levels as you...

    C++ Redo PROG8, a previous program using functions and/or arrays. Complete as many levels as you can. Level 1: (20 points) Write FUNCTIONS for each of the following: a) Validate #students, #scores. b) Compute letter grade based on average. c) Display student letter grade. d) Display course average. Level 2: (15 points) Use ARRAYS for each of the following. e) Read a student's scores into array. f) Calculate the student's average based on scores in array. g) Display the student's...

  • Write them in python IDLE ***** 5. Average Rainfall Write a program that uses nested loops...

    Write them in python IDLE ***** 5. Average Rainfall Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate twelve times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations,...

  • Write a program in C++ that lets the user enter the total rainfall for each of...

    Write a program in C++ that lets the user enter the total rainfall for each of 12 months into an array of doubles. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts. Input validation: Do not accept negative numbers for monthly rainfall figures.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT