Ive spent almost 4 hours messing with this code trying to figure out whats wrong...to no avail.
I keep getting zero as the total in the displayTripCost function at the bottom, i dont know if i messed up the pointers or what when i called by reference.... Please help!!!

//Toni-Ann Mighty
//October 20, 2013
//Call By Reference

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

//Lets Prototype
void getUserInput(int *millage, int *price, int *mpg);
int getChoice();
void displayMenu();
void updateCurrentMillage(int *millage);
void updatePricePerGallon(int *price);
void displayTripCost(int,int,int);

main(){
    int choice=0,currentMillage=0, MPG=0;
    int pricePerGallonOfGas=0;

    getUserInput(&currentMillage, &pricePerGallonOfGas, &MPG);

    do {

        choice = getChoice();
        switch (choice) {
        case 1:
              updateCurrentMillage(&currentMillage);
            break;
        case 2:
              updatePricePerGallon(&pricePerGallonOfGas);
            break;
        case 3: 
            displayTripCost(currentMillage, pricePerGallonOfGas, MPG);
            break;
        } //end switch
    } while(choice!=4);

}//end main

void getUserInput(int *millage, int *price, int *mpg){
    printf("Please enter your current millage\nin other words how many miles have you driven?: \n");
    scanf_s("%i",millage);
    printf("\nPlease enter the price per gallon of gas: \n");
    scanf_s("%i", price);
    printf("\nPlease enter the MPG of your car: \n");
    scanf_s("%i",mpg);
    printf("Thank You\n");
    pause;
}//end getUserInput

int getChoice(){
    int result;
    do {
      displayMenu();
      scanf_s("%i", &result);
      if(result < 1 || result > 4){
        printf("Error! I said to enter between 1-4!\n");
        pause;
      }//end if
    } while (result < 1 || result > 4);
    return result;
} //end function getChoice

void displayMenu(){
    cls;
    printf("Main Menu\n\n");
    printf("1.Update Your Current Millage\n");
    printf("2.Update Your Total Spent of Gas\n");
    printf("3.View The Total Cost of your trip\n");
    printf("4.QUIT\n\n");
    printf("Please Make a selection..");
}//end display menu

void updateCurrentMillage(int *millage){

    printf("How miles have you driven in total now?\n\n ");
    scanf_s("%i",millage);
    printf("\nYour Milliage has been updated\n");
    pause;
}//end updateCurrentMillage

void updatePricePerGallon(int *price){
    printf("How much does a gallon of gas cost now?\n\n");
    scanf_s("%i",price);
    printf("\nThe price of gas has been updated\n");
    pause;
}//end updatePricePerGallon

void displayTripCost(int millage, int cost, int mpg){
    int math = cost/mpg, total = millage * math;
    printf("The cost of your trip is %i \n",total);
    pause;
}//end displayTripCost

If you want your calculations to be accurate at all, you need to use doubles or floats.

Integer division is especially (spectacularly) diabolical.

And "millage"? Really?

Thank you so much!! I didnt know intergers did such damage in this situation :O Your solution worked! Thanks again. As for millage, thats how my instructor spelled it in the pdf directions so i just blindly copied. Probably would have googled if i had to spell it myself xD

Edited 3 Years Ago by toniann.midori

This question has already been answered. Start a new discussion instead.