Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- // MAIN FUNCTIONS //
- void convert_lengths();
- void lengths_to_metric(); // FIX ME //
- void read_length_us();
- void convert_length_to_metric();
- void output_length_metric();
- //
- void lengths_to_us(); // FIX ME //
- void read_length_metric();
- void convert_length_to_us();
- void output_length_us();
- /**/
- void convert_weights();
- void weight_to_metric(); // FIX ME //
- void read_weight_us();
- void convert_weight_to_metric();
- void output_weight_metric();
- void weight_to_us(); // FIX ME //
- void read_weight_metric();
- void convert_weight_to_us();
- void output_weight_us();
- void clear_keyboard_buffer();
- int main(int argc, char const *argv[])
- {
- int choice, clear;
- /* Validate Answer Loop */
- while (clear < 1 || choice > 2 || choice != 0)
- {
- clear = 0;
- printf("1. convert length\n2. convert weight\n0. Exit\nPlease choose from (1, 2, 0):\n");
- clear = scanf("%d", &choice);
- clear_keyboard_buffer();
- if (choice == 1)
- {
- convert_lengths();
- }
- if (choice == 2)
- {
- convert_weights();
- }
- if (choice == 0)
- {
- printf("User chose to exit.\n");
- break;
- }
- }
- return 0;
- }
- // CONVERT LENGTHS //
- /******************************************/
- void convert_lengths()
- {
- int choice = 99, clear;
- while (clear < 1 || choice > 2 || choice == 99 || choice != 0)
- {
- printf("1. convert lengths to metrics\n");
- printf("2. convert lengths to US\n");
- printf("0. Return to Main Menu\n");
- clear = scanf("%d", &choice);
- clear_keyboard_buffer();
- if (choice == 1)
- {
- lengths_to_metric();
- }
- if (choice == 2)
- {
- lengths_to_us();
- }
- if (choice == 0)
- {
- printf("User chose to go main menu.\n");
- break;
- }
- }
- }
- // LENGTHS FUNCTIONS //
- // FIX ME //
- void lengths_to_metric()
- {
- double ft=0, in=0, m=0, cm=0;
- read_length_us(&ft, &in);
- convert_length_to_metric(&ft, &in, &m, &cm);
- output_length_metric(&ft, &in, &m, &cm);
- }
- void read_length_us(double *ftPointer, double *inPointer)
- {
- int verifyInput;
- printf("Enter feet and inches (separated by a space):\n");
- verifyInput = scanf("%lf %lf", ftPointer, inPointer);
- clear_keyboard_buffer();
- while (verifyInput < 1)
- {
- printf("That's not an acceptable value\n");
- printf("Enter feet and inches (separated by a space):\n");
- verifyInput = scanf("%lf %lf", ftPointer, inPointer);
- }
- }
- void convert_length_to_metric(double *ftPointer, double *inPointer,
- double *mPointer, double *cmPointer)
- {
- *mPointer = (*ftPointer * 0.3048) + (*inPointer/12);
- *cmPointer = (*mPointer * 100);
- }
- void output_length_metric(double *ftPointer, double *inPointer,
- double *mPointer, double *cmPointer)
- {
- printf("%.0lf feet and %.4lf inches converted to %.3lf meters and %lf centimeters.\n",
- *ftPointer, *inPointer, *mPointer, *cmPointer);
- }
- // FIX ME //
- void lengths_to_us()
- {
- double ft=0, in=0, m=0, cm=0;
- read_length_metric(&m, &cm);
- convert_length_to_metric(&m, &cm, &ft, &in);
- output_length_us(&m, &cm, &ft, &in);
- }
- void read_length_metric(double *mPointer, double *cmPointer)
- {
- int verifyInput =0;
- printf("Enter meter and centimeters (separated by a space)\n");
- verifyInput = scanf("%lf %lf", mPointer, cmPointer);
- clear_keyboard_buffer();
- while (verifyInput < 1)
- {
- printf("That's not an acceptable value\n");
- printf("Enter meter and centimeters (separated by a space)\n");
- verifyInput = scanf("%lf %lf", mPointer, cmPointer);
- clear_keyboard_buffer();
- }
- }
- void convert_length_to_us(double *mPointer, double *cmPointer, double *ftPointer, double *inPointer)
- {
- *ftPointer = (*mPointer/0.3048) + (*cmPointer * 12);
- *inPointer = (*ftPointer * 12);
- }
- void output_length_us(double *mPointer, double *cmPointer, double *ftPointer, double *inPointer)
- {
- printf("%.0lf meters and %lf centimeters converted to %.0lf feet and %lf inches.\n\n", *mPointer,
- *cmPointer, *ftPointer, *inPointer);
- }
- void convert_weights()
- {
- printf("The user wants to convert_weights.\n");
- int choice = 99, clear;
- while (clear < 1 || choice > 2 || choice == 99 || choice != 0)
- {
- printf("1. convert weights to metrics\n");
- printf("2. convert weights to US\n");
- printf("0. Return to Main Menu\n");
- clear = scanf("%d", &choice);
- clear_keyboard_buffer();
- if (choice == 1)
- {
- weight_to_metric();
- }
- if (choice == 2)
- {
- weight_to_us();
- }
- if (choice == 0)
- {
- printf("User chose to go main menu.\n");
- break;
- }
- }
- }
- // WEIGHTS FUNCTIONS //
- // FIX ME //
- void weight_to_metric()
- {
- double lb=0, oz=0, kg=0, g=0;
- read_weight_us(&lb, &oz);
- convert_length_to_metric(&lb, &oz, &kg, &g);
- output_weight_metric(&lb, &oz, &kg, &g);
- }
- void read_weight_us(double *lbPointer, double *ozPointer)
- {
- int verifyInput = 0;
- printf("Enter pound and ounces (separated by a space)\n");
- verifyInput = scanf("%lf %lf", lbPointer, ozPointer);
- clear_keyboard_buffer();
- while (verifyInput < 1)
- {
- printf("That's not an acceptable value\n");
- printf("Enter pound and ounces (separated by a space)\n");
- verifyInput = scanf("%lf %lf", lbPointer, ozPointer);
- clear_keyboard_buffer();
- }
- }
- void convert_weight_to_metric(double *lbPointer, double *ozPointer, double *kgPointer, double *gPointer)
- {
- *kgPointer = *lbPointer / 2.2046;
- *gPointer = (*kgPointer * 1000) + 1000 * (*ozPointer/16) / 2.2046;
- *kgPointer = *kgPointer + (*gPointer/1000);
- }
- void output_weight_metric(double *lbPointer, double *ozPointer, double *kgPointer, double *gPointer)
- {
- printf("%.0lf pounds and %lf ounces converted to %.0lf kilograms and %lf grams.\n\n", *lbPointer,
- *ozPointer, *kgPointer, *gPointer);
- }
- // FIX ME //
- void weight_to_us()
- {
- double lb=0, oz=0, kg=0, g=0;
- read_weight_metric(&kg, &g);
- convert_length_to_us(&kg, &g, &lb, &oz);
- output_weight_us(&kg, &g, &lb, &oz);
- }
- void read_weight_metric(double *kgPointer, double *gPointer)
- {
- int verifyInput = 0;
- printf("Enter kilograms and grams (separated by a space)\n");
- verifyInput = scanf("%lf %lf", kgPointer, gPointer);
- clear_keyboard_buffer();
- while (verifyInput < 1)
- {
- printf("That's not an acceptable value\n");
- printf("Enter kilograms and grams (separated by a space)\n");
- verifyInput = scanf("%lf %lf", kgPointer, gPointer);
- clear_keyboard_buffer();
- }
- }
- void convert_weight_to_us(double *kgPointer, double *gPointer, double *lbPointer, double *ozPointer)
- {
- printf("FIX ME: convert_weight_to_us");
- }
- void output_weight_us(double *kgPointer, double *gPointer, double *lbPointer,
- double *ozPointer)
- {
- printf("%.0lf kilograms and %lf grams converted to %.0lf pounds and %lf ounces\n",
- *kgPointer, *gPointer, *lbPointer, *ozPointer);
- }
- void clear_keyboard_buffer(void)
- {
- char ch;
- scanf("%c", &ch);
- while (ch != '\n' && ch != '\0')
- {
- scanf("%c", &ch);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement