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...
Hi I how do i use a vector to search a array for a string that user has enter into the console. here is some of the instructions for the programing I am trying to work on The purpose of this program is to write a class whose job is to read up to 10,000 names into a string array. The class sorts the names and then the user can ask the class to find full names containing a name...
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...
I need help programming this question using C language. This program uses input data entered in command line at the same time when the command or .exe file is entered. They are called command-line arguments. Because everything entered in command line is taken as a string, these arguments including the command itself or the .exe file name are stored in an array of char pointers, each pointer points to the first character of a string (i.e., argument). The inputs can...
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...