Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

client code given CalorieCounter.cpp and an example input file drCsSpecialBreadI

ID: 3830054 • Letter: C

Question

client code given CalorieCounter.cpp and an example input file drCsSpecialBreadIngredients.tab. create Ingredient.cpp and Ingredient.h so that the client code complies and produces the output that exactly matches that in drCsSpecialBreadIngredients-stdout.txt.

Your program needs to be able to compile with the following line:

g++ CalorieCounter.cpp Ingredient.cpp

CalorieCounter.cpp

#include <iostream>

#include <fstream>

#include <string>

#include "Ingredient.h"

using namespace std;

const int MAX_NUM_INGREDIENTS = 100; // maximum number of ingredients

const string DEFAULT_INPUT_FILENAME = "drCsSpecialBreadIngredients.tab";

int main( ){

ifstream dataFS; // input file stream

Ingredient ingredients[ MAX_NUM_INGREDIENTS ]; // array of all ingredients read in from the file

int numberOfIngredients = 0; // counter

string name; // temp variable

int calories, grams; // temp variable

int totalCalories = 0, totalGrams = 0; // aggregates

// If just a return (' ') is entered, then use DEFAULT_INPUT_FILENAME.

// Otherwise, read in the input filename from STDIN.

string inputFilename;

cout << "Please enter the input filename (or simply press return to use " << DEFAULT_INPUT_FILENAME << ") ";

getline( cin, inputFilename);

if( inputFilename == ""){

inputFilename = DEFAULT_INPUT_FILENAME;

}

// open file specified on the command-line

dataFS.open( inputFilename.c_str() );

  

// verify that the file opened successfully

if( ! dataFS){

cerr << "Error opening "" << inputFilename << ""! ";

return 1;

}

// read in data from the file

cout << "Reading ingredients from " << inputFilename << " . . . ";

numberOfIngredients = 0;

  

while( dataFS >> name >> calories >> grams ){

  

ingredients[ numberOfIngredients ].setName( name );

ingredients[ numberOfIngredients ].setCalories( calories );

ingredients[ numberOfIngredients ].setGrams( grams );

totalCalories += calories;

totalGrams += grams;

  

numberOfIngredients++;

}

dataFS.close();

for( int i = 0; i < numberOfIngredients; i++){

ingredients[ i ].print();

}

  

Ingredients dish( "Total", totalCalories, totalGrams );

dish.print();

  

return 0;

}

drCsSpecialBreadIngredients.tab

honey   683   227

oil   964   99

salt   0   26

water   0   1419

wheat   7862   2160

yeast   117   26

drCsSpecialBreadIngredients-stdout.txt. (output)

Reading ingredients from drCsSpecialBreadIngredients.tab . . .

honey has 683 calories and weighs 227 grams

oil has 964 calories and weighs 99 grams

salt has 0 calories and weighs 26 grams

water has 0 calories and weighs 1419 grams

wheat has 7862 calories and weighs 2160 grams

yeast has 117 calories and weighs 26 grams

Total has 9626 calories and weighs 3957 grams

Explanation / Answer

// Ingredient.h

#include<iostream>
using namespace std;
class Ingredient
{
   private:
       string _str;
       int _calories;
       int _grams;
   public:
       Ingredients(string str, int totalCal, int totalGram);
       ~Ingredients();
       void setName(string str);
       void setCalories(int calories);
       void setGrams(int grams);
       void print();
};

// Ingredient.cpp

#include "Ingredient.h"


Ingredients::Ingredients(string str, int totalCal, int totalGram)
{
   _str = str;
   _calories = totalCal;
   _grams = totalGram;
}

Ingredients::~Ingredients()
{}

void setName(string str)
{
   _str = str;
}

void setCalories(int calories)
{
   _calories = calories;
}

void setGrams(int grams)
{
   _grams = grams;
}

void print()
{
   cout<<_str<<" has "<<_calories<<" calories and weighs "<< _grams <<" grams"<<endl;

}