project 6
Extend the functionality of project 5 to
- To allow the user to specify the size of the history on entry to the program
Use a text driven menu to present the choices for actions to the user
Also, make it so that when you press a letter or anything other than a number for wind speed and wind direction it prompts the user to re-enter. Keep the functionality from project 5 the exact same and just add on to it.
C++ code:
#include "stdafx.h" #include #include using namespace std; void moveTemperaturesToRight(double temperatures[], double windSpeed[], string windDirection[]) { for (int i = 3; i > 0; i--) { temperatures[i] = temperatures[i - 1]; windSpeed[i] = windSpeed[i - 1]; windDirection[i] = windDirection[i - 1]; } } int main() { string name; int choice; int numOfReadings = 0; double temperatures[4], windSpeeds[4]; string windDirections[4]; bool initialized = false; string str; //Have the user provide a name for the weather station upon entry. cout << "Enter the name of weather station: "; getline(cin, name); //Control loop to perform various actions. while (true) { cout << "1. Input a complete weather reading." << " "; cout << "2. Print the current weather." << " "; cout << "3. Print the weather history (from most recent to oldest)." << " "; cout << "4. Exit the program." << " "; cout << "Enter your choice: "; cin >> str; if (str.length() != 1 || str < "1" || str > "4") choice = 0; else choice = atoi(str.c_str()); //Switch based on choice. switch (choice) { case 1: moveTemperaturesToRight(temperatures, windSpeeds, windDirections); cout << "Enter the temperature:"; cin >> temperatures[0]; //get correct wind speed do { cout << "Enter the wind speed (a value >=0):"; cin >> windSpeeds[0]; } while (windSpeeds[0] < 0); //get correct wind direction do { cout << "Enter the wind direction (North,South,East or West):"; cin >> windDirections[0]; } while (windDirections[0] != "North" && windDirections[0] != "South" && windDirections[0] != "East" && windDirections[0] != "West"); initialized = true; if (initialized) numOfReadings++; if (numOfReadings > 4) numOfReadings = 4; break; case 3: //Print the current weather, if valid weather is entered. for (int i = 0; i < numOfReadings; i++) { cout << "*****" << name << "*****" << " "; cout << "Temperature: " << temperatures[i] << " "; cout << "Wind speed: " << windSpeeds[i] << " "; cout << "Wind direction: " << windDirections[i] << " " << " "; } if (numOfReadings == 0) cout << "Please enter the details before asking to print." << " "; break; case 2: if (numOfReadings == 0) { cout << "Please enter the details before asking to print." << " "; break; } cout << "*****" << name << "*****" << " "; cout << "Temperature: " << temperatures[0] << " "; cout << "Wind speed: " << windSpeeds[0] << " "; cout << "Wind direction: " << windDirections[0] << " " << " "; break; case 4: return 0; //Stops execution. default: cout << "Invalid choice. Please follow the menu." << " "; } } }