Challenge 34

Description

The purpose of this challenge is to use an array of an abstract data type (struct) and save the data to a file. This challenge implements data entry for collection of weather data.

Requirements

  1. Declare a struct called Weather as below:
    struct Weather 
    {
      string city;
      double temperature;
      double humidity;  
    };
    
  2. In main(), create an array using the struct variable called weather_data. This array can contain 10 elements. Remember that the Weather struct act likes a type and you can use it to declare an array of that type.
    Weather weather_data[10];
    
  3. Create a function int collect_data(Weather data[], int size). This function prompts the user to input data for each of the properties of the Weather struct (city, temperature and humidity). During the collection process, if the user enters a city name of stop, do not store this is as the city name and do not ask the user for temperature and humidity. stop is an indicator to stop user input. Using stop as the city name will stop the user input process, even if the array is not yet full. This function will return the number of actual records entered. A record is equivalent to one complete struct data element. While the array has size elements, the user may not necessarily enter data for each element, which is why we return the actual number of records out of the function.
  4. In main(), declare an integer called count. This represents the count of records actually entered by the user.
  5. Create a function void save_data(Weather data[], int count). This function will save the struct data to a file called “weather.txt”. In this file, each record will be stored as city, temperature, humidity each record taking up one line in the file. The count parameter indicates how many elements in the data array actually contain user input. Remember that your collect_data() function returned the number of actual number of records entered.
  6. Your main() function will simply call collect_data() and save_data() to get the user input and to save the data to the file respectively.

DO NOT USE

Classes.

Sample Interaction / Output

City? Seattle
Temperature? 87
Humidity? 100

City? Tampa
Temperature? 100
Humidity? 100

City? stop
2 records entered. Open weather.txt file to see info.

Contents of weather.txt

Seattle, 87, 100
Tampa, 100, 100

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL0004

Print Requirements