Thread: Weight Conversion Program

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    26

    Weight Conversion Program

    Hi guys,
    I started my programming class at college today and the first task is to create a program to convert kilograms to stone and pounds and pounds and stones to kilos. Im working on the first one now and so far I have this:

    Code:
    #include <stdio.h>
    
    int main ()
    {
        float a;
        printf ("Kilograms to Stones and Pounds Program \n Enter the amount in Kilograms");
        scanf ("%f",a);
        a = a / 2.2;
        printf ("%f",a);
        getchar();
        getchar();
        return 0;
    }
    I take the number they enter, divide it by 2.2 to convert it to pounds and kilos and then print the result. I use floating point variables so I can have a more accurate answer outputted.

    However when I run it, and type a number in it crashes. The debug on Dev C++ says, An Access Violation (Segmentation Fault) raised in your program. Any ideas?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are passing a float, rather than the address of a float to scanf().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    scanf("&#37;f", &a);

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    Sorry I see the mistake now no &sign before the a.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You didn't inquire about this, but consider this a freebie:

    Whenever you're naming a variable, always try to give it a name that describes WHAT THE DEVIL IT IS!

    Int a means nothing, but Kilos_In would be a variable name that you'd be perfectly clear on, even years later, looking back at your code to improve or add something to it.

    Enjoy your class!

  6. #6
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    The above is so true ^^, you can actually lose marks sometimes for assignments and so on if you given meaningless variable names.

    Not to mention it's a killer when you examine longer code that you've written after a week or so.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    Thanks for the help guys I appreciate it.



    This is my final program

    Code:
    #include <stdio.h>
    
    int main ()
    {
        float amount_in_kilos, amount_in_stones;
        int program,;
        
        printf ("Enter any number to start or 3 to quit");
        scanf ("&#37;d",&program);
        
        while(program!=3)
        {
          printf ("Press \n 1 for Kilos-Stones \n 2 for Stones-Kilos \n 3 to Quit \n");
        scanf ("%d",&program);
        
        if (program==1){
        printf ("Kilograms to Stones and Pounds Program \n\n Enter the amount in Kilograms");
        scanf ("%f",&amount_in_kilos);
        amount_in_kilos = amount_in_kilos * 2.2;
        printf ("\n The amount in Stones and Pounds is %f",amount_in_kilos);
        getchar();
        getchar();
    }
        else if (program==2)
     {   printf ("Stones and Pounds to Kilograms Program \n\n Enter the amount in Stones and Pounds");
        scanf ("%f",&amount_in_stones);
        amount_in_stones = amount_in_stones / 2.2;
        printf ("\n The amount in Kilograms is %f",amount_in_stones);
        getchar();
        getchar();
    }
    }
        return 0;
    }
    Any comments it works fine but any pointers on how to improve?
    Last edited by AKalair; 11-21-2007 at 08:50 AM.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A stone is not a pound - a stone is 14 pounds.

    I don't know if all you need for your task is to translate from kilogram (kg) to stones (st) or just pounds (lbs), or kg to st/lbs. And if the latter, you also need to accept st and lbs as input for converting to kg.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int program,;
    Typo?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    Thanks for the replies, I got so caught up in programming I forgot the Maths and what I was doing lol.

    Code:
    #include <stdio.h>
    
    int main ()
    {
        float amount_in_kilos, amount_in_stones, stoneq, stone, stonesa ;
        int program;
        
        printf ("Enter any number to start or 4 to quit");
        scanf ("%d",&program);
        
        while(program!=4)
        {
          printf (" \n Press \n 1 for Kilos-Pounds/Stone \n 2 for Pounds-Kilos \n 3 for Stone-Kilos \n 4 to Quit \n");
        scanf ("%d",&program);
        
        if (program==1){
        printf (" \n Kilograms to Stones and Pounds Program \n\n Enter the amount in Kilograms");
        scanf ("%f",&amount_in_kilos);
        amount_in_kilos = amount_in_kilos * 2.2;
        printf ("\n The amount in Pounds is %f",amount_in_kilos);
        printf ("\n\n Press 1 to convert to stones");
        scanf ("%f",&stoneq);
        if (stoneq==1){
                      stone = amount_in_kilos / 14;
                      printf (" \n The amount in stones is %f",stone );}
        getchar();
        getchar();
    }
        else if (program==2)
     {   printf (" \n Pounds to Kilograms Program \n\n Enter the amount in Pounds");
        scanf ("%f",&amount_in_stones);
        amount_in_stones = amount_in_stones / 2.2;
        printf ("\n The amount in Kilograms is %f",amount_in_stones );
        getchar();
        getchar();
    }
    else if (program==3){
         printf (" \n Stone to Kilograms Program \n\n Enter the amount in stones");
         scanf ("%f", &stonesa);
         stonesa = (stonesa * 14) / 2.2;
         printf ("\n The amount in Kilograms is %f", stonesa);
         getchar();
         getchar();}
    
    }
        return 0;
    }
    All fixed now

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What's the purpose of this:
    Code:
        printf ("Enter any number to start or 4 to quit");
        scanf ("%d",&program);
    Code:
        printf ("\n The amount in Pounds is %f",amount_in_kilos);
        printf ("\n\n Press 1 to convert to stones");
    I think the second printf() is misplaced here.

    Also, your indentation is off, as is some of the placements of braces - they should be placed so that they are CLEAR to see.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    ^
    Thanks for the advice guys, I'm not sure why I added that part at the start I think it was for the
    Code:
    while(program!=4)
    part but ive just tried it without it and it still works so I removed it. Not sure what you mean about the
    Code:
    printf()
    part?

    Tided up the formatting to.

    Code:
    #include <stdio.h>
    
    int main ()
    {
        float amount_in_kilos, amount_in_stones, stoneq, stone, stonesa;
        int program;
        
        while(program!=4)
    {
        printf (" \n Press \n 1 for Kilos-Pounds/Stone \n 2 for Pounds-Kilos \n 3 for Stone-Kilos \n 4 to Quit \n");
        scanf ("&#37;d",&program);
        
        if (program==1)
    {
        printf (" \n Kilograms to Stones and Pounds Program \n\n Enter the amount in Kilograms");
        scanf ("%f",&amount_in_kilos);
        amount_in_kilos = amount_in_kilos * 2.2;
        printf ("\n The amount in Pounds is %f",amount_in_kilos);
        printf ("\n\n Press 1 to convert to stones or any other number to continue");
        scanf ("%f",&stoneq);
        
        if (stoneq==1)
    {
        stone = amount_in_kilos / 14;
        printf (" \n The amount in stones is %f",stone );
    }
        getchar();
        getchar();
    }
        else if (program==2)
        
    {   printf (" \n Pounds to Kilograms Program \n\n Enter the amount in Pounds");
        scanf ("%f",&amount_in_stones);
        amount_in_stones = amount_in_stones / 2.2;
        printf ("\n The amount in Kilograms is %f",amount_in_stones );
        getchar();
        getchar();
    }
        else if (program==3)
    {
        printf (" \n Stone to Kilograms Program \n\n Enter the amount in stones");
        scanf ("%f", &stonesa);
        stonesa = (stonesa * 14) / 2.2;
        printf ("\n The amount in Kilograms is %f", stonesa);
        getchar();
        getchar();
    }
    
    }
        return 0;
    }

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This is a bit easier to follow, don't you think?

    Code:
    #include <stdio.h>
    
    int main ()
    {
         float amount_in_kilos, amount_in_stones, stoneq, stone, stonesa;
         int program;
         
         while(program!=4)
         {
    	  printf (" \n Press \n 1 for Kilos-Pounds/Stone \n 2 for Pounds-Kilos \n 3 for Stone-Kilos \n 4 to Quit \n");
    	  scanf ("%d",&program);
    	  
    	  if (program==1)
    	  {
    	       printf (" \n Kilograms to Stones and Pounds Program \n\n Enter the amount in Kilograms");
    	       scanf ("%f",&amount_in_kilos);
    	       amount_in_kilos = amount_in_kilos * 2.2;
    	       printf ("\n The amount in Pounds is %f",amount_in_kilos);
    	       printf ("\n\n Press 1 to convert to stones or any other number to continue");
    	       scanf ("%f",&stoneq);
    	       
    	       if (stoneq==1)
    	       {
    		    stone = amount_in_kilos / 14;
    		    printf (" \n The amount in stones is %f",stone );
    	       }
    	       getchar();
    	       >			getchar();
    	  }
    	  else if (program==2)
        
    	  {   printf (" \n Pounds to Kilograms Program \n\n Enter the amount in Pounds");
    	       scanf ("%f",&amount_in_stones);
    	       amount_in_stones = amount_in_stones / 2.2;
    	       printf ("\n The amount in Kilograms is %f",amount_in_stones );
    	       getchar();
    	       getchar();
    	  }
    	  else if (program==3)
    	  {
    	       printf (" \n Stone to Kilograms Program \n\n Enter the amount in stones");
    	       scanf ("%f", &stonesa);
    	       stonesa = (stonesa * 14) / 2.2;
    	       printf ("\n The amount in Kilograms is %f", stonesa);
    	       getchar();
    	       getchar();
    	  }
    
         }
         return 0;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Phone number to word conversion program
    By lostmyshadow in forum C Programming
    Replies: 12
    Last Post: 04-21-2009, 02:31 PM
  2. Decimal to Binary Conversion program
    By acidbeat311 in forum C Programming
    Replies: 5
    Last Post: 01-12-2006, 10:26 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. question about the loop in case conversion program
    By Elhaz in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2004, 04:06 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM