我如何使用.h和.cpp? [英] How do I use .h and .cpp?

查看:80
本文介绍了我如何使用.h和.cpp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在.h和.cpp中格式化它。我不确定应该怎么做。这就是我所拥有的。另外,我不确定如何处理WeatherMeasurement。谢谢



C ++代码:

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()
{
	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





temperature.h

#pragma once
double temperature;





wind.h



wind.h

#pragma once
string windDirection;
double windSpeed;





WeatherMeasurement.h



WeatherMeasurement.h

#pragma once





temperature.cpp



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



WeatherMeasurement.cpp

#include "stdafx.h"





wind.cpp



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;
};





我尝试过:



我是编程的新手,所以我甚至不知道如何启动它。



What I have tried:

I am new to programming so I am not even sure how to start it.

推荐答案

它是c语言和C ++中的惯例单独的代码,以提高可读性。在一个h。 file是函数,类和常量的声明。并且在cpp-files中是标头声明内容的实现。



您的WeatherMeasurement文件为空。为WeatherMeasurement编写一些代码是你的工作。



您应该看到这个 C ++初学者教程学习基础知识或阅读一些入门级C ++书籍以获取开始。



提示:学习编写清洁代码并使用类。
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.


A starter:标题和包含:为什么和如何 - C ++论坛 [ ^ ]。
A starter: Headers and Includes: Why and How - C++ Forum[^].


这篇关于我如何使用.h和.cpp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