So I made a class with an array with a bunch of numbers. I want to know how I can add up all those numbers and get an average for them.

#include <iostream>
#include <string>

using namespace std;


class WeatherStation
{
	string StationDesignation;  //Identifies the station
	string StationAgent;        //Who's responsible
	double Temperature;         //The temperature

public:	
	//Define some mutators...These member functions allow us to
    //change values in the object...

	void SetDesignation(string ID)      { StationDesignation = ID; }
	void SetAgent(string Agent)         { StationAgent = Agent; }
	void SetTemperature(double Degree)  {Temperature = Degree; }

	//Define some accessors...These member functions allow us to get
    //  at the internal values

	string GetDesignation()  { return StationDesignation; }
	string GetAgent()        { return StationAgent; }
	double GetTemperature()  { return Temperature; }

	 //Define a displayer...this will show the contents of the object
	 // in  an easy to read format..

	void Show()
	{
		cout << StationDesignation << endl;
		cout << StationAgent << endl;
	}
	void ShowTemp()
	{
		cout << StationDesignation << "   :               " <<  Temperature << endl;
		cout << "------------------------------------------------" << endl;
		
	}
	
	 
};

//Functions

void Menu();
int AddStation(WeatherStation[],int);
void PostTemperatures(WeatherStation*, int);
void DailyReport(WeatherStation*, int);

int main()
{
	string Command;
	WeatherStation names[25];
	int Size =0;

	while (true){
	Menu();
	cout << "Enter Command: " ;
	getline (cin,Command);
	cout << endl;
	cout << endl;
		if (Command == "Quit")
			break;
		else if (Command == "Add Stations")
			Size = Size + AddStation(names,25);
		else if (Command == "Post Temperatures")
			PostTemperatures (names,Size);
		else if (Command == "Daily Report")
			DailyReport (names, Size);
		else if (Command == "High-Low Report")
			cout << "High-Low Report" << endl;
	}
}
int AddStation(WeatherStation List[], int MaxSize)
{
	string ID, Agent;
	int K;

	cout << "Enter Station Information Below, Stop to Quit" << endl;

	for (K=0 ; K<MaxSize ; K++) {
		cout << "Enter Weather Station Designation: " ;
		getline(cin, ID);
		if (ID == "Stop")
			break;
		cout << "Enter Contact Person: " ;
		getline(cin,Agent);
		if (Agent == "Stop")
			break;

		List[K].SetDesignation(ID);
		List[K].SetAgent(Agent);
	}
	return K;
}

void Menu()  //The Menu
{
	cout << "=========== Menu ===========" << endl;
	cout << "==== Choices: ==============" << endl;
	cout << "Add Stations" << endl;
	cout << "Post Temperatures" << endl;
	cout << "Daily Report" << endl;
	cout << "High-Low Report" << endl;
	cout << "Quit" << endl;
	cout << "==============================" << endl;
}

void PostTemperatures(WeatherStation* List, int Size)
{
	int K;
	int Degree;
	cout << "Enter Reported Temperatures.." << endl;

	for (K=0 ; K<Size ; K++){
		List[K].Show();

		cout << "Enter Temperature: ";
		cin>>Degree;
		
		List[K].SetTemperature(Degree);

	}
	
}

void DailyReport (WeatherStation* List, int Size)
{
	int K;
	cout << "NGS Daily Temperature Report" << endl;
	cout << "================================================" << endl;
	cout << "                      Fahrenheit  Celsius" << endl;
	cout << "------------------------------------------------" << endl;
	for (K=0 ; K<Size ; K++){
		List[K].ShowTemp();
		
		//GET THE AVERAGE OF ALL THE TEMPERATURE, RIGHT HERE SOME HOW
	}
}

Also if you guys can explain the * in void DailyReport (WeatherStation* List, int Size) and what it does. I'm kinda confused.

declare double sum; within your for loop,
sum +=List[K].GetTemperature();
outside of for, output or assign to another variable sum/size;

I'm pretty new to this, so I don't quite understand. Can you show me? I mean I understand the List part but not the size and how it will divide by it.

void DailyReport (WeatherStation* List, int Size)
{
	int K;
        double sum;  //pop this in here to keep our sum
	cout << "NGS Daily Temperature Report" << endl;
	cout << "================================================" << endl;
	cout << "                      Fahrenheit  Celsius" << endl;
	cout << "------------------------------------------------" << endl;
	for (K=0 ; K<Size ; K++){
		List[K].ShowTemp();
		 we're already showing the temps we'll keep track of their 
                 sum in the same loop
		sum+=List[K].GetTemperature();  access the private 
                                                              members via your "getter"
                (or sum = sum+List[K].GetTemperature();) 
                each time we add on the next temp in the list
	}
          now we have a sum of all the temps, so just divide by the size that we passed into the function
      so either    std::cout<<"The average is: "<<(sum/size)<<std::endl;
       or do something else with the sum/size quantity like
                   double average = sum/size;
}

Here is what I did

void DailyReport (WeatherStation* List, int Size)
{
	int K;
	double sum;
	cout << "NGS Daily Temperature Report" << endl;
	cout << "================================================" << endl;
	cout << "                      Fahrenheit  Celsius" << endl;
	cout << "------------------------------------------------" << endl;
	for (K=0 ; K<Size ; K++){
		List[K].ShowTemp();
		sum=sum+List[K].GetTemperature();
	}
	cout << "Mean: " << (sum/Size) << endl;
}

It says sum is starting without being initialized, so i think sum=sum+List[K].GetTemperature(); is wrong.

Nevermind I figured it out. I just set sum=0 and everything works out, thanks alot!

Ok, but what happen if I use K>size. Can you please give the answer.

One more question. Everytime i do Post Temperature and finish entering the temps, It loops my Menu() twices, does anyone know why it does that?

Nevermind I figured it out. I just set sum=0 and everything works out, thanks alot!

Sorry about that I was building that method off the top of my head

One more question. Everytime i do Post Temperature and finish entering the temps, It loops my Menu() twices, does anyone know why it does that?

Try putting a cin.ignore(999,'\n') after your getline in main, there may be extra characters trickling through after your getline. That function gets rid of the first 999 chars until it hits a newline then it tosses that too.

Ok, but what happen if I use K>size. Can you please give the answer.

A big mess but thankfully the for loop is in place. Perhaps I don't understand your question. I suppose you could change k=0 to k=size+1 but then the loop just wouldn't run.....

If this is related to your own assignment, please start a new thread someone will help you.

can you explain the * in
void DailyReport (WeatherStation* List, int Size)? I'm not quite sure what it does

List is a pointer to a WeatherStation object (essentially an address). See this post http://www.daniweb.com/forums/thread11705.html (mainly for Narue's contribution) and/or google pointers, pass by value vs. pass by pointer (which is really pass by value with a pointer a la C) and there's pass by reference.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.