Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: unitConverter.c
- * Author: Nathan
- *
- * A Book On C: Fourth Edition. Chapter 2, Exercise 9
- * A program that converts between ounces, pounds, grams, and kilograms
- * uses symbolic constants defined in conversions.h for the conversion factors
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "conversions.h" //contains conversion factors
- char fromUnit[50] = "No Unit";
- char toUnit[50] = "No Unit";
- double input = 0.0;
- double output = 42.0; //the answer to life, the universe, and everything
- int main(int argc, char** argv) {
- beginning:
- printf("Input the value you would like to convert.\n");
- if (scanf("%lf", &input) != 1){//how do I ensure this value is not passed to convertmore?
- printf("Invalid value. It must be a double.\n");
- goto convertmore;
- } //takes an input value as a double
- printf("Input the unit of your value. (ounce/pound/gram/kilogram)\n");
- scanf("%s", fromUnit); //takes a unit type as a string
- printf("Input the unit to convert the value to. (ounce/pound/gram/kilogram)\n");
- scanf("%s", toUnit); //takes a unit type as a string
- if (strcmp(fromUnit, "ounce") == 0) { //from ounces
- if (strcmp(toUnit, "pound") == 0) { //to pounds
- output = input / 16; //an ounce is 1/16 of a pound
- } else if (strcmp(toUnit, "gram") == 0) { //to grams
- output = input * OUNCESTOGRAMS;
- } else if (strcmp(toUnit, "kilogram") == 0) { //to kilograms
- output = (input * OUNCESTOGRAMS) / 1000; // convert ounces to grams first, and then to kilograms
- }
- } else if (strcmp(fromUnit, "pound") == 0) { //from pounds
- if (strcmp(toUnit, "ounce") == 0) { //to ounces
- output = input * 16; //an ounce is 16 pounds
- } else if (strcmp(toUnit, "gram") == 0) { //to grams
- output = input * POUNDSTOGRAMS;
- } else if (strcmp(toUnit, "kilogram") == 0) {//to kilograms
- output = (input * POUNDSTOGRAMS) / 1000; //convert pounds to grams first, and then to kilograms
- }
- } else if (strcmp(fromUnit, "gram") == 0) { //from grams
- if (strcmp(toUnit, "ounce") == 0) { //to ounces
- output = input * GRAMSTOOUNCES;
- } else if (strcmp(toUnit, "pound") == 0) { //to pounds
- output = input * GRAMSTOPOUNDS;
- } else if (strcmp(toUnit, "kilogram") == 0) {//to kilograms
- output = input / 1000; // kilogram is 1/1000 of a gram
- }
- } else if (strcmp(fromUnit, "kilogram") == 0) { //from kilograms
- if (strcmp(toUnit, "ounce") == 0) { //to ounces
- output = (input * 1000) * GRAMSTOOUNCES; //convert to grams first, then grams to ounces
- } else if (strcmp(toUnit, "pound") == 0) {
- output = (input * 1000) * GRAMSTOPOUNDS; //convert to grams first, and then to pounds
- } else if (strcmp(toUnit, "gram") == 0) {
- output = input * 1000; // a gram is 1000 kilograms
- }
- } else if (strcmp(fromUnit, toUnit) == 0) { //if the units are the same
- output = input; //the output is the same as the input
- }
- else {
- printf("Unsupported unit type\n");
- goto convertmore;
- }
- printf("%lf %s is equal to %lf %s\n", input, fromUnit, output, toUnit);
- convertmore:
- printf("Would you like to perform additional conversions? (y/n)\n");
- char str[50];
- scanf("%s", str);
- if (strcmp(str, "y") == 0 || strcmp(str, "yes") == 0) {
- goto beginning;
- } else {
- printf("Exiting.\n");
- return (EXIT_SUCCESS);
- }
- }
- /*
- * File: conversions.h
- * Author: Nathan
- */
- #define POUNDSTOGRAMS 453.59237
- #define GRAMSTOPOUNDS 0.00220462262
- #define OUNCESTOGRAMS 28.3495231
- #define GRAMSTOOUNCES 0.0352739619
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement