两个相似的功能,不同的结果

时间:2013-01-18 21:30:53

标签: c

我的大脑很痛苦。我无法想象这里发生了什么。如果允许有最佳答案的人,我会付钱。

我有这个学校的体重计划,它允许用户输入起始体重,而不是通过开关,它调用各种功能,允许用户增加体重或减肥。有些函数返回我期望的内容,有些则没有。这些函数返回我期望的cakeGain(),hardWorkout(),mediumWorkout(),但我无法弄清楚为什么我在runLoss(),burgerGain(),pizzaGain()和easyWorkout()上得到错误的返回。我似乎无法弄清楚功能之间的任何重大差异,我尝试了各种选项。我最初通过引用所有函数传递我的权重变量,并让它们更改值,但我的教师不想要所有的指针。

这不是很漂亮的代码,我已经注释了一些我稍后会修复的部分。我知道我的很多工作都是糟糕的形式(系统调用等)但我的导师并没有教我们替代方法或不关心。这仍然被认为是初学者课程。我很好奇的是为什么有些功能可以在其他功能不起作用时运行。

再次感谢,

迈克(我非常感谢所有花时间从他们的日程安排中帮助像我这样的人。我迫不及待地想要帮助其他人帮助他们。)

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#define pause system ("pause")
#define cls system ("cls")
#define flush fflush(stdin)

//prototype functions here
void mainMenu(void);
char getUserChoice();
double enterWeight();
double runLoss(double usersWeight);
double burgerGain(double usersWeight);
double eatPizza(double usersWeight);
double eatCake(double usersWeight);
double hardWorkout(double usersWeight);
double mediumWorkout(double usersWeight);
double easyWorkout(double usersWeight);
//void weightChecker(double usersWeight);

int main() {
    //declare main variables here
    char userChoice = ' ';
    double usersWeight;


    do {
        userChoice = getUserChoice(); 

        switch (userChoice) {
                case'A': //get weight
                usersWeight = enterWeight();
                cls;
                printf("Your starting weight is: %dlbs.\n\n",usersWeight);
                break;
            case'B': //run
                usersWeight = runLoss(usersWeight);
                cls;
                printf("Your weight is now %d.\n", usersWeight);
                //weightChecker(usersWeight);
                break;
            case'C': //eat burger
                usersWeight = burgerGain(usersWeight);
                cls;
                printf("Your weight is now %d.\n", usersWeight);
                //weightChecker(usersWeight);
                break;
            case'D': //eat pizza
                usersWeight = eatPizza(usersWeight);
                cls;
                printf("Your weight is now %d.\n", usersWeight);
                //weightChecker(usersWeight);
                break;
            case'E': //eat cake
                usersWeight = eatCake(usersWeight);
                cls;
                printf("Your weight is now %d.\n", usersWeight);
                //weightChecker(usersWeight);
                break;
            case'F': //hard workout
                usersWeight = hardWorkout(usersWeight);
                cls;
                printf("Your weight is now %d.\n", usersWeight);
                //weightChecker(usersWeight);
                break;
            case'G': //medium workout
                usersWeight = mediumWorkout(usersWeight);
                cls;
                printf("Your weight is now %d.\n", usersWeight);
                //weightChecker(usersWeight);
                break;
            case'H': //easy workout
                usersWeight = easyWorkout(usersWeight);
                cls;
                printf("Your weight is now %d.\n", usersWeight);
                //weightChecker(usersWeight);
                break;
            case'I': //quit
                exit(0);

    }

    } while(userChoice != 'I');

    return 0;

} //end of main



char getUserChoice() {
    char result;
    do {
    mainMenu();
    scanf("%c", &result);
    flush;
    result = toupper (result);


    } while (result < 'A' || result > 'I');

    return result;

}

void mainMenu(void) {
    //cls;
    printf("A) \tEnter starting weight or change weight\n");
    printf("B) \tGo running\n");
    printf("C) \tEat Hamburgers\n");
    printf("D) \tEat pizza\n");
    printf("E) \tEat cake\n");
    printf("F) \tDo a hard workout\n");
    printf("G) \tDo a medium workout\n");
    printf("H) \tDo a easy workout\n");
    printf("I) \tQuit\n\n");
    printf("Enter your selection: ");

}

