Logic errors 2

http://prntscr.com/brs2kd


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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

#include <iostream>
#include <stdlib.h>
#include <string>


using namespace std;

bool fatGramValidation(double fatGrams);
void calValidation1 (double &fatGrams, double &amount);
bool calValidation2 (double calories);
void perOfFat (double &fatGrams, double &calories, double &caloriesPercentage);




const int NUM_FOR_FAT_GRAM_CAL_AND_CALORIE_CAL=0;
const int NUM_FOR_CALORIE_CAL_AND_PER_OF_FAT_CAL=9;
const double 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)
        return true;
    else
        return false;
}

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)
        return true;
    else
        return false;
}

void perOfFat(double &fatGrames, double &calories, double &caloriesPercentage)
{
    caloriesPercentage=((fatGrames*NUM_FOR_CALORIE_CAL_AND_PER_OF_FAT_CAL)/calories);
}
I fond this note on my professors site, and I was a bit confused.

http://prntscr.com/brs67m

Am I supposed to make sure it exceeds 36.0*fatgrams*9.0 or just fatgrams*9.0.

(I'm editing my code since I didn't see his note until I finished)
Firstly, it seems you didn't learn from your previous mistake.

if (calories<amount && || calories>NUM_FOR_FAT_GRAM_CAL_AND_CALORIE_CAL)

http://en.cppreference.com/w/cpp/language/operator_logical
I always get those confused, but isn't it right here?

The program asks me to make sure it's more than 0 and (now in this case) more than amount.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <stdlib.h>
#include <string>


using namespace std;

bool fatGramValidation(double fatGrams);
void calValidation1 (double &fatGrams, double &amount);
bool calValidation2 (double calories);
void perOfFat (double &fatGrams, double &calories, double &caloriesPercentage);




const int NUM_FOR_FAT_GRAM_CAL_AND_CALORIE_CAL=0;
const int NUM_FOR_CALORIE_CAL_AND_PER_OF_FAT_CAL=9;
const double 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)
        return true;
    else
        return false;
}

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)
        return true;
    else
        return false;
}

void perOfFat(double &fatGrames, double &calories, double &caloriesPercentage)
{
    caloriesPercentage=((fatGrames*NUM_FOR_CALORIE_CAL_AND_PER_OF_FAT_CAL)/calories);
}


I finished editing it.

:)



Last edited on
I found one mistake.

1
2
3
 {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.

Last edited on
> The first one is why the doAgain variable isn't working.

while(doAgain=="yes" || "YES" || "Yes")

==>
while(doAgain=="yes" || doAgain == "YES" || doAgain =="Yes")
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

#include <iostream>
#include <stdlib.h>
#include <string>


using namespace std;

bool fatGramValidation(double fatGrams);
void calValidation1 (double &fatGrams, double &amount);
bool calValidation2 (double calories);
void perOfFat (double &fatGrams, double &calories, double &caloriesPercentage);




const int NUM_FOR_FAT_GRAM_CAL_AND_CALORIE_CAL=0;
const int NUM_FOR_CALORIE_CAL_AND_PER_OF_FAT_CAL=9;
const double 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)
        return true;
    else
        return false;
}

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)
        return true;
    else
        return false;
}

void perOfFat(double &fatGrames, double &calories, double &caloriesPercentage)
{
    calorjavascript:editbox3.editSend()iesPercentage=((fatGrames*NUM_FOR_CALORIE_CAL_AND_PER_OF_FAT_CAL)/calories);
}
Last edited on
Glad you got your program running.
Thanks. <3

Oh, one more question.

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.

 
void calValidation1(double &fatGrams, double &amount)
Last edited on
I forgot after more than one statement it needs brackets.


Always put braces for statements in any loop or if statement, even if there is only 1 statement - this will save you one day when you ad more code.

1
2
3
4
5
6
7
8
           if (calories>amount && status2) {
                if (caloriesPercentage<NUM_FOR_CAL_PER) {
                    cout << "This food is low fat." << endl;
                }
                else {
                    cout << "This food is not low fat." << endl;
                }
           }


It's a defensive measure that will help you to avoid problems:+)
Thank you!

Do you have an answer for my reference variable?
Why do you want to know about that?
calValidation1 does not change fatGrams, so there should be no need to pass it by reference.
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.
I'm wondering why it didn't work when I passed it by value

:)
So how would it change the output if you passed it by value instead of by reference?
Last edited on
You're going to need to be explicit about what didn't work when you passed by value.
1
2
3
4
5
6
7
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.

It wouldn't do 9*fat grams without it.
Topic archived. No new replies allowed.