Okay, so this is only a portion of my code, but what I have to do is have the user enter their weight, and then choose from options like eat cake, pizza, work out, and a few others. If they choose eat easy work out, they enter the number of hours they worked out, and then they're new weight and amount of weight lost. When printed, the weight is only the number calculated and not the weight minus the weight burned. How do I fix this?

int easyWorkOut(int weight){
    int easyWorkOut = 0;
    int weightLostEasyWO = 0; 
    printf("How many hours did you work out?");
    scanf("%d", easyWorkOut);
    weightLostEasyWO = easyWorkOut * 0.025; 
    weight = weight - weightLostEasyWO;
    return weight;
    printf("Your current weight is %d pounds.  You lost %d pounds.  \n", weight, weightLostEasyWO);
    pause;
} //end easyWorkOut

int weightEntered(){
    int weight = 0;
    printf("Please Enter Your Weight: ");
    scanf("%d", &weight);

} //end of weightEntered

I think in posting only a "portion" of the code, you've edited it into nonsensicalness. Could you post more code, or ideally, a small but complete program that exhibits the problem.

When printed, the weight is only the number calculated and not the weight minus the weight burned.

are you talking about variable weightLostEasyWO... i'm confused
if so the variable weightLostEasyWO scope only exists on that function, either make it a global variable or declare it at main and pass it by reference to the func., etc.
or it could be that in main your not saving the value returned by the function easyworkOut

also @ line 8 all code after the return statement will be ignored

Edited 3 Years Ago by zeroliken

This is the entire code

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

//Prototype functions here
  void displayMenu();
  char getUserChoice();
  int amountRan();
  int burgersConsumed();
  int pizzaConsumed();
  int cakesConsumed();
  int hardWorkOut();
  int mediumWorkOut();
  int easyWorkOut();
  int weightEntered();
  void bodyType();

main(){
    // Declare Variables Here
       char userChoice= 0;

    do{
        displayMenu();
        userChoice = getUserChoice();

    switch (userChoice) {
        case 'A': //Get weight
            weightEntered();
            pause;
            break;
        case 'B': //Run
            amountRan();
            pause;
            break;
        case 'C': //Eat burger
            burgersConsumed();
            pause;
            break;
        case 'D': //Eat pizza
            pizzaConsumed();
            pause;
            break;
        case 'E': //Eat cake
            cakesConsumed();
            pause;
            break;
        case 'F': //Hard Work-Out
            hardWorkOut();
            pause;
            break;
        case 'G': //Medium Work-Out

            break;
        case 'H': //East Work-Out

            break;
        case 'I': //Quit

            break;
    } //End Switch




    }while(userChoice != 'I');
}//End of main

void displayMenu(){
    cls;
    printf("A) \tEnter a weight or change weight \n");
    printf("B) \tRun \n");
    printf("C) \tEat a burger \n");
    printf("D) \tEat pizza \n");
    printf("E) \tEat Cake \n");
    printf("F) \tHard Work-Out \n");
    printf("G) \tMedium Work-Out \n");
    printf("H) \tEasy Work-Out \n");
    printf("I) \tQuit \n\n");
    printf("Please choose your selection: ");
} //End of displayMenu()

char getUserChoice(){
    char result;
    do{
        displayMenu();
        scanf("%c", &result);
        result = toupper(result);
    }while(result  < 'A'   ||  result > 'I');
    return result;
}//End getUserChoice

int amountRan(int weight){
    int amountRan = 0;
    int weightLostRunning = 0;
    printf("How many miles did you run? Please enter an even amount.");
    scanf("%d", &amountRan);
    weightLostRunning = amountRan * 0.005;
    weight = weight - weightLostRunning;
    printf("Your current weight is %d pounds.  You lost %lf pounds. \n", weight, weightLostRunning);
    pause;
} //end amountRan

