How To Encapsulate Functions In Code?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

41 Replies - 1085 Views - Last Post: 07 November 2017 - 11:28 AM Rate Topic: -----

#16 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,114
  • Joined: 05-May 12

Re: How To Encapsulate Functions In Code?

Posted 03 November 2017 - 12:23 PM

Structs are just classes with everything marked as public by default. :)
Was This Post Helpful? 0
  • +
  • -

#17 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,316
  • Joined: 20-March 10

Re: How To Encapsulate Functions In Code?

Posted 03 November 2017 - 12:44 PM

Yes Im aware of that.... :)
Was This Post Helpful? 0
  • +
  • -

#18 11ash1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 25-October 15

Re: How To Encapsulate Functions In Code?

Posted 03 November 2017 - 05:28 PM

Hello, this is project 4:

project 4
Extend the functionality of project 3 to incorporate functions that encapsulate the four actions to take:
- Input a complete weather reading.
- temperature
- wind speed
- wind direction
- print the current weather
- If the user hasn't entered any data then print a message to that effect
- Exit the program
Add error checking on user inputs

I know it works, but I want it to be changed where the user's actions are not encapsulated in functions. I want them to be. The code is below. This is really important. I am not sure how to do this. Can someone help. Thank you. I appreciate it.

C++ code:

#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
int choice;
double temperature, windSpeed;
string windDirection;
bool initialized = false;
string str;
  
//Have the user provide a name for the weather station upon entry
cout << "Enter the name of weather station: ";
getline(cin, name);
  
//control loop to perform various actions
while(true)
{
cout << "1. Input a complete weather reading." << "\n";
cout << "2. Print the current weather." << "\n";
cout << "3. Exit the program." << "\n";
cout << "Enter your choice: ";
cin >> str;
if(str.length() != 1 || str < "1" || str > "3")
choice = 0;
else
choice = atoi(str.c_str());
  
//switch based on choice
switch(choice)
{
case 1:
do
{
cout << "Enter the temperature (a value >= 0): ";
cin >> temperature;
}while(temperature < 0);
  
  
//get correct wind speed
do
{
cout << "Enter the wind speed (a value >= 0): ";
cin >> windSpeed;
}while(windSpeed < 0);
  
//get correct wind direction
do
{
cout << "Enter the wind direction (North, South, East or West): ";
cin >> windDirection;
}while(windDirection != "North" && windDirection != "South"
&& windDirection != "East" && windDirection != "West");
  
  
initialized = true;
  
if(initialized)
{
cout << "*****" << name << "******" << endl;
cout << "Temperature: " << temperature << endl;
cout << "Wind Speed: " << windSpeed << endl;
cout << "Wind Direction: " << windDirection << endl;
  
}
break;
  
case 2:
if(!initialized)
{
cout << "Please enter the details before asking to print." << endl;
}
else
{
cout << "*****" << name << "******" << endl;
cout << "Temperature: " << temperature << endl;
cout << "Wind Speed: " << windSpeed << endl;
cout << "Wind Direction: " << windDirection << endl;
  
}
break;
case 3:
return 0; //stop execution
default:
cout << "Invalid choice. Please follow the menu." << endl;
  
}
  
}
}

Was This Post Helpful? 0
  • +
  • -

#19 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13488
  • View blog
  • Posts: 53,879
  • Joined: 12-June 08

Re: How To Encapsulate Functions In Code?

Posted 03 November 2017 - 05:33 PM

Please do not put homework questions in the discussion lounge. Moving up a thread.
Was This Post Helpful? 0
  • +
  • -

#20 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,316
  • Joined: 20-March 10

Re: How To Encapsulate Functions In Code?

Posted 03 November 2017 - 05:37 PM

Also you have another thread open on the same subject now I will have to merge threads...

Please 11ash1 do not start new topics on the same subject... found here...

http://www.dreaminco...ganize-my-code/


Also on this very thread I give you a reasonable design pattern to follow and so does Skydiver

merging...
Was This Post Helpful? 0
  • +
  • -

#21 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2009
  • View blog
  • Posts: 5,388
  • Joined: 27-December 05

Re: How To Encapsulate Functions In Code?

Posted 03 November 2017 - 06:23 PM

View PostSkydiver, on 03 November 2017 - 03:23 PM, said:

Structs are just classes with everything marked as public by default. :)/>



View Postsnoopy11, on 03 November 2017 - 03:44 PM, said:

Yes Im aware of that.... :)/>


Yes but OP is most likely not aware of that. OP seems to be in an intro to programming class using C++ in which classes are introduced only after covering screen I/O, conditional statements, loops, file I/O, functions, arrays, strings, simple sorting and searching. The struct aspect of this assignment is probably going to serve as the segue into classes -- maybe next week.
Was This Post Helpful? 0
  • +
  • -

#22 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,316
  • Joined: 20-March 10

Re: How To Encapsulate Functions In Code?

Posted 03 November 2017 - 06:44 PM

Yes I can imagine it will and I recognise that his teacher is moving through the gears at some pace too....
Was This Post Helpful? 0
  • +
  • -

#23 11ash1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 25-October 15

Re: How To Encapsulate Functions In Code?

Posted 04 November 2017 - 06:30 AM

***EDIT massive quote from me snoopy11 removed***

Hello, is this any better than it was?
#include <iostream>

#include<string>

#include<vector>

#include<sstream>

using namespace std;

struct Weather_Station

{

string name;

double temperatures[4];

double windSpeeds[4];

string windDirections[4];

};

string DisplayMenu(string station_name)

{

string str, temp;

  

do

{

cout << "*******************WEATHER STATION: " << station_name << " *******************"<< endl << endl;

cout << "I. Input a complete weather reading." << endl;

cout << "P. Print the current weather." << "\n";

cout << "H. Print the weather history (from most recent to oldest)." << endl;

cout << "E. Exit the program." << "\n";

cout << "Enter your choice: " << endl;

cin >> str;

temp = str;

for (std::string::size_type i = 0; i < str.length(); ++i)

temp[i] = toupper(str[i]);

str = temp;

  

  

} while (!(str == "I" || str == "P" || str == "H" || str == "E"));

return str;

}

/////////////////////////////My code start////////////////////////////////////////////////

void moveTemperaturesToRight(double temperatures[],double windSpeed[],string windDirection[])

{

for (int i = 3; i > 0; i--)

{

temperatures[i] = temperatures[i - 1];

windSpeed[i] = windSpeed[i - 1];

windDirection[i] = windDirection[i - 1];

}

}

/////////////////////////////////End//////////////////////////////////////////

int main()

{

//Have the user provide a name for the weather station upon entry.

int numOfReadings = 0;

int size;

bool initialized = false;

vector<Weather_Station> myStation;

Weather_Station myWeather_Details;

string station_name, input_choice;

cout << "Enter the name of Weather Station: ";

getline(cin, station_name);

myWeather_Details.name = station_name;

//Control loop to perform various actions

input_choice = DisplayMenu(station_name);

if (input_choice == "I")

{

moveTemperaturesToRight(myWeather_Details.temperatures,myWeather_Details.windSpeeds,myWeather_Details.windDirections);

cout << "Enter the temperature:";

cin >> myWeather_Details.temperatures[0];

//get correct wind speed

do

{

cout << "Enter the wind speed (a value >=0):";

cin >> myWeather_Details.windSpeeds[0];

while (cin.fail() || (cin.peek() != '\r' && cin.peek() != '\n'))

{

cout << "Invalid Input! Re Enter the wind speed" << endl;

cin.clear();

while (cin.get() != '\n');

cin >> myWeather_Details.windSpeeds[0];

}

} while (myWeather_Details.windSpeeds[0] < 0);

//get correct wind direction

do

{

cout << "Enter the wind direction (North,South,East or West):";

cin >> myWeather_Details.windDirections[0];

} while (myWeather_Details.windDirections[0] != "North" && myWeather_Details.windDirections[0] != "South" && myWeather_Details.windDirections[0] != "East" && myWeather_Details.windDirections[0] != "West");

initialized = true;

if (initialized)

numOfReadings++;

if (numOfReadings > 4)

numOfReadings = 4;

}

else if (input_choice == "H")

{

//Print the current weather, if valid weather is entered.

cout << "Enter size of the history wants:";

cin >> size;

if (numOfReadings<size)

{

cout << "History size is high";

}

for (int i = 0; i < size; i++)

{

cout << "*****" << myWeather_Details.name << "*****" << "\n";

cout << "Temperature: " << myWeather_Details.temperatures[i] << "\n";

cout << "Wind speed: " << myWeather_Details.windSpeeds[i] << "\n";

cout << "Wind direction: " << myWeather_Details.windDirections[i] << "\n" << "\n";

}

if (numOfReadings == 0)

cout << "Please enter the details before asking to print." << "\n";

}

else if (input_choice == "P")

{

if (numOfReadings == 0)

{

cout << "Please enter the details before asking to print." << "\n";

}

cout << "*****" << myWeather_Details.name << "*****" << "\n";

cout << "Temperature: " << myWeather_Details.temperatures[0] << "\n";

cout << "Wind speed: " << myWeather_Details.windSpeeds[0] << "\n";

cout << "Wind direction: " << myWeather_Details.windDirections[0] << "\n" << "\n";

}

else if (input_choice == "E")

{

return 0; //Stops execution.

}

else

{

cout << "Invalid choice. Please follow the menu." << "\n";

}

//store the details

myStation.push_back(myWeather_Details);

return 0;

}

This post has been edited by snoopy11: 04 November 2017 - 07:03 AM
Reason for edit:: removed quote from snoopy11, stop making massive quotes that are totally unnecessary !!

Was This Post Helpful? 0
  • +
  • -

#24 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,316
  • Joined: 20-March 10

Re: How To Encapsulate Functions In Code?

Posted 04 November 2017 - 07:00 AM

No,

Not really I was expecting you to follow the design pattern i suggested and move windspeed and wind direction functions outside of main()

but you went the other way and took the getTemperature function out and put it inside main...

Please try again...

This time create functions outside of main that handle wind speed, temperature, wind direction, the printing of the current weather and the printing of the entire history of the weather station.

and are simply called by main...

and stop making massive quotes that I am forced to remove I have enough to do... !!
Was This Post Helpful? 0
  • +
  • -

#25 11ash1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 25-October 15

Re: How To Encapsulate Functions In Code?

Posted 04 November 2017 - 10:35 AM

I am sorry. This is just really hard for me. Is this one better?
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include<string>
#include <stdlib.h>
#include<vector>

#include<sstream>

using namespace std;

struct Weather_Station
{
	string name;
	double temperature;
	double windspeed;
	string windDirection;
};

string DisplayMenu(string station_name)
{
	string str, temp;
	do
	{
		cout << "*******************WEATHER STATION: " << station_name\
			<< " *******************" << endl << endl;
		cout << "I. Input a complete weather reading." << endl;
		cout << "P. Print the current weather." << "\n";
		cout << "H. Print the weather history (from most recent to oldest)." << endl;
		cout << "E. Exit the program." << "\n";
		cout << "Enter your choice: " << endl;
		cin >> str;
		temp = str;
		for (std::string::size_type i = 0; i < str.length(); ++i)
			temp[i] = toupper(str[i]);
		str = temp;
	} while (!(str == "I" || str == "P" || str == "H" || str == "E"));

	return str;

}

double getTemperature()

{

	double temp;

	string temp_string;

	stringstream converter;

	cout << "Enter the temperature: ";

	cin >> temp_string;

	converter << temp_string;

	converter >> temp;

	return temp;

}
double getWindSpeed()

{

	double temp;

	string temp_string;

	stringstream converter;

	cout << "Enter Wind speed: ";

	cin >> temp_string;

	converter << temp_string;

	converter >> temp;

	return temp;

}
string getWindDirection()

{

	string temp_string;


	cout << "Enter the Wind direction: ";

	cin >> temp_string;

	return temp_string;

}
void printWeather(Weather_Station ws)
{
	cout << "Station Name " << ws.name << endl;
	cout << "Temperature " << ws.temperature << endl;
	cout << "Wind Direction " << ws.windDirection << endl;
	cout << "Wind Speed " << ws.windspeed << endl;
	cout << endl;
}
int main()
{
	//Have the user provide a name for the weather station upon entry.

	vector<Weather_Station> myStation;

	Weather_Station myWeather_Details;

	string station_name, input_choice;

	cout << "Enter the name of Weather Station: ";

	getline(cin, station_name);

	myWeather_Details.name = station_name;
	while (1)
	{

		//Control loop to perform various actions

		input_choice = DisplayMenu(station_name);

		if (input_choice == "I")
		{

			// get the details

			myWeather_Details.temperature = getTemperature(); // get temperature

			myWeather_Details.windDirection = getWindDirection(); //get wind direction
			myWeather_Details.windspeed = getWindSpeed(); //get wind direction
														  //store the details
			myStation.push_back(myWeather_Details);

		}
		else if (input_choice == "P")
		{
			cout << "*************Printing Current Weather*************" << endl;
			printWeather(myStation.back());
		}
		else if (input_choice == "H")
		{
			cout << "*************Printing Weather History*************" << endl;
			vector<Weather_Station>::reverse_iterator rit;
			for (rit = myStation.rbegin(); rit != myStation.rend(); rit++)
				printWeather(*rit);
		}
		else if (input_choice == "E")
		{
			exit(0);
		}
	}

	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#26 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,316
  • Joined: 20-March 10

Re: How To Encapsulate Functions In Code?

Posted 04 November 2017 - 10:51 AM

Yes thats a lot better I would change


getWindDirection to something like this though

string getWindDirection()
{
	
	string temp_string, temp;
	
	do {
		cout << "Enter the Wind Direction (North,South,East,West): ";
		cin >> temp_string;
		temp = temp_string;
		for (std::string::size_type i = 0; i < temp_string.length(); ++i)
			temp[i] = toupper(temp_string[i]);

	   } while (!(temp == "NORTH" || temp == "SOUTH" || temp == "EAST" || temp == "WEST" || temp == "N" || temp == "S" || temp == "E") || temp == "W");

	temp_string = temp;
	if (temp_string == "N")
		temp_string = "NORTH";
	if (temp_string == "S")
		temp_string = "SOUTH";
	if (temp_string == "W")
		temp_string = "WEST";
	if (temp_string == "E")
		temp_string = "EAST";
	return temp_string;

};



It will standardise the Data better...

But Well done !

Now you have to split the functions into .h and .cpp files just leaving int main() as it is !
Was This Post Helpful? 1
  • +
  • -

#27 11ash1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 25-October 15

Re: How To Encapsulate Functions In Code?

Posted 04 November 2017 - 11:07 AM

Thanks. What I just did was editing and changing things up without structs, and then I am adding them after I finish. There are still functions in my program that I need to add.

Thanks for your help. My teacher cares more about the fact if it works, and not really the design so I had to really change this program.
Was This Post Helpful? 0
  • +
  • -

#28 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,316
  • Joined: 20-March 10

Re: How To Encapsulate Functions In Code?

Posted 04 November 2017 - 11:34 AM

Also remember this part of project 7

Refactor your program so that the code for

- temperature is in two files (.h declarations) and (.cpp implementations)

- wind is in two files (.h declarations) and (.cpp implementations)

-WeatherMeasurement is in two files (.h declarations) and (.cpp implementations)

- And your main is in one file

however it should be more obvious to you on how to go about this now.
Was This Post Helpful? 0
  • +
  • -

#29 11ash1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 25-October 15

Re: How To Encapsulate Functions In Code?

Posted 05 November 2017 - 08:34 PM

I made a few changes to my program. I needed to add more functionality.
here is the code:
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include<string>
#include <stdlib.h>
#include<vector>
#include<sstream>

using namespace std;

struct Weather_Station
{
	string name;
	double temperature;
	double windspeed;
	string windDirection;
};

string DisplayMenu(string station_name)
{
	string str, temp;
	do
	{
		cout << "*******************WEATHER STATION: " << station_name\
			<< " *******************" << endl << endl;
		cout << "I. Input a complete weather reading." << endl;
		cout << "P. Print the current weather." << "\n";
		cout << "H. Print the weather history (from most recent to oldest)." << endl;
		cout << "E. Exit the program." << "\n";
		cout << "Enter your choice: " << endl;
		cin >> str;
		temp = str;
		for (std::string::size_type i = 0; i < str.length(); ++i)
			temp[i] = toupper(str[i]);
		str = temp;
	} while (!(str == "I" || str == "P" || str == "H" || str == "E"));

	return str;

}

double getTemperature()

{

	double temp;

	string temp_string;

	stringstream converter;

	cout << "Enter the temperature: ";

	cin >> temp_string;

	converter << temp_string;

	converter >> temp;

	return temp;

}
double getWindSpeed()

{

	double temp;

	string temp_string;

	stringstream converter;

	//this loop will be iterated continuously untill user enters windspeed which is greater than zero
	cout << "Enter Wind speed: ";

	cin >> temp_string;

	converter << temp_string;

	converter >> temp;

	if (temp <= 0)
		cout << "Wind speed should be always greater than 0(zero)";
}
while (temp <= 0);

	return temp;

	 }
	 string getWindDirection()
{
	string temp_string, temp;

	do {
		cout << "Enter the Wind Direction (North,South,East,West): ";
		cin >> temp_string;
		temp = temp_string;
		for (std::string::size_type i = 0; i < temp_string.length(); ++i)
			temp[i] = toupper(temp_string[i]);

	} while (!(temp == "NORTH" || temp == "SOUTH" || temp == "EAST" || temp == "WEST" || temp == "N" || temp == "S" || temp == "E") || temp == "W");

	temp_string = temp;
	if (temp_string == "N")
		temp_string = "NORTH";
	if (temp_string == "S")
		temp_string = "SOUTH";
	if (temp_string == "W")
		temp_string = "WEST";
	if (temp_string == "E")
		temp_string = "EAST";
	return temp_string;

};