double enterWeight() {
    double startingWeight = 0.0;
    printf("Please enter your starting weight.\n");
    scanf("%i", &startingWeight);
    flush;

    return startingWeight;
}

double runLoss(double usersWeight) {
    double result;
    double milesRan = 0.0;
    double runMinus = 0.0;
    printf("How many miles did you run?\n");
    scanf("%i", &milesRan);
    flush;
    runMinus = milesRan * .005;
    result = usersWeight - runMinus;

    return result;
}

double burgerGain(double usersWeight) {
    double result;
    double hamburgersAte = 0.0;
    double hamburgerGain = 0.0;
    printf("How many hamburgers did you eat?\n");
    scanf("%i", &hamburgersAte);
    flush;
    hamburgerGain = hamburgersAte * .005;

    if (hamburgersAte > 2) {
        hamburgerGain = hamburgerGain + .5;
    }

    result = usersWeight + hamburgerGain;

    return result;
}

double eatPizza(double usersWeight) {
    double result;
    double pizzas = 0.0;
    double pizzaGain = 0.0;
    printf("How many pizza's did you eat?\n");
    scanf("%i", &pizzas);
    flush;
    pizzaGain = pizzas * .075;

    if (pizzas > 2) {
        pizzaGain = pizzaGain + 1;
    }

    result  = usersWeight + pizzaGain;

    return result;
}

double eatCake(double usersWeight) {
    double result;
    double cakesEaten = 0.0;
    double cakeGain = 0.0;
    printf("How many cakes did you eat?\n");
    scanf("%i", &cakesEaten);
    flush;
    cakeGain = cakesEaten * 2;
    result = usersWeight + cakeGain;

    return result;
}

double hardWorkout(double usersWeight) {
    double result;
    double hardWorkoutHours = 0.0;
    double hardWorkoutLoss = 0.0;
    printf("How many hours did you perform a hard workout?\n");
    scanf("%i", &hardWorkoutHours);
    flush;
    hardWorkoutLoss = hardWorkoutHours * 1;

    if (hardWorkoutHours > 2) {
        hardWorkoutLoss = hardWorkoutLoss + 1;
    }

    result = usersWeight - hardWorkoutLoss;

    return result;
}

double mediumWorkout(double usersWeight) {
    double result;
    double mediumWorkoutHours = 0.0;
    double mediumWorkoutLoss = 0.0;
    printf("How many hours did you perform a medium workout?\n");
    scanf("%i", &mediumWorkoutHours);
    flush;
    mediumWorkoutLoss = mediumWorkoutHours * .5;

    if (mediumWorkoutHours > 2) {
        mediumWorkoutLoss = mediumWorkoutLoss + .25;
    }

    result = usersWeight - mediumWorkoutLoss;

    return result;
    }

    double easyWorkout(double usersWeight) {
    double result;
    double easyWorkoutHours = 0.0;
    double easyWorkoutLoss = 0.0;
    printf("How many hours did you perform a easy workout?\n");
    scanf("%i", &easyWorkoutHours);
    flush;
    easyWorkoutLoss = easyWorkoutHours * .025;
    result = usersWeight - easyWorkoutLoss;

    return result;
    }

    /*void weightChecker(double usersWeight) {

    if (usersWeight >= 200 && usersWeight <= 400) 
    {
        printf("You are obese, time to start working out.\n");
    }
    else if (usersWeight >= 150 && usersWeight < 200)
    {
        printf("Your weight is considered heavy, time to cut down on junk food.\n");
    }
    else if (usersWeight >= 125 && usersWeight <150)
    {
        printf("You are fit, keep up the good work.\n");
    }
    else if (usersWeight >= 80 && usersWeight <125)
    {
        printf("You are thin, time to start considering eating more.\n");
    }
    else
    {
        printf("You are dead due to improper weight management. Game Over!\n");
        pause;
        exit(0);
    }

    pause;
} */

1 个答案:

答案 0 :(得分:1)

当您打印double或float值时,应使用%f

float x;
x = eatNutella();
printf("x is %f\n", x);

%d用于十进制值(如intp>