Thread: Please halp, the code will not run...simple division.

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    21

    Please halp, the code will not run...simple division.

    Not sure what I'm doing wrong. Please help me debug this. Using C. Ch compiler.

    Code:
    #include <stdio.h>			/* standard IO library routines, like printf, scanf */
    
    
    
    
    int main(void)
    {
        
        char length_unit[30];
        float conversion_factor, equal_distance, newmiles;
        
        printf("Enter the name of the length unit you want to convert to> ");
        scanf("%s", &length_unit);
        printf("You entered: %s\n", length_unit);
        printf("Enter the conversion factor> ");
        scanf("%f", &conversion_factor);
        printf("You entered a converison factor of %.4f%s/mile\n", conversion_factor, length_unit);
        printf("Enter the distance in miles > ");
        scanf("%f", &newmiles);
        printf("You entered: %.4f miles \n", newmiles);
        
        equal_distance = coversion_factor / newmiles;
        
        printf("The equivalent distance is %f\n", equal_distance);
        
        return 0;		
    }

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    the division part halts the code

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You forgot the info on the unit you are converting FROM. Aha!

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    hi! thank you for responding to my post. Can you please be a little specific?

  5. #5
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by team23 View Post
    Not sure what I'm doing wrong. Please help me debug this. Using C. Ch compiler.

    Code:
    #include <stdio.h>			/* standard IO library routines, like printf, scanf */
    
    
    
    
    int main(void)
    {
        
        char length_unit[30];
        float conversion_factor, equal_distance, newmiles;
        
        printf("Enter the name of the length unit you want to convert to> ");
        scanf("%s", &length_unit);
        printf("You entered: %s\n", length_unit);
        printf("Enter the conversion factor> ");
        scanf("%f", &conversion_factor);
        printf("You entered a converison factor of %.4f%s/mile\n", conversion_factor, length_unit);
        printf("Enter the distance in miles > ");
        scanf("%f", &newmiles);
        printf("You entered: %.4f miles \n", newmiles);
        
        equal_distance = coversion_factor / newmiles;
        
        printf("The equivalent distance is %f\n", equal_distance);
        
        return 0;		
    }
    This thread looks familiar... see above in bold red.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Your code doesn't even compile, because as mentioned in your duplicate thread, "coversion_factor" is not the same as "conversion_factor".

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    Doh!! Thank you. I was working on this really late last night, i must have missed the obvious.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    SourceForge.net: Scanf woes - cpwiki
    Your program is a ticking time bomb. Take some time to read the article on why scanf is not recommended for reading strings.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-21-2010, 12:50 PM
  2. How to fix a simple problem with division of numbers
    By TiagoPereira in forum C Programming
    Replies: 11
    Last Post: 08-11-2010, 11:35 AM
  3. Weird error in this simple code! C2248!
    By gross100 in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2005, 01:31 AM
  4. Replies: 14
    Last Post: 11-23-2005, 08:53 AM
  5. Replies: 5
    Last Post: 04-17-2003, 07:07 AM