How To Encapsulate Functions In Code?

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

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

#31 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

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

Re: How To Encapsulate Functions In Code?

Posted 05 November 2017 - 08:40 PM

You are missing a

do{

at the start of the while loop...

I would recommend revising do... while ... loops

your indentation is all messed up too... indent your code properly please..
Was This Post Helpful? 0
  • +
  • -

#32 11ash123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-November 17

Re: How To Encapsulate Functions In Code?

Posted 06 November 2017 - 04:32 PM

Hello, I am told that I am needed to:
Project 7

Change the program to use structs for temperature, wind, and weather measurement.

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

I am not sure what it wants me to do. I needed to change my last program using structs which I did, but I have no idea how to do this. If someone could help I will really appreciate it.

C++ 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)";
 
}
 
do {
 
} 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;
 
}

Was This Post Helpful? 0
  • +
  • -

#33 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5074
  • View blog
  • Posts: 13,697
  • Joined: 18-April 07

Re: How To Encapsulate Functions In Code?

Posted 06 November 2017 - 04:45 PM

Well first you should start creating some files. Some with an .h extension and others with .cpp. Your cpp files are going to include your header files. Take a look at the following thread on SO to see examples of how you might set this up.

https://stackoverflo...n-a-header-file

Then of course you need to start building some structs for your various pieces of data. You already built one for weather station, so do something similar for the others. Try this out a bit and show us what you have when you get stuck.
Was This Post Helpful? 0
  • +
  • -

#34 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5898
  • View blog
  • Posts: 20,138
  • Joined: 05-May 12

Re: How To Encapsulate Functions In Code?

Posted 06 November 2017 - 05:37 PM

11ash123: When you signed up for this site, one of the things you agreed to is not to create a duplicate account. If you've you've forgotten the logon for your old account, please let us know and we can make arrangements to merge your accounts and get you access again.
Was This Post Helpful? 0
  • +
  • -

#35 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 07 November 2017 - 07:25 AM

Hello, I have a program right now where the wind speed of my program once you enter the value that is to low and prompts you to re enter a value no matter what value you enter it still prompts you to re enter. Also, I would like to know if I did structs right in my program. I am not sure how to fix this. Thank you

