Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can you please follow all the instructions ? The answers that I got has either missing range for loop or one funtion . Read the

can you please follow all the instructions ? The answers that I got has either missing range for loop or one funtion .
Read the instructions carefully. At least 10% will be deducted if the instructions are not followed.
For general lab requirements, see General Lab Requirements.
Do not prompt the user for input during the execution of your program. Do not pause at the end of the execution waiting for the user's input.
Notes
1. Your program should use named string constant for the file name to open the file. Do not add any path to the file name because the path on your computer is not likely to exist on the computer the instructor uses to grade the program. Points may be deducted if you don't follow this instruction.
2. Split statements and comments longer than 80 characters into multiple lines and use proper indentations for the split portions.
3. You should not use "break" or "continue" in your code.
4. You should not return from the middle of a function.
5. When you need to cast a variable to a different type, don't use the C-style casting. For example, (char *)ptr will not be accepted.
(Lab2b.cpp) This program has two parts. The first part is to produce a set of nutrition data and save it to a file in binary format. The second part of the program retrieves a certain nutrition data from the binary file.
The program must be written in accordance to the following plan:
Define Data Structure
Define a data structure named NutritionData that contains these fields
foodName (array of 40 char) , use an all uppercase integer constant identifier for the size.
servingSize (double)
calFromCarb (double)
calFromFat (double)
calFromProtein (double)
totalCalories (double)
Use the data types in parentheses for the fields.
Write necessary function prototypes. You should have at least the following two functions:
- A function that can save an array of NutritionData to a binary file. The function returns true if the binary file has been created and the data has been written to the file. Otherwise it returns false. Choose a meaningful identifier in camelCase for the function. For parameters, see the definition description below.
- A function that can retrieve and return a certain nutrition data record from a binary file. The function returns true if the record has been retrieved; otherwise it returns false. Choose a meaningful identifier in camelCase for the function. For parameters, see the definition description below.
Write the main Function
Define a string constant for the file name. The constant identifier should be all uppercase. The file name shall be called "nutrition.dat". The constant identifier should be used every time the file name is needed in the main function.
Define and initialize an array of NutritionData using the following data:
Apples raw, 110, 50.6, 1.2, 1.0
Bananas, 225, 186, 6.2, 8.2
Bread pita whole wheat, 64, 134, 14, 22.6
Broccoli raw, 91, 21.9, 2.8, 6.3
Carrots raw, 128, 46.6, 2.6, 3.3
Each record of data contains these fields: food name, serving size in grams, calories from carb, calories from fat and calories from protein.
Use a range-based for loop to iterate through the array of NutritionData and update the totalCalories field of each nutrition data. A range-based for loop that can update the array element values has a syntax like this:
for (auto &element: arrayName) {
}
Call the function that can save the array of NutritionData to "nutrition.dat".
If the call was successful, call the function that can retrieve a nutrition data record to retrieve the third record from "nutrition.dat".
If the call was successful, display the retrieved record. Otherwise, print "The desired structure cannot be extracted."
Write Function Definitions
- For the function that can save an array of NutritionData to a binary file.
The function should have the following parameters:
A parameter for the file name. This can be a const string reference parameter.
A pointer parameter that points to a NutritionData which is the first element in an array of NutritionData.
A parameter for the number of NutritionData in the array.
Parameters should be in camelCase. The function returns true if the array of NutritionData has been successfully save to the file, otherwise it returns false.
If the file already exists, print the message, "The file filename is an existing file. You can either delete the file or move it to another location and then run the program again." Replace the word filename with the value of the file name parameter.
If the file doesn't exist already, create a binary file for output using the file name parameter.
Write the array of NutritionData passed in through parameters to the file in binary format.
Close the file.
The function returns true if the file has been opened successfully and all records have been written to the file. Otherwise, it should return false. Therefore, you need to have code that tests the status of the file for success or error at each file operation.
- For the function that can retrieve a nutrition data.
The function should have the following parameters:
A parameter for the file name. This can be a const string reference parameter.
A reference parameter for returning the retrieved NutritionData.
A parameter for the desired structure. It must be a positive integer. If the value is 1, the function will attempt to return the first structure on file. If the value is 2, the function will attempt to return the second structure on file. And so on.
Parameters should be in camelCase. The function returns true if the desired record has been successfully retrieved from the file, otherwise it returns false.
Open the file as an input binary file using the file name parameter.
If the file doesn't exist, print the message, "The file filename cannot be opened." Replace the word filename with the value of the file name parameter.
If the file exists, access the desired record from the file. The program should use the seek mechanism to access the desired record. Note that the first record is at offset 0, the second record is at the offset that equals the size of a record, etc.
Read the desired record and store that in the passed NutritionData parameter.
Close the file.
The function returns true if the file has been opened successfully and the desired record has been retrieved and stored in the parameter. Otherwise, it should return false. Therefore, you need to have code that tests the status of the file for success or error at each file operation.
Test The Program
The output should look exactly as follows:
Food Name: Bread pita whole wheat
Serving Size: 64.0 grams
Calories Per Serving: 170.6
Calories From Carb: 134.0
Calories From Fat: 14.0
Calories From Protein: 22.6

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions