Please Help me with this (if) code

This is a discussion on Please Help me with this (if) code within the C Programming forums, part of the General Programming Boards category; I am practising the if code and decided to do a converter of different units of different types. It is ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    3

    Please Help me with this (if) code

    I am practising the if code and decided to do a converter of different units of different types. It is not finished yet but I am running into a problem, when I select 2 then 9, 10, 11,12 it always starts with 8. It reads off 8 then goes 9... Help will be appreciated. Here is the code ...
    Code:
    #include <stdio.h>
    #include <conio.h>
    int main(void)
    {
      int Type_of_conversion;
      float fahrenheit;
      float celsius;
      int f_or_c;
      printf("Temperature......................1\n\n");
      printf("Mass.............................2\n\n");
      printf("Type The Corresponding Number To Select The Type of Conversion: ");
      scanf("%d" ,&Type_of_conversion);
      if (Type_of_conversion ==1)
        {
        printf("\n");
        printf("Fahrenheit to Celsius.......1\n");
        printf("Celsius to Fahrenheit.......2\n");
        printf("\n");
        printf("Type the corresponding number for the unit of conversion: ");
        scanf("%d" ,&f_or_c);
        if (f_or_c == 1) {
            printf("Enter the Temperature in Fahrenheit:  ");
            scanf("%f" ,&fahrenheit);
            celsius = (fahrenheit-32)*(5.0/9.0);
            printf("%f degrees celsius", celsius);
        }
        if (f_or_c == 2) {
            printf("Enter the Temperature in Celsius:  ");
            scanf("%f" ,&celsius);
            fahrenheit = celsius*(9.0/5.0) + 32;
            printf("%f degress fahrenheit", fahrenheit);
        }
       }
       if (Type_of_conversion ==2) {
        printf("                             \n");
        printf("Kilograms to Pounds.........1\n");
        printf("Pounds to Kilograms.........2\n");
        printf("Kilograms to Ounces.........3\n");
        printf("Ounces to Kilograms.........4\n");
        printf("Pounds to Ounces............5\n");
        printf("Ounces to Pounds............6\n");
        printf("Grams to Kilograms..........7\n");
        printf("Kilograms to Grams..........8\n");
        printf("Grams to Pounds.............9\n");
        printf("Pounds to Grams.............10\n");
        printf("Grams to Ounces.............11\n");
        printf("Ounces to Grams.............12\n");
        printf("                             \n");
        printf("Type the corresponding number for the unit of conversion: ");
        int type_of_mass;
        float kg;
        float pound;
        float grams;
        float ounce;
        scanf("%d" ,&type_of_mass);
        if (type_of_mass ==1){
            printf("Enter the mass in Kilograms: ");
            scanf("%f" ,&kg);
            pound = (kg*2.2);
            printf("%f Pounds" ,pound);
        }
        if (type_of_mass ==2) {
            printf("Enter the mass in Pounds: ");
            scanf("%f" ,&pound);
            kg = (pound*0.4535);
            printf("%f Kilograms" ,kg);
        }
        if (type_of_mass ==3) {
            printf("Enter the mass in Kilograms: ");
            scanf("%f" ,&kg);
            ounce = kg*35.274;
            printf("%f Ounces" ,ounce);
        }
        if (type_of_mass ==4){
            printf("Enter the mass in Ounces: ");
            scanf("%f" ,&ounce);
            kg = ounce*(1.0/35.274);
            printf("%f Kilograms" ,kg);
        }
        if (type_of_mass ==5) {
            printf("Enter the mass in Pounds: ");
            scanf("%f" ,&pound);
            ounce = pound*16;
            printf("%f Ounces" ,ounce);
        }
        if (type_of_mass ==6) {
            printf("Enter the mass in Ounces: ");
            scanf("%f" ,&ounce);
            pound  = ounce/16;
            printf("%f Pounds" ,pound);
        }
        if (type_of_mass ==7) {
            printf("Enter the mass in Grams: ");
            scanf("%f" ,&grams);
            kg = grams/1000;
            printf("%f Kilograms" ,kg);
        }
        if (type_of_mass ==8); {
            printf("Enter the mass in Kilograms: ");
            scanf("%f" ,&kg);
            grams = (kg*1000);
            printf("%f Grams" ,grams);
        }
        if (type_of_mass ==9) {
            printf("Enter the mass in Grams: ");
            scanf("%f" ,&grams);
            pound = grams*0.0022;
            printf("%f Pounds" ,pound);
        }
        if (type_of_mass ==10) {
            printf("Enter the mass in Pounds: ");
            scanf("%f" ,&pound);
            grams = pound/0.0022046;
            printf("%f Grams" ,grams);
        }
       }
      getchar();
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,205
    Look at line 98. Misplaced semi-colon.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    3
    thanks a lot grumpy appreciate it, also is there a way to restart the code after it ends.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    do while loop... that way you can have a final menu item to exit the program

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    3
    im a beginner, can u tell me where to place the code

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,205
    Try reading your textbook, or any introductory material, about the "do while loop".
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2010, 10:28 AM
  2. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  3. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 06:09 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21