Как я использую .h и .cpp?
Я пытаюсь отформатировать его в .h и .cpp. Я не знаю, как это на самом деле должно быть сделано. вот что у меня есть. Кроме того, я не знаю, что делать с измерением погоды. Спасибо
Код на языке 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; }
температуры.ч
#pragma once double temperature;
ветра.ч
#pragma once string windDirection; double windSpeed;
WeatherMeasurement.ч
#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; };
Что я уже пробовал:
Я новичок в программировании, поэтому даже не знаю, как его начать.
jeron1
Вместо того, чтобы начинать новый поток, вы должны просто обновить поток, который вы начали вчера.
Member 13141830
Хорошо, но не могли бы вы мне помочь? Это действительно доставляет мне неприятности
GKP1992
Тебе нужно начать читать по-настоящему.
Начните с этого https://www.amazon.com/C-Primer-Stanley-B-Lippman/dp/0321714113.