0

I just started programming and now I'm working with input validation. I'm running in to a problem where I made a function to validate input but the second iteration of the loop returns the same value as the priming read. Also, this is for school so tips and advice would be more appreciated than complete answers. I don't want to cheat. The code is incomplete but it looks like this:

#include <iostream>
#include <cmath>
using namespace std;

const int VALID_CALORIES = 9;

double validFatGrams(double fatGrams);
double validCalories(double fatGrams, double calories);

int main()
{
    double fatGrams = 0, calories = 0, percentCalFromFat = 0;

    cout << "Enter the number of fat grams:" << endl;
    cin >> fatGrams;

    validFatGrams(fatGrams);
    fatGrams = validFatGrams(fatGrams);

    cout << "Enter the number of calories:" << endl;
    cin >> calories;

    validCalories(fatGrams, calories);
    calories = validCalories(fatGrams, calories);

    cout << fatGrams << " " << calories << endl;
}

double validFatGrams(double fatGrams)
{
    while(fatGrams < 0)
    {
        cout << "fat" << "  " << fatGrams << endl;
        cout << "Invalid input. The number of fat grams must be greater than 0" << endl;
        cout << "Enter the number of fat grams (greater than 0):" << endl;
        cin >> fatGrams;
    }
    return fatGrams;
}

double validCalories(double fatGrams, double calories)
{
    double passableCalories = 0j;

    passableCalories = fatGrams * VALID_CALORIES;

    while(calories < passableCalories)
    {
        cout << "cal" << "  " << calories << endl;
        cout << "Invalid input. The number of calories must be greater than 0" << endl;
        cout << "and must be greater than fat grams x 9." << endl;
        cout << "Enter a number of calories greater than " << passableCalories << ":" <<endl;
        cin >> calories;
    }
    return calories;
}

Here's an sample of output:

Enter the number of fat grams:
-1
fat  -1
Invalid input. The number of fat grams must be greater than 0
Enter the number of fat grams (greater than 0):
1
fat  -1
Invalid input. The number of fat grams must be greater than 0
Enter the number of fat grams (greater than 0):
1
Enter the number of calories:
2
cal  2
Invalid input. The number of calories must be greater than 0
and must be greater than fat grams x 9.
Enter a number of calories greater than 9:
30
cal  2
Invalid input. The number of calories must be greater than 0
and must be greater than fat grams x 9.
Enter a number of calories greater than 9:
30
1 30
Program ended with exit code: 0

Thanks for any advice

fzerg
  • 11
  • 2
  • I don't understand what you're saying the problem is. – user253751 Jul 05 '16 at 04:17
  • Sorry about that ... I edited the comment to show the weird results I'm seeing. Thanks for pointing that out. – fzerg Jul 05 '16 at 04:25
  • I think you're confusing [pass by value vs pass by reference](http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) – Xeren Narcy Jul 05 '16 at 04:28
  • That fixed it. Thanks for the help! edit: I assumed that the value would not need to be passed by reference because it would always use the same value within the function. If that makes sense. I'm still learning this stuff but I'm really enjoying it. – fzerg Jul 05 '16 at 04:30
  • You're calling the function twice... the first time you enter "1" and then you ignore the result and call the function again, and this time the program uses the number for stuff. – user253751 Jul 05 '16 at 04:31
  • That's probably the more efficient solution. Although passing by reference also fixed it, having the call appear twice was redundant. I thought I had to run the function and declare the result as the new value of the fatGrams variable. Thanks – fzerg Jul 05 '16 at 04:44
  • If you had written `fatGrams = validFatGrams(fatGrams); fatGrams = validFatGrams(fatGrams);` without reference parameter, it would have worked, too. Of course, in either case, reference parameter or my variant, the second function call is obsolete, as it only detects the value already being fine, assured by the first call. My point here is rather to hint once more to the not too uncommon error of forgetting to evaluate the return value of functions... – Aconcagua Jul 05 '16 at 05:23

2 Answers2

0
validFatGrams(fatGrams);
fatGrams = validFatGrams(fatGrams);

Get rid of the first line. As written, the code looks for valid input twice.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • Thanks for that. I thought had to call it first and then initialize a value with the function output. That worked and passing the argument by reference fixed the problem as well. This is the optimal solution. Thank you. – fzerg Jul 05 '16 at 04:41
0

Credit to Xeren Narcy. Passing by reference worked. Also credit to Pete Becker and immibis. Removing the redundant call was the best way to fix the problem. Here's the result if anyone is interested. Thanks for the help. Also, is this the wrong place for me? As a beginner I don't want to waste your time...

#include <iostream>
#include <cmath>
using namespace std;

const int VALID_CALORIES = 9;

double validFatGrams(double fatGrams);
double validCalories(double fatGrams, double calories);

int main()
{
    double fatGrams = 0, calories = 0, percentCalFromFat = 0;

    cout << "Enter the number of fat grams:" << endl;
    cin >> fatGrams;

    fatGrams = validFatGrams(fatGrams);

    cout << "Enter the number of calories:" << endl;
    cin >> calories;

    calories = validCalories(fatGrams, calories);

    cout << fatGrams << " " << calories << endl;
}

double validFatGrams(double fatGrams)
{
    while(fatGrams < 0)
    {
        cout << "fat" << "  " << fatGrams << endl;
        cout << "Invalid input. The number of fat grams must be greater than 0" << endl;
        cout << "Enter the number of fat grams (greater than 0):" << endl;
        cin >> fatGrams;
    }
    return fatGrams;
}

double validCalories(double fatGrams, double calories)
{
    double passableCalories = 0;

    passableCalories = fatGrams * VALID_CALORIES;

    while(calories < passableCalories)
    {
        cout << "cal" << "  " << calories << endl;
        cout << "Invalid input. The number of calories must be greater than 0" << endl;
        cout << "and must be greater than fat grams x 9." << endl;
        cout << "Enter a number of calories greater than " << passableCalories << ":" <<endl;
        cin >> calories;
    }
    return calories;
}

And a sample of fixed output ...

Enter the number of fat grams:
-1
fat  -1
Invalid input. The number of fat grams must be greater than 0
Enter the number of fat grams (greater than 0):
1
Enter the number of calories:
2
cal  2
Invalid input. The number of calories must be greater than 0
and must be greater than fat grams x 9.
Enter a number of calories greater than 9:
3
cal  3
Invalid input. The number of calories must be greater than 0
and must be greater than fat grams x 9.
Enter a number of calories greater than 9:
20
1 20

Thanks again

fzerg
  • 11
  • 2