Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use c++ and this File: #include #include #include using namespace std; //Define a data structure named NutritionData const int NAME_SIZE = 40; struct NutritionData {

image text in transcribed
image text in transcribed
Use c++ and this File:
#include
#include
#include
using namespace std;
//Define a data structure named NutritionData
const int NAME_SIZE = 40;
struct NutritionData {
char foodName[NAME_SIZE];
double servingSize;
double calFromCarb;
double calFromFat;
double calFromProtein;
};
bool retrieveNutritionData(const string &fileName,
NutritionData *dataPtr,
int recordNum);
void printNutritionData(const NutritionData *dataPtr);
int main() {
string filename = "nutri.dat";
// Write your code here . . .
return 0;
}
bool retrieveNutritionData(const string &fileName,
NutritionData *dataPtr,
int recordNum) {
bool result {false};
// Write your code here . . .
return result;
}
//***********************************************************************
//* Definition of the function that can print a nutrition data.
//* The function calculates the total calories per serving by
//* adding the calories from three nutrition components.
void printNutritionData(const NutritionData *dataPtr) {
// Write you code here . . .
}
This lab is to write some code so that records stored in a binary file can be retrieved for processing. Eoch record occupies the same amount of space in the file A template has been provided to you for completion The template has provided the following contents. Data Structure Definition A data structure named NutritionDota thot contains these fields foodName (array of 40 char), use on all uppercase integer constant identifier for the size. serving Size (double) colFromCarb (double) colFromFat (double) colFromProtein (double) Function Prototypes The prototype of the function that retrieve certain Nutrition Deto record from a binary file. . The prototype of the function that prints a NutritionData record. Main Function You need to complete the main function as follows: I call the function that can retrieve a nutrition dato record to retrieve the third record from the file nutrida 3. If the call was successful coll the print function to display the retrieved record. Otherwise, print "The desired structure cannot be extracted. Definition of the function that con retrieve a nutrition date The function has the following parameters A parameter for the file name. This is a const string reference parameter. A parameter for returning the retrieved NutritionData. A parameter for the desired structure. It must be a positive integer. It the value las 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. The function returns true if the desired record has been successfully retrieved from the file, otherwise it returns talse. You need to complete the body of the function as follows. 1. Open the file as an input binary file using the file name parameter. 2. If the file desty't exist, print the message. The file filename cannot be opened." Replace the word filename with the value of the file name porometer. 3. If the fie 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 4. If the seek is successtul, read the desired record and store that in the cailor's NutritionDato variabic through the pointer parameter. Close the file Note that the function should return true only in the file has been opened successfully and the desired record has been retrieved and stored in the paramuter. 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 You need to complete the print function to print a Nutritionate information in the expected format 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 This lab is to write some code so that records stored in a binary file can be retrieved for processing. Eoch record occupies the same amount of space in the file A template has been provided to you for completion The template has provided the following contents. Data Structure Definition A data structure named NutritionDota thot contains these fields foodName (array of 40 char), use on all uppercase integer constant identifier for the size. serving Size (double) colFromCarb (double) colFromFat (double) colFromProtein (double) Function Prototypes The prototype of the function that retrieve certain Nutrition Deto record from a binary file. . The prototype of the function that prints a NutritionData record. Main Function You need to complete the main function as follows: I call the function that can retrieve a nutrition dato record to retrieve the third record from the file nutrida 3. If the call was successful coll the print function to display the retrieved record. Otherwise, print "The desired structure cannot be extracted. Definition of the function that con retrieve a nutrition date The function has the following parameters A parameter for the file name. This is a const string reference parameter. A parameter for returning the retrieved NutritionData. A parameter for the desired structure. It must be a positive integer. It the value las 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. The function returns true if the desired record has been successfully retrieved from the file, otherwise it returns talse. You need to complete the body of the function as follows. 1. Open the file as an input binary file using the file name parameter. 2. If the file desty't exist, print the message. The file filename cannot be opened." Replace the word filename with the value of the file name porometer. 3. If the fie 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 4. If the seek is successtul, read the desired record and store that in the cailor's NutritionDato variabic through the pointer parameter. Close the file Note that the function should return true only in the file has been opened successfully and the desired record has been retrieved and stored in the paramuter. 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 You need to complete the print function to print a Nutritionate information in the expected format 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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

2. How economists distinguish between nominal GDP and real GDP.

Answered: 1 week ago