Answer to Question #284782 in C++ for Harshika

Question #284782

Craete a class city which have data for average temperature accept data from the user for 5 different cities and display the information on screen

1
Expert's answer
2022-01-04T10:47:49-0500
#include <iostream> 
#include<string>

using namespace std;

class City
{
	string city;
	float averTemp;
public:
	City(string _city):city(_city){}
	void SetData()
	{
		float tmp;
		float sumTmp=0;
		int count=0;
		cout<<"\nPlease, enter temperatures for "<<city<<" (100 - exit):";
		do
		{
			cin>>tmp;
			if(tmp==100)break;
			sumTmp+=tmp;
			count++;
		}while(true);
		averTemp=(float)sumTmp/count;
	}
	void Display()
	{
		cout<<"\nAverage temperature in "<<city<<" is "<<averTemp;
	}
};

int main()
{
	City* cities[5]={new City("Paris"),new City("New York"),
		new City("London"),new City("Madrid"),new City("Amsterdam")};
	
	for(int i=0;i<5;i++)
	{
		cities[i]->SetData();
	}
	for(int i=0;i<5;i++)
	{
		cities[i]->Display();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog