C++ Write a program using structures to store the following weather information:
ID: 3814108 • Letter: C
Question
C++
Write a program using structures to store the following weather information:
Total Rainfall
High Temperature
Low Temperature
Average Temperature
Use an input file called input.txt to load the data to your array of 12 structures to hold the weather data (the average temperature should be calculated). Once the data for all the months is entered, the program should calculate and display the monthly rainfall, total rainfall for the year, highest and lowest temps for the year (and the months they happen in) and the average of all of the months average temperatures.
Validatation Input temps should be between -100 and +140 and and total rainfall must not be less than 0
input file txt
Explanation / Answer
PROGRAM CODE:
/*
* RainfallTemp.cpp
*
* Created on: 09-Apr-2017
* Author: kasturi
*/
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
struct Rainfall
{
string month;
double totalRainfall;
double highTemp;
double lowTemp;
double AvgTemp;
};
void readFile(struct Rainfall rainfall[])
{
ifstream in("input.txt");
string line;
int counter = 0;
while(getline(in, line))
{
istringstream iss(line);
string month;
double rain, highTemp, lowtemp;
if(!(iss>>month>>rain>>highTemp>>lowtemp))
{
}
rainfall[counter].month = month;
rainfall[counter].totalRainfall = rain;
rainfall[counter].highTemp = highTemp;
rainfall[counter].lowTemp = lowtemp;
rainfall[counter].AvgTemp = (highTemp + lowtemp)/2;
counter++;
}
}
int main()
{
struct Rainfall rainfall[12];
readFile(rainfall);
double maxTemp = rainfall[0].highTemp, minTemp = rainfall[0].lowTemp, totalRainfall = 0.0;
for(int i=0; i<12; i++)
{
totalRainfall += rainfall[i].totalRainfall;
if(rainfall[i].highTemp > maxTemp)
maxTemp = rainfall[i].highTemp;
if(rainfall[i].lowTemp < minTemp)
minTemp = rainfall[i].lowTemp;
cout<<"Rainfall for "<<rainfall[i].month<<" is "<<rainfall[i].totalRainfall<<endl;
}
cout<<" Total rainfall for the year is "<<totalRainfall<<endl;
cout<<"Highest temperature for the year is "<<maxTemp<<endl;
cout<<"Lowest temperature for the year is "<<minTemp<<endl;
return 0;
}
OUTPUT:
Rainfall for January is 0
Rainfall for February is 0
Rainfall for March is 5
Rainfall for April is 25
Rainfall for May is 12
Rainfall for June is 18
Rainfall for July is 6
Rainfall for August is 18
Rainfall for September is 16
Rainfall for October is 12
Rainfall for November is 5
Rainfall for December is 0
Total rainfall for the year is 117
Highest temperature for the year is 102
Lowest temperature for the year is -30
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.