Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C++
#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()
{
	bool initialized = false;
	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 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
			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

C++
#pragma once
double temperature;


wind.h
C++
#pragma once
string windDirection;
double windSpeed;


WeatherMeasurement.h
C++
#pragma once


temperature.cpp
C++
#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
C++
#include "stdafx.h"


wind.cpp
C++
#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;
};


What I have tried:

I am new to programming so I am not even sure how to start it.
Posted
Updated 7-Nov-17 20:23pm
v3
Comments
jeron1 7-Nov-17 18:15pm    
Instead of starting a new thread, you should just update the thread you started yesterday.
Member 13141830 7-Nov-17 18:31pm    
Alright, but can you help me. This is really giving me trouble
GKP1992 7-Nov-17 23:03pm    
You need to start reading really.
Start with this https://www.amazon.com/C-Primer-Stanley-B-Lippman/dp/0321714113.

It is convention in the language c and C++ to seperate code for better readability. In a h. file are the declaration of functions, classes and constants. And in the cpp-files is the implementation of the declared stuff of the header.

Your WeatherMeasurement files are empty. It is your job to write some code for "WeatherMeasurement".

You should watcht this C++ beginners tutorial to learn the basics or read some entry level C++ book to get started.

Tip: Learn writing "Clean Code" and use classes.
 
Share this answer
 
Comments
CPallini 8-Nov-17 3:58am    
5.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900