Question

In: Computer Science

using C++ 24. Using Files—Total and Average Rainfall Write a program that reads in from a...

using C++

24. 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.

You can use note pad to create a Rainfall.txt with the following data:

January
May
1.35 2.15 3.03 4.41 5.41

Please note that you must read the first line of the file as starting month and second line as ending month. Please do not hard code the starting and ending month into your program.

test case

During the months of January-May the total
rainfall was 16.35 inches and the average monthly
rainfall was 3.27 inches.

Solutions

Expert Solution

Thanks for the question, here is the simple C++ program to read from the file and displays the total and average rainfalls

When you run the program, please update the below line for the file name path in your system

char * filename ="Rainfall.txt";

else it wont be able to read the file and display the output.

=====================================================================

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

using namespace std;

int main(){
  
   char * filename ="F:\\Rainfall.txt";
  
   ifstream infile(filename);
   double total_rainfall=0;
   int months=0;
   string startMonth;
   string endMonth;
   if(infile.is_open()){
      
  
       double rainfall;
       infile>>startMonth>>endMonth;
       while(infile>>rainfall){
           total_rainfall+=rainfall;
           months+=1;
       }
       infile.close();
      
   }else{
       cout<<"Unable to read data from file: "<<filename<<endl;
       exit(EXIT_FAILURE);
   }
  
   cout<<"During the months if "<<startMonth<<"-"
   <<endMonth<<" the total \nrainfall was "
   <<fixed<<showpoint<<setprecision(2)
   <<total_rainfall<<" inches and the average monthly \nrainfall was "
   <<total_rainfall/months<<" inches."<<endl;
}


Related Solutions

-In C Programming- Write a program to display the total rainfall for a year. In addition,...
-In C Programming- Write a program to display the total rainfall for a year. In addition, display the average monthly rainfall, and the months with the lowest and highest rainfall. Create an array to hold the rainfall values. Create a 2nd parallel array (as a constant) to hold the abbreviated names of the months. I created my arrays to be 1 element bigger than needed, and then disregarded element [0] (so that my months went from [1] = "Jan" to...
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
Write a C++ program that lets the user enter the total rainfall for each of 12...
Write a C++ program that lets the user enter the total rainfall for each of 12 months (starting with January) into an array of doubles. The program should calculate and display (in this order): the total rainfall for the year,     the average monthly rainfall,     and the months with the highest and lowest amounts. Months should be expressed as English names for months in the Gregorian calendar, i.e.: January, February, March, April, May, June, July, August, September, October, November,...
Using C++ Write a program that reads a text from the keyboard until the user presses...
Using C++ Write a program that reads a text from the keyboard until the user presses the “Enter” key. Your program must count the number of uppercase alphabets (A through Z), lowercase alphabets (a through z), digits (0 through 9) and any other characters. The other character count value should NOT include the “Enter” key. Display the count on the screen. You must use the in-built C++ character I/O functions in this program.
How do I create this program? Using C++ language! Write a program that reads data from...
How do I create this program? Using C++ language! Write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global varibles are the mean, standard deviation, and the number of data entered. All other varibles must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function....ALL...
Write a C++ program that uses nested loops to collect data and calculate the average rainfall...
Write a C++ program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask the user for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 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, the program should display the number...
In C++, develop a menu program which reads in data from 4 different input files -...
In C++, develop a menu program which reads in data from 4 different input files - appetizers, entrees, desserts, and drinks. Then, record the item information ordered by the customer for each item selected. When finished, display all items ordered to the screen with the subtotal, tax (10% for easy math), and the total. Be sure to get tip as well. Then, ensure enough payment was received before generating an output file receipt which can be printed off. Use functions...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
C++ Write a program that reads in a list of 10 names as input from a...
C++ Write a program that reads in a list of 10 names as input from a user and places them in an array. The program will prompt for a name and return the number of times that name was entered in the list. The program should output total number of instances of that name and then prompt for another name until the word done is typed in. For this lab, use the string data type as opposed to char to...
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT