The following is a program from Starting out with C++ by Toni Gaddis. I am getti
ID: 3558898 • Letter: T
Question
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 following weather data
//for a particular month:
//Total Rainfall
//High Temperature
//Low Temperature
//Average Temperature
//The program should have an array of 12 structures to hold weather data for
//an entire year. When the program runs, it should ask the user to enter data
//for each month. (The average temperature should be calculated.) Once the
//data are entered for all the months, the program should calculate and
//display the average monthly rainfall, the total rainfall for the year,
//the highest and lowest temperatures for the year (and the months they
//occur in), and the average of all the monthly average temperatures.
//Input Validation: Only accept temperatures within the range between
//-100 and +140 degrees Fahrenheit.
//Header file section
//#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
struct weather
{
double totalRainfall;
double highTemp;
double lowTemp;
double avgTemp;
};
void main()
{
weather months[12];
double total =0, highest, lowest, avgsum;
int highmonth, lowmonth;
string month[12]= {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December"};
for(int i=0; i<12; i++)
{
cout<<"Enter total rainfall for month "<<month[i]<<": ";
cin>>months[i].totalRainfall;
cout<<"Enter high temperature: ";
cin>>months[i].highTemp;
while (months[i].highTemp < -100 || months[i].highTemp > 140)
{
cout<<"ERROR: Temperature must be in the range of "
<<"-100 through 140. ";
cout<<" High Temperature: ";
cin>>months[i].highTemp;
}
cout<<"Enter low temperature: ";
cin>>months[i].lowTemp;
while (months[i].lowTemp < -100 || months[i].lowTemp > 140)
{
cout<<"ERROR: Temperature must be in the range of "
<<"-100 through 40. ";
cout<<" Low Temperature: ";
cin>>month[i].lowTemp;
}
}
//Calculate the monthly average temperature and total rainfall.
for (int i=0; i<12; i++)
{
total=total + months[i].totalRainfall;
months[i].avgTemp= (months[i].highTemp + month[i].lowTemp)/2;
}
highest=months[0].highTemp;
lowest=months[0].lowTemp;
for(int i=1; i<12; i++)
{
if(months[i].highTemp>highest)
{
highest=months[i].highTemp;
highmonth=i;
}
if(months[i].lowTemp<lowest)
{
lowest=months[i].lowTemp;
lowmonth=i;
}
}
avgsum = 0;
//Calculate the average.
for(int i=0; i<12; i++)
{
avgsum=avgsum+months[i].avgTemp;
}
//Display the calculated data.
cout<<"Average monthly rainfall: "<<total/12<<endl;
cout<<"Total monthly rainfall: "<<total<<endl;
cout<<"Highest rainfall in "<<month[highmonth]<<"is: "<<highest<<endl;
cout<<"Lowest rainfall in "<<month[lowmonth]<<"is: "<<lowest<<endl;
cout<<"Average of average temperatures is: "<<avgsum/12<<endl;
system("pause");
}
Explanation / Answer
//Please check this Solution please
//error caused because here yo used month intstead of months :):)
#include<iostream>
#include<string>
using namespace std;
struct weather
{
double totalRainfall;
double highTemp;
double lowTemp;
double avgTemp;
};
int main()
{
weather months[12];
double total =0, highest, lowest, avgsum;
int highmonth, lowmonth;
string month[12]= {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December"};
for(int i=0; i<12; i++)
{
cout<<"Enter total rainfall for month "<<month[i]<<": ";
cin>>months[i].totalRainfall;
cout<<"Enter high temperature: ";
cin>>months[i].highTemp;
while (months[i].highTemp < -100 || months[i].highTemp > 140)
{
cout<<"ERROR: Temperature must be in the range of "
<<"-100 through 140. ";
cout<<" High Temperature: ";
cin>>months[i].highTemp;
}
cout<<"Enter low temperature: ";
cin>>months[i].lowTemp;
while (months[i].lowTemp < -100 || months[i].lowTemp > 140)
{
cout<<"ERROR: Temperature must be in the range of "
<<"-100 through 40. ";
cout<<" Low Temperature: ";
cin>>months[i].lowTemp;
}
}
//Calculate the monthly average temperature and total rainfall.
for (int i=0; i<12; i++)
{
total=total + months[i].totalRainfall;
months[i].avgTemp= (months[i].highTemp + months[i].lowTemp)/2;
}
highest=months[0].highTemp;
lowest=months[0].lowTemp;
for(int i=1; i<12; i++)
{
if(months[i].highTemp>highest)
{
highest=months[i].highTemp;
highmonth=i;
cout<<highmonth;
}
if(months[i].lowTemp<lowest)
{
lowest=months[i].lowTemp;
lowmonth=i;
cout<<highmonth;
}
}
avgsum = 0;
//Calculate the average.
for(int i=0; i<12; i++)
{
avgsum=avgsum+months[i].avgTemp;
}
//Display the calculated data.
cout<<"Average monthly rainfall: "<<total/12<<endl;
cout<<"Total monthly rainfall: "<<total<<endl;
cout<<"Highest rainfall in "<<month[highmonth]<<"is: "<<highest<<endl;
cout<<"Lowest rainfall in "<<month[lowmonth]<<"is: "<<lowest<<endl;
cout<<"Average of average temperatures is: "<<avgsum/12<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.