-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

double または float 値を出力するときは、 %f を使用する必要があります

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

%d は 10 進数値 (int など) 用です

于 2013-01-18T21:40:24.747 に答える