void printWeather(Weather_Station ws)
{
	cout << "Station Name " << ws.name << endl;
	cout << "Temperature " << ws.temperature << endl;
	cout << "Wind Direction " << ws.windDirection << endl;
	cout << "Wind Speed " << ws.windspeed << endl;
	cout << endl;
}
int main()
{
	//Have the user provide a name for the weather station upon entry.

	vector<Weather_Station> myStation;

	Weather_Station myWeather_Details;

	string station_name, input_choice;

	int histCount = 0;

	cout << "Enter the name of Weather Station: ";

	getline(cin, station_name);

	myWeather_Details.name = station_name;
	while (1)
	{

		//Control loop to perform various actions

		input_choice = DisplayMenu(station_name);

		if (input_choice == "I")
		{

			// get the details

			myWeather_Details.temperature = getTemperature(); // get temperature

			myWeather_Details.windDirection = getWindDirection(); //get wind direction
			myWeather_Details.windspeed = getWindSpeed(); //get wind direction
														  //store the details
			myStation.push_back(myWeather_Details);

		}
		else if (input_choice == "P")
		{
			cout << "*************Printing Current Weather*************" << endl;
			printWeather(myStation.back());
		}
		else if (input_choice == "H")
		{
			//this loop will be iterated continuously untill user gives the input count more than 0 and it is not greater than available record count in vector
			do {
				cout << "Please enter how many records you want" << "\n";
				cin >> histCount;

				if (histCount <= 0)
					cout << "Input record count should always be greater than 0(zero)"<<"\n";
				else if (histCount>>myStation.size())
					cout << "Input record count shouldn't be more than available record count"<<"\n";


			} while (histCount <= 0 || histCount>>myStation.size());

			cout << "*************Printing Weather History*************" << endl;
			vector<Weather_Station>::reverse_iterator rit;
			for (rit = myStation.rbegin(); rit != myStation.rend(); rit++)
				printWeather(*rit);
		}
		else if (input_choice == "E")
		{
			exit(0);
		}
	}

	return 0;
}


Can you help me solve my problem though. I am getting declaration error. I am not sure what is wrong though.
This is the code
if (temp <= 0)
		cout << "Wind speed should be always greater than 0(zero)";
}
while (temp <= 0);

	return temp;

	 }


I am getting errors for both while and the return

This post has been edited by 11ash1: 05 November 2017 - 08:35 PM

Was This Post Helpful? 0
  • +
  • -

#30 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2009
  • View blog
  • Posts: 5,388
  • Joined: 27-December 05

Re: How To Encapsulate Functions In Code?

Posted 05 November 2017 - 08:36 PM

How many threads are you going to post this question in?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3