I finished this program with two logic errors that I can't figure out.
The first one is why the doAgain variable isn't working. It should work since the variable is outside the loops. It worked with my other program fine, so I don't know what occurred wrong now.
The second logic error is the fact that whether the food is low fat executes even if the number of calories isn't valid.
I put this outside all the do while loops so the program would have to exit all the do while loops (making sure the values were correct) before it reached calculating if the food is low fat.
The third do while loop didn't execute until the second one (concerning the fat grams) executed correctly. I believe I setup the calculate if the food is low fat equation the same, but outside the loops. I'm not sure why it displays even if the calories aren't valid.
#include <iostream>
#include <stdlib.h>
#include <string>
usingnamespace std;
bool fatGramValidation(double fatGrams);
void calValidation1 (double &fatGrams, double &amount);
bool calValidation2 (double calories);
void perOfFat (double &fatGrams, double &calories, double &caloriesPercentage);
constint NUM_FOR_FAT_GRAM_CAL_AND_CALORIE_CAL=0;
constint NUM_FOR_CALORIE_CAL_AND_PER_OF_FAT_CAL=9;
constdouble NUM_FOR_CAL_PER=0.3;
int main()
{
double fatGrams=0;
double calories=0;
double amount=0;
bool status1;
bool status2;
double caloriesPercentage=0;
string doAgain;
cout << "We are going to be calculating the number of far grams and calories in a food item." << endl;
cout << "We are also going to be calculating whether that food is low fat or not." << endl;
do
{
do
{
cout << "Please input the number of fat grams." << endl;
cin >> fatGrams;
cout << "You put " << fatGrams << " amount of fat grams." << endl;
status1=fatGramValidation(fatGrams);
if (status1==true)
cout << "The number of fat grams you entered were valid." << endl;
else
cout << "The number of fat grams you entered weren't valid. It has to be more than 0." << endl;
}
while(status1==false);
do
{
cout << "Please input the number of calories in the food you're eating." << endl;
cin >> calories;
cout << "You put " << calories << " amount of calories." << endl;
calValidation1(fatGrams, amount);
status2=calValidation2(calories);
if (calories<amount && calories>NUM_FOR_FAT_GRAM_CAL_AND_CALORIE_CAL)
cout << "You entered a valid number of calories." << endl;
else
cout << "You didn't enter a valid number of calories. This is either because it's less than zero or because it exceeds" << endl;
cout << "fat grams * nine." << endl;
}
while(calories>amount && status2==false);
perOfFat(fatGrams, calories, caloriesPercentage);
if (caloriesPercentage<NUM_FOR_CAL_PER)
cout << "This food is low fat." << endl;
else
cout << "This food is not low fat." << endl;
cout <<"Would you like to go through the fat gram calculator again? If so, please press yes." << endl;
cin >> doAgain;
}
while(doAgain=="yes" || "YES" || "Yes");
system("Pause");
}
bool fatGramValidation(double fatGrams)
{
if (fatGrams>NUM_FOR_FAT_GRAM_CAL_AND_CALORIE_CAL)
returntrue;
elsereturnfalse;
}
void calValidation1(double &fatGrams, double &amount)
{
amount=(fatGrams*NUM_FOR_CALORIE_CAL_AND_PER_OF_FAT_CAL);
cout << "The amount is " << amount << endl;
}
bool calValidation2(double calories)
{
if (calories>NUM_FOR_FAT_GRAM_CAL_AND_CALORIE_CAL)
returntrue;
elsereturnfalse;
}
void perOfFat(double &fatGrames, double &calories, double &caloriesPercentage)
{
caloriesPercentage=((fatGrames*NUM_FOR_CALORIE_CAL_AND_PER_OF_FAT_CAL)/calories);
}
#include <iostream>
#include <stdlib.h>
#include <string>
usingnamespace std;
bool fatGramValidation(double fatGrams);
void calValidation1 (double &fatGrams, double &amount);
bool calValidation2 (double calories);
void perOfFat (double &fatGrams, double &calories, double &caloriesPercentage);
constint NUM_FOR_FAT_GRAM_CAL_AND_CALORIE_CAL=0;
constint NUM_FOR_CALORIE_CAL_AND_PER_OF_FAT_CAL=9;
constdouble NUM_FOR_CAL_PER=0.3;
int main()
{
double fatGrams=0;
double calories=0;
double amount=0;
bool status1;
bool status2;
double caloriesPercentage=0;
string doAgain;
cout << "We are going to be calculating the number of far grams and calories in a food item." << endl;
cout << "We are also going to be calculating whether that food is low fat or not." << endl;
do
{
do
{
cout << "Please enter a number of fat grams that's more than zero." << endl;
cin >> fatGrams;
cout << "You put " << fatGrams << " amount of fat grams." << endl;
status1=fatGramValidation(fatGrams);
if (status1==true)
cout << "The number of fat grams you entered were valid." << endl;
else
cout << "The number of fat grams you entered weren't valid. It has to be more than 0." << endl;
}
while(status1==false);
do
{
cout << "Please input the number of calories in the food you're eating." << endl;
cin >> calories;
cout << "You put " << calories << " amount of calories." << endl;
calValidation1(fatGrams, amount);
status2=calValidation2(calories);
if (calories>amount && calories>NUM_FOR_FAT_GRAM_CAL_AND_CALORIE_CAL)
cout << "You entered a valid number of calories." << endl;
else
cout << "You didn't enter a valid number of calories. This is either because it's less than zero or because it doesn't exceed" << endl;
cout << "fat grams * nine." << endl;
}
while(calories<amount && status2==false);
perOfFat(fatGrams, calories, caloriesPercentage);
if (caloriesPercentage<NUM_FOR_CAL_PER)
cout << "This food is low fat." << endl;
else
cout << "This food is not low fat." << endl;
cout <<"Would you like to go through the fat gram calculator again? If so, please press yes." << endl;
cin >> doAgain;
}
while(doAgain=="yes" || "YES" || "Yes");
system("Pause");
}
bool fatGramValidation(double fatGrams)
{
if (fatGrams>NUM_FOR_FAT_GRAM_CAL_AND_CALORIE_CAL)
returntrue;
elsereturnfalse;
}
void calValidation1(double &fatGrams, double &amount)
{
amount=(fatGrams*NUM_FOR_CALORIE_CAL_AND_PER_OF_FAT_CAL);
cout << "The amount is " << amount << endl;
}
bool calValidation2(double calories)
{
if (calories>NUM_FOR_FAT_GRAM_CAL_AND_CALORIE_CAL)
returntrue;
elsereturnfalse;
}
void perOfFat(double &fatGrames, double &calories, double &caloriesPercentage)
{
caloriesPercentage=((fatGrames*NUM_FOR_CALORIE_CAL_AND_PER_OF_FAT_CAL)/calories);
}
{cout << "You didn't enter a valid number of calories." << endl;
cout << "This is either because it's less than zero or because it doesn't exceed" << endl;
cout << "fat grams * nine." << endl;}
I forgot after more than one statement it needs brackets.
This stopped my program from executing the print statements when the calorie input was valid.
I believe I fixed most of my issues, actually. I may be wrong, haha.
I found the bracket problem which fixed the weird sentence execution, and I think where you wanted me to change && to || is supposed to stay &&.
I put the if-then statement into a nested if-then statement, and now it only executed when the calorie loop has be exited (which means when it is true).
You helped me with the string variable.
1 2 3 4 5
if (calories>amount && status2==true)
if (caloriesPercentage<NUM_FOR_CAL_PER)
cout << "This food is low fat." << endl;
else
cout << "This food is not low fat." << endl;
My program is executing correctly (I believe). This is my final code.
#include <iostream>
#include <stdlib.h>
#include <string>
usingnamespace std;
bool fatGramValidation(double fatGrams);
void calValidation1 (double &fatGrams, double &amount);
bool calValidation2 (double calories);
void perOfFat (double &fatGrams, double &calories, double &caloriesPercentage);
constint NUM_FOR_FAT_GRAM_CAL_AND_CALORIE_CAL=0;
constint NUM_FOR_CALORIE_CAL_AND_PER_OF_FAT_CAL=9;
constdouble NUM_FOR_CAL_PER=0.3;
int main()
{
double fatGrams=0;
double calories=0;
double amount=0;
bool status1;
bool status2;
double caloriesPercentage=0;
string doAgain;
cout << "We are going to be calculating the number of far grams and calories in a food item." << endl;
cout << "We are also going to be calculating whether that food is low fat or not." << endl;
do
{
do
{
cout << "Please enter a number of fat grams that's more than zero." << endl;
cin >> fatGrams;
cout << "You put " << fatGrams << " amount of fat grams." << endl;
status1=fatGramValidation(fatGrams);
if (status1==true)
cout << "The number of fat grams you entered were valid." << endl;
else
cout << "The number of fat grams you entered weren't valid. It has to be more than 0." << endl;
}
while(status1==false);
do
{
cout << "Please input the number of calories in the food you're eating." << endl;
cin >> calories;
cout << "You put " << calories << " amount of calories." << endl;
calValidation1(fatGrams, amount);
status2=calValidation2(calories);
if (calories>amount && calories>NUM_FOR_FAT_GRAM_CAL_AND_CALORIE_CAL)
cout << "You entered a valid number of calories." << endl;
else
{cout << "You didn't enter a valid number of calories." << endl;
cout << "This is either because it's less than zero or because it doesn't exceed" << endl;
cout << "fat grams * nine." << endl;}
}
while(calories<amount && status2==false);
perOfFat(fatGrams, calories, caloriesPercentage);
if (calories>amount && status2==true)
if (caloriesPercentage<NUM_FOR_CAL_PER)
cout << "This food is low fat." << endl;
else
cout << "This food is not low fat." << endl;
cout <<"Would you like to go through the fat gram calculator again? If so, please press yes." << endl;
cin >> doAgain;
}
while(doAgain=="yes" || doAgain == "YES" || doAgain =="Yes");while(doAgain=="yes" || doAgain == "YES" || doAgain =="Yes")
system("Pause");
}
bool fatGramValidation(double fatGrams)
{
if (fatGrams>NUM_FOR_FAT_GRAM_CAL_AND_CALORIE_CAL)
returntrue;
elsereturnfalse;
}
void calValidation1(double &fatGrams, double &amount)
{
amount=(fatGrams*NUM_FOR_CALORIE_CAL_AND_PER_OF_FAT_CAL);
cout << "The amount is " << amount << endl;
}
bool calValidation2(double calories)
{
if (calories>NUM_FOR_FAT_GRAM_CAL_AND_CALORIE_CAL)
returntrue;
elsereturnfalse;
}
void perOfFat(double &fatGrames, double &calories, double &caloriesPercentage)
{
calorjavascript:editbox3.editSend()iesPercentage=((fatGrames*NUM_FOR_CALORIE_CAL_AND_PER_OF_FAT_CAL)/calories);
}
Can you explain why I had to put a reference variable for fatGrams here to get my program to work? I feel like it should've worked even without the reference passing symbol (&), but it didn't until it put it there. Though, other variables that were just passed by value worked for similar reasons.
I don't see why that is a problem - you don't change the value of it inside the function. Just on that fatGrams should be const. What problems did you encounter?
You do need it for the amount variable though, so that value is available in the outer scope.
void calValidation1(double &fatGrams, double &amount)
{
amount=(fatGrams*NUM_FOR_CALORIE_CAL_AND_PER_OF_FAT_CAL);
cout << "The amount according to the nutritional formula is " << amount << endl;
cout << "This amount allows us to figure out if your calories are valid or not." << endl;
}//end calValidation1 function
When it showed the amount in the cout statement, it would simply show NUM_FOR_CALORIE_CAL_AND_PER_OF_FAT_CAL or (9) to simplify.