C++ code:

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include<string>
#include <stdlib.h>
#include<vector>
#include<sstream>
using namespace std;
struct WindMeasurement
{
	double windspeed;
	string windDirection;
};
struct TemperatrueMeasurement
{
	double temperature;
};
struct Weather_Station
{
	string name;
	TemperatrueMeasurement temperatureMeasure;
	WindMeasurement windMeasure;
};
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
	do {
		cout << "Enter Wind speed(>=0): ";
		cin >> temp_string;
		converter << temp_string;
		converter >> temp;
		if (temp < 0)
		{
			cout << "Wind speed should be always greater than or equal to 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_string = temp;
	return temp_string;
};
void printWeather(Weather_Station ws)
{
	cout << "Station Name " << ws.name << endl;
	cout << "Temperature " << ws.temperatureMeasure.temperature << endl;
	cout << "Wind Direction " << ws.windMeasure.windDirection << endl;
	cout << "Wind Speed " << ws.windMeasure.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.temperatureMeasure.temperature = getTemperature(); // get temperature
			myWeather_Details.windMeasure.windDirection = getWindDirection(); //get wind direction
			myWeather_Details.windMeasure.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 << "Number of readings entered: " << myStation.size() << endl;
				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;
}

Was This Post Helpful? 0
  • +
  • -

#36 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5898
  • View blog
  • Posts: 20,138
  • Joined: 05-May 12

Re: How To Encapsulate Functions In Code?

Posted 07 November 2017 - 07:27 AM

Please don't open new threads about essentially the same code base. Merging threads again...

Consider this an official warning. Future duplicate threads will just be closed instead of merged.
Was This Post Helpful? 0
  • +
  • -

#37 jimblumberg  Icon User is offline

  • member icon

Reputation: 5336
  • View blog
  • Posts: 16,618
  • Joined: 25-December 09

Re: How To Encapsulate Functions In Code?

Posted 07 November 2017 - 07:33 AM

You need to either reset or clear your stringstream in that loop.

Jim
Was This Post Helpful? 0
  • +
  • -

#38 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 07 November 2017 - 07:49 AM

Hello, I fixed my errors with wind speed, but now if I first enter a positive value for wind speed initially it works, but then if I enter a negative value for the second input it prompts me it is low and to re enter, but then no matter what value I enter it prompts me to re enter. Also, I fixed my structs so is it good now

C++ code:

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include<string>
#include <stdlib.h>
#include<vector>
#include<sstream>
using namespace std;
struct WindMeasurement
{
	double windspeed;
	string windDirection;
};
struct TemperatrueMeasurement
{
	double temperature;
};
struct Weather_Station
{
	string name;
	TemperatrueMeasurement temperatureMeasure;
	WindMeasurement windMeasure;
};
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
	do {
		cout << "Enter Wind speed(>=0): ";
		cin >> temp_string;
		converter << temp_string;
		converter >> temp;
		if (temp < 0)
		{
			cout << "Wind speed should be always greater than or equal to 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_string = temp;
	return temp_string;
};
void printWeather(Weather_Station ws)
{
	cout << "Station Name " << ws.name << endl;
	cout << "Temperature " << ws.temperatureMeasure.temperature << endl;
	cout << "Wind Direction " << ws.windMeasure.windDirection << endl;
	cout << "Wind Speed " << ws.windMeasure.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.temperatureMeasure.temperature = getTemperature(); // get temperature
			myWeather_Details.windMeasure.windDirection = getWindDirection(); //get wind direction
			myWeather_Details.windMeasure.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 << "Number of readings entered: " << myStation.size() << endl;
				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;
}

Was This Post Helpful? 0
  • +
  • -

#39 jimblumberg  Icon User is offline

  • member icon

Reputation: 5336
  • View blog
  • Posts: 16,618
  • Joined: 25-December 09

Re: How To Encapsulate Functions In Code?

Posted 07 November 2017 - 07:59 AM

Quote

but then no matter what value I enter it prompts me to re enter.

I already told you what you need to do to fix this problem, is there something you don't understand about my last post?


Jim
Was This Post Helpful? 0
  • +
  • -

#40 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 07 November 2017 - 10:10 AM

I am trying to format it in the .h and .cpp. I am not sure how it is really supposed to be done. here is what I have. Also, I am not sure what to do with WeatherMeasurement. Thank you

C++ code:
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include<string>
#include <stdlib.h>
#include<vector>
#include<sstream>
using namespace std;
struct WindMeasurement
{
	double windspeed;
	string windDirection;
};
struct TemperatrueMeasurement
{
	double temperature;
};
struct Weather_Station
{
	string name;
	TemperatrueMeasurement temperatureMeasure;
	WindMeasurement windMeasure;
};
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(>=0): ";
	cin >> temp_string;
	converter << temp_string;
	converter >> temp;
	if (temp < 0)
	{
		cout << "Wind speed should be always greater than or equal to 0(zero)";
	}
	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_string = temp;
	return temp_string;
};
void printWeather(Weather_Station ws)
{
	cout << "Station Name " << ws.name << endl;
	cout << "Temperature " << ws.temperatureMeasure.temperature << endl;
	cout << "Wind Direction " << ws.windMeasure.windDirection << endl;
	cout << "Wind Speed " << ws.windMeasure.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
			int valid_wind_direction = 0, valid_wind_speed = 0;
			myWeather_Details.temperatureMeasure.temperature = getTemperature(); // get temperature
			myWeather_Details.windMeasure.windDirection = getWindDirection(); //get wind direction
			myWeather_Details.windMeasure.windspeed = getWindSpeed(); //get wind direction
			if (myWeather_Details.windMeasure.windspeed >= 0)
			{
				valid_wind_speed = 1;

			}
			if ((myWeather_Details.windMeasure.windDirection == "NORTH") ||
				(myWeather_Details.windMeasure.windDirection == "SOUTH") ||
				(myWeather_Details.windMeasure.windDirection == "EAST") ||
				(myWeather_Details.windMeasure.windDirection == "WEST"))
			{
				valid_wind_direction = 1;
			}

			//store the details
			if (valid_wind_direction && valid_wind_speed)
				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 << "Number of readings entered: " << myStation.size() << endl;
				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;
}


temperature.h
#pragma once
double temperature;


wind.h

#pragma once
string windDirection;
double windSpeed;


WeatherMeasurement.h
#pragma once


temperature.cpp

#include "stdafx.h"
#include "temperature.h"

double temp;
string temp_string;
stringstream converter;
cout << "Enter the temperature: ";
cin >> temp_string;
converter << temp_string;
converter >> temp;
return temp;


WeatherMeasurement.cpp
#include "stdafx.h"


wind.cpp
#include "stdafx.h"
#include "wind.h"
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(>=0): ";
	cin >> temp_string;
	converter << temp_string;
	converter >> temp;
	if (temp < 0)
	{
		cout << "Wind speed should be always greater than or equal to 0(zero)";
	}
	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_string = temp;
	return temp_string;
};

Was This Post Helpful? 0
  • +
  • -

#41 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 594
  • Posts: 2,491
  • Joined: 13-June 14

Re: How To Encapsulate Functions In Code?

Posted 07 November 2017 - 11:26 AM

The header files are used for the interfaces: the function prototypes and the declarations of your types (classes/structs). The cpp files are then where the implementations go, i.e. the function or method definitions.

This post has been edited by ndc85430: 19 November 2017 - 11:48 AM

Was This Post Helpful? 1
  • +
  • -

#42 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

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

Re: How To Encapsulate Functions In Code?

Posted 07 November 2017 - 11:28 AM

No,

Function declarations in .h

Function Implementations in .cpp

ie

.h

int sum(int a, int c);




in .cpp


int sum(int a, int c)
{
  return a+c;
}



edit: ninja'd by ndc !

This post has been edited by snoopy11: 07 November 2017 - 11:30 AM
Reason for edit:: ninja'd

Was This Post Helpful? 1
  • +
  • -

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