-1

我的大脑很痛。我无法弄清楚这里发生了什么。如果允许给出最佳答案的人,我会付钱。

我有这个用于学校的体重程序,它允许用户输入起始体重,然后通过一个开关调用各种功能,这些功能可以让用户增加体重或减轻体重。有些函数返回我所期望的,有些则没有。这些函数返回我所期望的 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;
} */
4

1 回答 1

1

当您打印双精度或浮点值时,您应该使用 %f

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

%d 用于十进制值(如 int)

于 2013-01-18T21:40:24.747 回答