int burgersConsumed(int weight){
    int burgersConsumed = 0;
    int weightGainedBurgers = 0;
    printf("How many burgers did you eat?");
    scanf("%d", &burgersConsumed);
    if(burgersConsumed > 2){
    weightGainedBurgers = burgersConsumed * 0.005 + 0.5;
    }
    else( 
        weightGainedBurgers = burgersConsumed * 0.005);
    weight = weight + weightGainedBurgers;
    printf("Your current weight is %d pounds. You gained %d pounds. \n", weight, weightGainedBurgers);
    pause;
} //end burgersConsumed

int pizzaConsumed(int weight){
    int pizzaConsumed = 0;
    int weightGainedPizza = 0;
    printf("How many pizzas did you eat?");
    scanf("%d", &pizzaConsumed);
    if(pizzaConsumed > 2){
    weightGainedPizza = pizzaConsumed * 0.075 + 1;
    }
    else( 
        weightGainedPizza = pizzaConsumed * 0.075);
    weight = weight + weightGainedPizza;
    printf("Your current weight is %d pounds. You gained %d pounds. \n", weight, weightGainedPizza);
    pause;
} //end pizzaConsumed

int cakesConsumed(int weight){
    int cakesConsumed = 0;
    int weightGainedCake = 0;
    printf("How many cakes did you eat?");
    scanf("%d", &cakesConsumed);
    weightGainedCake = cakesConsumed * 2;
    weight = weight + weightGainedCake;
    printf("Your current weight is %d pounds. You gained %d pounds. \n", weight, weightGainedCake);
    pause;
} //end cakesConsumed

int hardWorkOut(int weight){
    int hardWorkOut = 0;
    int weightLostHardWO = 0;
    printf("How many hours did you work out?");
    scanf("%d", hardWorkOut);
    if(hardWorkOut > 2){
    weightLostHardWO = hardWorkOut * 1 - (hardWorkOut * 1) ;
    }
    else( 
        weightLostHardWO = hardWorkOut * 1);
    weight = weight - weightLostHardWO;
    printf("Your current weight is %d pounds.  You lost %d pounds.  \n", weight, weightLostHardWO);
    pause;
} //end hardWorkOut

int mediumWorkOut(int weight){
    int mediumWorkOut = 0;
    int weightLostMediumWO = 0;
    printf("How many hours did you work out?");
    scanf("%d", mediumWorkOut);
    if(mediumWorkOut > 2){
        weightLostMediumWO = mediumWorkOut * 0.5 - (mediumWorkOut * 0.25);
    }
    else( 
        weightLostMediumWO = mediumWorkOut * 0.5);
    weight = weight - weightLostMediumWO;
    return weight;
    printf("Your current weight is %d pounds.  You lost %d pounds.  \n", weight, weightLostMediumWO);
    pause;
} //end mediumWorkOut

int easyWorkOut(int weight){
    int easyWorkOut = 0;
    int weightLostEasyWO = 0; 
    printf("How many hours did you work out?");
    scanf("%d", easyWorkOut);
    weightLostEasyWO = easyWorkOut * 0.025; 
    weight = weight - weightLostEasyWO;
    return weight;
    printf("Your current weight is %d pounds.  You lost %d pounds.  \n", weight, weightLostEasyWO);
    pause;
} //end easyWorkOut

int weightEntered(){
    int weight = 0;
    printf("Please Enter Your Weight: ");
    scanf("%d", &weight);

} //end of weightEntered

Do you mean in this line?
printf("Your current weight is %d pounds. You lost %d pounds. \n", weight, weightLostEasyWO);

That line will never be reached because the function returns before that point. Some of your other functions don't return anything at all, you've got int values trying to store what presumably you mean to be float values ( weightLostEasyWO = easyWorkOut * 0.025; , for example - weightLostEasyWO is an int, so what do you think its value will be if easyWorkOut is, for example, 12?), and nothing in your code even calls the function easyWorkOut. You've also got variables with the same names as functions, which isn't going to do you any favours for legibility.

You're also frequently using scanf wrongly. scanf("%d", hardWorkOut); for example. That second parameter should be a pointer. In your code it is the int zero, which will be interpreted as a pointer and will cause a segFault when you try to write over memory that is not yours.

Edited 3 Years Ago by Moschops

This article has been dead for over six months. Start a new discussion instead.