upload as Source File) write a program which reads in information about a person
ID: 3606844 • Letter: U
Question
upload as Source File) write a program which reads in information about a person's diet from a file assume that there is only one food item in the diet and displays the information to the console screen. The program shall be able to compute the total number of calories that the int is the food ites total lection Your program shall do the following Functions and structures: 1) Create a structure that contains 3 variables to represent attributes of the food item. Specifically, a string Name: name of the food b) it Ouantityll number of units consumed C) Int UniCaloriesI calories per unit rigure 1 example of Menu 4. Prior to exiting the program, use the writekile function to save any changes made to the food list Detect if the writerve function has encountered an error, if it has print an error to the console 2) Create a function nemed readie which takes a reference to the structure and a string contains filename) as parameters, the function shall read and parse the file and store the information into the attributes. It can be assumed the name of the food item will always be one word (hintgetline may not be the optimal method), if the function fails to read the file, the function shall return false. otherwise the function shall return true Hile format (Please create it by yourself); food name quantity calories where the fields food name, quantity, and calories will be a single word as shown in Figure 2. 3) Create a function named printtoods which takes the structure as parameter, the function shall print the contents of the item, name, quantity, and calories, to the console. There shall be nothing returned from this function . A ty in EEE is that 4) Create a function named coloniecounter which takes the Guantity and Unurcalories as parameters. This function shall compute the total number of calories consumed by multiplying the quantity with the Unit alone, the function shall then return the computed calories, there shall be no sin/ cout in this function s) Create a function named modify and which takes a references to the structure as the parameter, The function shall prompt the user to enter in the new name, quantity and unit calories of the food Item, there shall be no return 6) Create a function named write title which takes the structure as the parameter. The function shall write the food item into the specified file using the specified formatif the function is successful retum back true, otherwise return back false. Man Function 1. Declare an instance of the struture to store three attributes of the food item . 2 open the file using the recorie function detect if the readie function has encountered an errorif it has, print an error message and ask user to select "modify" to manually input the data instead of quitting 3. Display a menu similar to the one listed in figure and use the functions above to implement each of the menu items.Explanation / Answer
Given below is the code and output. Please don't forget to rate the answer if it helped Thank you
#include <iostream>
#include <fstream>
using namespace std;
struct Food
{
string Name;
int Quantity;
int UnitCalories;
};
bool readFile(string filename, Food &f);
void printFood(Food f);
int calorieCounter(int qty, int unitCal);
void modifyFood(Food &f);
bool writeFile(string filename, Food f);
int main()
{
string filename;
Food food;
string ans;
cout << "Enter filename containing food: ";
cin >> filename;
if(!readFile(filename, food))
{
cout << "Error reading input file " << filename << endl;
cout << "Use modify option from menu to set values for food" << endl << endl;
}
while(ans != "exit")
{
cout << "Select one of the following options: " << endl;
cout << "list: To list the food item" << endl;
cout << "modify: To modify the food item" << endl;
cout << "count: To get a total calorie count" << endl;
cout << "exit: To exit the program" <<endl;
cout << "Selection: ";
cin >> ans;
if(ans == "list")
printFood(food);
else if(ans == "modify")
{
modifyFood(food);
cout << "Food added" << endl << endl;
}
else if(ans == "count")
cout << "Number of calories: " << calorieCounter(food.Quantity, food.UnitCalories) << endl << endl;
else if(ans != "exit")
cout << "Invalid selection!" << endl << endl;
}
if(!writeFile(filename, food))
cout << "Error writing to file " << filename << endl;
cout << "Good bye!" << endl;
}
bool writeFile(string filename, Food f)
{
ofstream ofs(filename.c_str());
if(!ofs.is_open())
return false;
else
{
ofs << f.Name << " " << f.Quantity << " " << f.UnitCalories << endl;
ofs.close();
return true;
}
}
void modifyFood(Food &f)
{
cout << "Enter the name of the food: ";
cin >> f.Name;
cout << "Enter the quantity of the food: ";
cin >> f.Quantity;
cout << "Enter the calories in the food: ";
cin >> f.UnitCalories;
}
int calorieCounter(int qty, int unitCal)
{
return qty * unitCal;
}
void printFood(Food f)
{
cout << "Name: " << f.Name << endl;
cout << "Quantity: " << f.Quantity << endl;
cout << "Unit Calories: " << f.UnitCalories << endl;
cout << endl;
}
bool readFile(string filename, Food &f)
{
ifstream ifs(filename.c_str());
if(ifs.is_open())
{
ifs >> f.Name >> f.Quantity >> f.UnitCalories;
ifs.close();
return true;
}
else
return false;
}
inptut file : diet.txt
doritos 2 140
output
Enter filename containing food: diet.txt
Select one of the following options:
list: To list the food item
modify: To modify the food item
count: To get a total calorie count
exit: To exit the program
Selection: list
Name: doritos
Quantity: 2
Unit Calories: 140
Select one of the following options:
list: To list the food item
modify: To modify the food item
count: To get a total calorie count
exit: To exit the program
Selection: count
Number of calories: 280
Select one of the following options:
list: To list the food item
modify: To modify the food item
count: To get a total calorie count
exit: To exit the program
Selection: modify
Enter the name of the food: burger
Enter the quantity of the food: 3
Enter the calories in the food: 150
Food added
Select one of the following options:
list: To list the food item
modify: To modify the food item
count: To get a total calorie count
exit: To exit the program
Selection: list
Name: burger
Quantity: 3
Unit Calories: 150
Select one of the following options:
list: To list the food item
modify: To modify the food item
count: To get a total calorie count
exit: To exit the program
Selection: count
Number of calories: 450
Select one of the following options:
list: To list the food item
modify: To modify the food item
count: To get a total calorie count
exit: To exit the program
Selection: exit
Good bye!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.