print the values using the function defined above for (int i = 0; i < counter; i++) { printNutritionData(nutritions[i]); } return 0;

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Can you fix the code to out put the same as the picture.

// #include <iostream>
// #include <string>
#include <bits/stdc++.h>
using namespace std;


class NutritionData {
private:
//to store the food name
string foodName;
//to store the serving size of a food type
int servingSize;
//to store amount of cal coming from carbs in the food type
double calFromCarb;
//to store amount of cal coming from Fat in the food type
double calFromFat;
//to store amount of cal coming from protien in the food type
double calFromProtien;
public:
//default constructor
NutritionData();
//mutator for food name
void setFoodName(string foodName);
//mutator for servingSize
void setServingSize(int servingSize);
//mutator for calFromCarb
void setCalFromCarb(double calFromCarb);
//mutator for calFromFat
void setCalFromFat(double calFromFat);
// mutator for calFromProtein
void setCalFromProtien(double calFromProtien);

//accessor for food name
string getFoodName();

//accessor for foodserving size
int getServingSize();

//accessor for calFromCarb
double getCalFromCarb();
//accessor for calFromFat
double getCalFromFat();
//accessor for calFromProtien
double getCalFromProtien();
//get calorie per serving calculates the calories in one serving of the food
double getCaloriesPerServing();
};

//get calorie per serving
double NutritionData::getCaloriesPerServing() {
return (calFromCarb + calFromFat + calFromProtien);
}

//default constructor
NutritionData::NutritionData() {
foodName = "";
servingSize = 0;
calFromCarb = 0.0;
calFromFat = 0.0;
calFromProtien = 0.0;
}
//mutator for food name
void NutritionData::setFoodName(string foodName) {
this->foodName = foodName;
}
//mutator for servingSize
void NutritionData::setServingSize(int servingSize) {
this->servingSize = servingSize;
}

//mutator for calFromCarb
void NutritionData::setCalFromCarb(double calFromCarb) {
this->calFromCarb = calFromCarb;
}
//mutator for calFromFat
void NutritionData::setCalFromFat(double calFromFat) {
this->calFromFat = calFromFat;
}
//mutator for calFromProtien
void NutritionData::setCalFromProtien(double calFromProtien) {
this->calFromProtien = calFromProtien;
}
//accessor for food name
string NutritionData::getFoodName() {
return foodName;
}
//accessor for servingSize
int NutritionData::getServingSize() {
return servingSize;
}
//accessor for calFromCarb
double NutritionData::getCalFromCarb() {
return calFromCarb;
}
//accessor for calFromFat
double NutritionData::getCalFromFat() {
return calFromFat;
}
//accessor for calFromProtien
double NutritionData::getCalFromProtien() {
return calFromProtien;
}

//prniting the nutrition data of a food type
void printNutritionData( NutritionData &n) {
cout << "\nFood Name: " << n.getFoodName() << endl;
cout << " Serving Size: " << n.getServingSize() << " grams\n";
cout << " Calories Per Serving: " << n.getCaloriesPerServing() << endl;
cout << " Calories From Carb: " << n.getCalFromCarb() << endl;
cout << " Calories From Fat: " << n.getCalFromFat() << endl;
cout << " Calories From Protien: " << n.getCalFromProtien() << endl;
}
int main() {
// read the values from a text file
// save the text file in the same directory as "nutri.txt"
ifstream input("nutri.txt");

// stores the total number of inputs
int N;

// create an array of class type NutritionData objects
NutritionData nutritions[100];

// counter for the array
int counter = 0;

// bool to check for first
bool first = true;

// first line contains the number of inputs
for (string line; getline(input, line);) {

if (first) {
first = false;
continue;
}

// stores the four cal values
// 0: serving size, 1:cal carb, 2: cal fat, 3: protein
double calValues[4] = {0};

int calCounter = 0;
bool foundName = false;
// now process the line
string foodName;

string curr = "";

for (int i = 0; i < line.length(); i++) {
if (line[i] == ',') {
if (foundName) {
calValues[calCounter++] = stod(curr);
} else {
foodName = curr;
foundName = true;
}
curr = "";
} else {
curr += line[i];
}
}
calValues[calCounter++] = stod(curr);

// insert the values in the nutritions array
nutritions[counter].setFoodName(foodName);
nutritions[counter].setServingSize(calValues[0]);
nutritions[counter].setCalFromCarb(calValues[1]);
nutritions[counter].setCalFromFat(calValues[2]);
nutritions[counter].setCalFromProtien(calValues[3]);
counter++;
}
// print the values using the function defined above
for (int i = 0; i < counter; i++) {
printNutritionData(nutritions[i]);
}

return 0;
}

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY