The Required code is given below:-
//---------------------------------------------------------------------------------------------------------------------------------------
#include
#include
using namespace std;
struct NutritionData
{
char foodName[40];
double servingSize;
double calFromCarb;
double calFromFat;
double calFromProtein;
double totalCalories;
};
bool storeFoodList(const char*filename,NutritionData*list,int
num);
bool retrieveFood(const char*filename,NutritionData&food,int
n);
int main(int argc,char**argv)
{
bool success=false;
struct NutritionData food;
const char* FILE_NAME = "nutrition.dat";
struct NutritionData foods[5] = { {"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}
};
for(auto &food:foods)
{
food.totalCalories=food.calFromCarb+food.calFromFat+food.calFromProtein;
}
success = storeFoodList(FILE_NAME,foods,5);
if(success)
{
success=retrieveFood(FILE_NAME,food,3);
}
if(success)
{
cout<<"Food Name:
"<
else
{
cout<<"The desired structure
cannot be extracted."<
return 0;
}
bool storeFoodList(const char*filename,NutritionData*list,int
num)
{
ofstream fw;
bool flag=true;
fw.open(filename);
if(fw.fail()) {
cout<<"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."<
}
else
{
// create a new file here
for(int i=0;i
fw.write((char*)&list[i],sizeof(list[i]));
}
}
fw.close();
return flag;
}
bool retrieveFood(const char*filename,NutritionData&food,int
n)
{
ifstream fr;
bool flag=true;
fr.open(filename);
if(fr.fail()) {
cout<<"The file filename
cannot be opened."<
}
else
{
// create a new file here
fr.seekg((n-1)*sizeof(food),ios::beg);
fr.read((char*) &
food,sizeof(food));
}
fr.close();
return flag;
}
can you please follow all the instructions ? The answers that I got has either missing...
c++ program 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....
The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...
C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank you. Use the given function to create the program shown in the sample run. Your program should find the name of the file that is always given after the word filename (see the command line in the sample run), open the file and print to screen the contents. The type of info held in the file is noted by the word after the filename:...
C++ 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...
in C++ and also each function has its own main function so please do the main function as well. Previously when i asked the question the person only did the function and didn't do the main function so please help me do it. 1-1. Write a function that returns the sum of all elements in an int array. The parameters of the function are the array and the number of elements in the array. The function should return 0 if...
I need help with this assignment in C++, please! *** The instructions and programming style details are crucial for this assignment! Goal: Your assignment is to write a C+ program to read in a list of phone call records from a file, and output them in a more user-friendly format to the standard output (cout). In so doing, you will practice using the ifstream class, I'O manipulators, and the string class. File format: Here is an example of a file...
T F a) Void Functions can use reference parameters. T F b) Arguments corresponding to reference parameters can be variables only T F c) The C++ statement retum "hello can be used with an integer value-retuming function T F d) Static variables maintain their value from function call to function call. T F e) The components of a C++ array must be homogeneous T F fA hierarchical structure has at least one component that is itself a structure. T F...
I need help with the following code... Instructions: This lab builds on the skills from Lab 2c, in which you read data values from a file and kept a count of the number of invalid values. In this lab, your program will be using the same code to read each data entry from the file, but you will also save each value in an array named allMags after each is read in. When all values in the file have been...
I am having a little trouble with my Python3 code today, I am not sure what I am doing wrong. Here are the instructions: and here is my code: update: I have seen I did not close x in sumFile and I am still only getting a 2/10 on the grader. any help appreciated. Lab-8 For today's lab you going to write six functions. Each function will perform reading and/or write to a file Note: In zybooks much like on...
Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...