C++ programming computer science code exercise question: Use this given code and
ID: 3565703 • Letter: C
Question
C++ programming computer science code exercise question:
Use this given code and transform it to add new 3 functions:
//void displayStart();
//float calculateFatCalPercent(int g, int c);
//void displayResults(string n, int gr, int ca, float fac);
/* Your int main function will look like this:
int main ()
{
string foodItem;
int gramsOfFat;
int calories;
float fatCalPercent;
displayStart();
cout << "Enter the name of a food item."
<< " Press return." << endl;
getline(cin, foodItem);
cout << "Enter the grams of fat; press return." << endl;
cin >> gramsOfFat;
cout << "Enter the number of calories; press return."
<< endl;
cin >> calories;
fatCalPercent = calculateFatCalPercent(gramsOfFat, calories);
displayResults(foodItem, gramsOfFat, calories, fatCalPercent);
return 0;
}
This functions will perform the rest of the operations to get it to work.
See sample output at the bottom of this code
*/
//Original program (start here)
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main ()
{
string foodItem;
int gramsOfFat;
int calories;
float fatCalPercent;
cout << "Enter the name of a food item."
<< " Press return." << endl;
getline(cin, foodItem);
cout << "Enter the grams of fat; press return." << endl;
cin >> gramsOfFat;
cout << "Enter the number of calories; press return."
<< endl;
cin >> calories;
fatCalPercent = gramsOfFat * 9;
fatCalPercent = fatCalPercent / calories;
fatCalPercent = fatCalPercent * 100;
//Printer headings
cout << setw(4) << "Item" << setw(5) << "Grams of Fat" << setw(5) << "Calories" << setw(5) << "Percent from Fat" << endl;
//Print table values:
cout << "--------------------------------------------------------------------"<< endl;
cout << setw(4) << foodItem << setw(5) << gramsOfFat << setw(5) << calories << setw(15)
<< fatCalPercent << endl;
return 0;
}
/******* Use the following data to find logic errors - and correct
Chicken 1 6 150
Tuna 2 170 10.5882
Corned Beef 7 200 31.5
V8 0 35 0
**********************************************************************/
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void displayStart()
{
cout<<"Calculate Fat Calorise Percentage "<<endl;
}
float calculateFatCalPercent(int gramsOfFat, int calories)
{
float fatCalPercent;
fatCalPercent = gramsOfFat * 9;
fatCalPercent = fatCalPercent / (calories*(1.0));
fatCalPercent = fatCalPercent * 100;
return fatCalPercent;
}
void displayResults(string foodItem, int gramsOfFat, int calories, float fatCalPercent)
{
cout << setw(4) << "Item" << setw(5) << "Grams of Fat" << setw(5) << "Calories" << setw(5) << "Percent from Fat" << endl;
//Print table values:
cout << "--------------------------------------------------------------------"<< endl;
cout << setw(4) << foodItem << setw(5) << gramsOfFat << setw(5) << calories << setw(15)<< fatCalPercent << endl;
}
int main ()
{
string foodItem;
int gramsOfFat;
int calories;
float fatCalPercent;
displayStart();
cout << "Enter the name of a food item."
<< " Press return." << endl;
getline(cin, foodItem);
cout << "Enter the grams of fat; press return." << endl;
cin >> gramsOfFat;
cout << "Enter the number of calories; press return."
<< endl;
cin >> calories;
fatCalPercent = calculateFatCalPercent(gramsOfFat, calories);
displayResults(foodItem, gramsOfFat, calories, fatCalPercent);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.