1 Replies - 185 Views - Last Post: 07 November 2017 - 10:37 AM Rate Topic: -----

#1 11ash1  Icon User is offline

  • New D.I.C Head

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

How to organize between .h and .cpp?

Posted 07 November 2017 - 10:28 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;
};


Is This A Good Question/Topic? 0
  • +

Replies To: How to organize between .h and .cpp?

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13483
  • View blog
  • Posts: 53,820
  • Joined: 12-June 08

Re: How to organize between .h and .cpp?

Posted 07 November 2017 - 10:37 AM

Please do not create duplicate topics. Closing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1