第一次迭代后,c ++验证函数循环返回相同的结果

时间:2016-07-05 04:12:14

标签: c++

我刚刚开始编程,现在我正在使用输入验证。我遇到了一个问题,我在其中创建了一个函数来验证输入,但循环的第二次迭代返回与启动读取相同的值。此外,这是针对学校的,所以提示和建议将比完整的答案更受赞赏。我不想作弊。代码不完整但看起来像这样:

#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;
}

以下是输出样本:

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

感谢您的任何建议

2 个答案:

答案 0 :(得分:0)

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

摆脱第一线。如上所述,代码会查找两次有效输入。

答案 1 :(得分:0)

归功于Xeren Narcy。通过参考传递工作。同样归功于Pete Becker和immibis。删除冗余呼叫是解决问题的最佳方法。如果有人有兴趣,这就是结果。谢谢您的帮助。另外,这对我来说是错误的地方吗?作为初学者,我不想浪费你的时间......

#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;
}

固定输出样本......

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

再次感谢