Question: project 7 Refactor your program so that the code for - temperature is in two files (.h declarations) and (.cpp implementations) - wind is in
project 7
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
Someone told me that this has to be done. Please help. I will really appreciate it:
The first thing you need to do is split everything into functions instead of cramming everything into main() Then call the functions one by one from main() The three functions should be called Temperature() Wind() & Weather_Measurement() You will need to figure out what parameters each function takes and what it should return. This is why you should never cram everything into main() and should always follow good design principles. You have really done this to yourself by bad design now you will have to break apart your program and refactor it into functions I recommend if using VS is that you start a new instance of VS and start again with a clean new project copying over the snippets that are still relevant from the old project, the results from user inputs should be stored in a dynamic container such as a std::vector simply to keep track of all the possible inputs.
C++ code using Structs:
#include "stdafx.h" #include #include using namespace std; struct temperature { string name; int choice; int numOfReadings = 0; int size; double temperatures[4], windSpeeds[4]; string windDirection[4]; bool initialized = false; } temp1, temp2,temp3,temp4; //void moveTemperaturesToRight(temperature temperatures[], temperature windSpeeds[], temperature windDirection[]); int main() { char str; cout << "Enter the name of weather station: "; getline(cin, temp2.name); while (true) { cout << "I. Input a complete weather reading." << " "; cout << "P. Print the current weather." << " "; cout << "H. Print the weather history (from most recent to oldest)." << " "; cout << "E. Exit the program." << " "; cout << "Enter your choice: "; cin >> str; if (str != 'I'&& str != 'P'&& str != 'H'&& str != 'E') temp4.choice = 0; else temp4.choice = str; //Switch based on choice. switch (temp4.choice) { case 'I': //moveTemperaturesToRight(temperature temperatures[], temperature windSpeeds[], temperature windDirection[]); cout << "Enter the temperature:"; cin >> temp1.temperatures[0]; do { cout << "Enter the wind speed (a value >=0):"; cin >> temp1.windSpeeds[0]; while (cin.fail() || (cin.peek() != ' ' && cin.peek() != ' ')) { cout << "Invalid Input! Re Enter the wind speed" << endl; cin.clear(); while (cin.get() != ' '); cin >> temp1.windSpeeds[0]; } } while (temp1.windSpeeds[0] < 0); //get correct wind direction do { cout << "Enter the wind direction (North,South,East or West):"; cin >> temp2.windDirection[0]; } while (temp2.windDirection[0] != "North" && temp2.windDirection[0] != "South" && temp2.windDirection[0] != "East" && temp2.windDirection[0] != "West"); temp3.initialized = true; if (temp3.initialized) temp4.numOfReadings++; if (temp4.numOfReadings > 4) temp4.numOfReadings = 4; break; case 'H': //Print the current weather, if valid weather is entered. cout << "Enter size of the history wants:"; cin >> temp4.size; if (temp4.numOfReadings temperature.h
#pragma once
temperature.cpp
wind.h
#pragma once
wind.cpp
weatherMeasurement.h
#pragma once
weatherMeasurement.cpp