Question
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
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.
(Lab2b.cpp) Write a program to process structured data stored in a 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)
servingSize (double)
calFromCarb (double)
calFromFat (double)
calFromProtein (double)
totalCalories (double)
Use the data types in parentheses for the fields.
Create File
Create a binary file named nutri.dat. The file name should be defined in a named string constant. The string constant identifier should be used every time the file name is needed in the program.
If the file already exists, print the message, "The file nutri.dat is an existing file. You can either delete the file or move it to another location and then run the program again." The program should terminate after printing out the message.
Write five records of structured data to the file in binary format. Each record of data contains these fields: food name, serving size in grams, calories from carb, calories from fat and calories from protein.
Use 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
Close the file.
Read Data From File
Open the file and access the third record from the file. The program should use the seek mechanism to access the desired record.
Read the third record.
Process the record by calculating the calories per serving.
Print the third record to the screen.
Close the file.
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
Submit Lab2b.cpp
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started