detect error

This is a discussion on detect error within the C Programming forums, part of the General Programming Boards category; Hi all, I just started to experiment with C, and i am trying to write a few rudimentary programs in ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    8

    detect error

    Hi all,

    I just started to experiment with C, and i am trying to write a few rudimentary programs in order to get hands on with this language. I wrote a small prog to convert inches to cm or vice versa, however, when i am trying to get a choice from the user to select the type of conversion, the program is not working fine. Can someone point me out the error pls, as the prog compiles fine.

    Code:
    #include <stdio.h>
    
    int main()
    {
          const float CMETERS = 2.54;
          float input;
          float ans;
          char choice;
    
          printf("1. Convert Inches to CM\n\n");
          printf("2. Convert CM to Inches\n\n");
          scanf("%c", &choice);
    
          if (choice == '1')
           {
            printf("Enter the Amount of Inches ");
            scanf("%f", &input);
            ans = input * CMETERS;
            printf("%f Inches are equal to %f", &input, &ans);
           }
          else if (choice == '2')
                 {
                  printf("Enter the Amount of Centimeters ");
                  scanf("%f", &input);
                  ans = input / CMETERS;
                  printf("%f CM are equal to %f", &input, &ans);
                 }
          else
              {
               printf("Your selection %c is invalid", &choice);
              }
    
          system("PAUSE");
          return 0;
    }
    Thank You

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    The main problem I can see is that you are accidentally trying to print the address values of the variables.

    Code:
    printf("%f Inches are equal to %f", &input, &ans);
    Remove the ampersands from each printf statement and it should work fine. Well, with a little more work on your output formatting.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    8
    thanks very much, that sorted out my problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11:52 PM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

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