as part of an assignment I wrote a Gas cost calculator. This program outputs the current mileage, and the total spent on gas.
Here's my code :
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define PAUSE system("pause")
#define CLS system("cls")
// Declare variables
float currentMilage = 0.0, pricePerGallon = 0.0, MPG = 0.0, totalCost = 0.0;
// Function to calculate the total cost
void calculateTotalCost() {
totalCost=(currentMilage/MPG)*pricePerGallon;
}
//Function to read input from user
void userInput() {
printf("Enter the current milage: ");
scanf("%f", ¤tMilage);
printf("Enter the price per gallon: ");
scanf("%f", &pricePerGallon);
printf("Enter the MPG: ");
scanf("%f", &MPG);
calculateTotalCost();
}
// Function to calculate the cost for trip
float calculateCost(float miles) {
float cost;
cost=(miles/MPG)*pricePerGallon;
return cost;
}
//Function to display cost
void displayCost(float cost) {
printf("Cost for trip: %.2f\n", cost);
}
//Function to update the current milage
void updateCurrentMilage(float miles) {
currentMilage = currentMilage + miles;
}
// Function to calcuate the total spend on gas
void updateTotalSpend(float cost) {
totalCost=totalCost+cost;
}
// Function takeTrip
void takeTrip() {
// Declare variables
float miles,cost;
// Read input from user
printf("Enter the miles driven: ");
scanf("%f", &miles);
// Caliculate the total cost
cost=calculateCost(miles);
// Display the cost
displayCost(cost);
// Update the current milage
updateCurrentMilage(miles);
// Update the total spend
updateTotalSpend(cost);
}
// Function to display the menu
int menu() {
int ch;
printf("\n\n");
printf("1: Take a trip\n");
printf("2: Display the current milage\n");
printf("3: Display the total spend\n");
printf("4: Exit\n");
printf("Enter the choice: ");
scanf("%d",&ch);
return ch;
}
// Function to display the current milage
void displayCurrentMilage() {
printf("The current milage is %.2f\n",currentMilage);
}
// Function to display the total spend
void displayTotalSpend() {
printf("The total spend on gas is %.2f\n",totalCost);
}
// Main class
int main() {
int ch=0;
// Read the user input
userInput();
// while start
while(ch!=4) {
ch=menu();
switch(ch) {
case 1:
takeTrip();
PAUSE;
CLS;
break;
case 2:
displayCurrentMilage();
PAUSE;
CLS;
break;
case 3:
displayTotalSpend();
PAUSE;
CLS;
break;
case 4:
break;
}
} // while end
getchar();
return 0;
} // end main
The program works excellently, I just realized I am not allowed to use Global Variables or goto statements. The teacher said I must not use global variables, prototype the functions, and call functions passing values and returning stuff through the functions. How can I do this? Thanks!
Move global declarations:
float currentMilage = 0.0, pricePerGallon = 0.0, MPG = 0.0, totalCost = 0.0;
to main
.
Change:
void calculateTotalCost() {
totalCost=(currentMilage/MPG)*pricePerGallon;
}
to
float calculateTotalCost(float currentMilage, float MPG, float pricePerGallon) {
float totalCost = (currentMilage/MPG)*pricePerGallon;
return totalCost;
}
Modify functions which do calculations to take more parameters and return the value:
void updateCurrentMilage(float miles) {
currentMilage = currentMilage + miles;
}
like:
float updateCurrentMilage(float miles, float currentMilage) {
float current = currentMilage + miles;
return current;
}
Use the returned value to update the appropriate variable;
currentMilage = updateCurrentMilage(miles,currentMilage);
Repeat the process for all relevant data which you want to calculate.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.