Is there anything wrong with this code?

This is a discussion on Is there anything wrong with this code? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int inches, centimeters=2; printf("Enter inches: "); scanf("%d, &inches"); printf("%d ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    24

    Is there anything wrong with this code?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    int inches, centimeters=2;
    printf("Enter inches: ");
    scanf("%d, &inches");
    printf("%d inches equals %d centimeters", inches, centimeters);
    
      return 0;
    }

    I can't find anything wrong with it, but it doesn't work..?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Look carefully at this line:
    Code:
    scanf("%d, &inches");
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    Ah! Ok got it. Next step. Why isn't the math working now?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    float inches, centimeters=2.54;
    printf("Enter inches: ");
    scanf("%f", &inches);
    printf("%.2f inches equals %.2f centimeters", inches, centimeters, inches*centimeters);
    
      return 0;
    }
    When I type in 3 inches, it should say 7.62 centimeters, yet it only says 2.54

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Notice that your format string has two format specifiers but you provide three arguments to be formatted and printed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    Ok that made me think. Thank you Ok so I re-thought my solution and I changed it to:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    float inches, centimeters;
    printf("Enter inches: ");
    scanf("%f", &inches);
    centimeters=inches*2.54;
    printf("%.2f inches equals %.2f centimeters",inches, centimeters);
    
      return 0;
    }
    And it works!

    For my reference, can you tell me how else this could be written if I declared the variables like this:

    Code:
    float inches, centimeters=2.54

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Code:
    printf("%.2f inches equals %.2f centimeters", inches, inches*centimeters);
    "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

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    Ah I was SO close. I had it:

    printf("%.2f inches equals %.2f centimeters", inches, centimeters, inches*centimeters);
    but now I see why that does not work.

    Thanks all!!

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And if you do that sort of thing, it shoudl be
    Code:
    const float centimeters = 2.54f;
    --
    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. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 09:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 05:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 01:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM

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