Thread: Is there anything wrong with this code?

  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
    28,413
    Look carefully at this line:
    Code:
    scanf("%d, &inches");
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    Notice that your format string has two format specifiers but you provide three arguments to be formatted and printed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,817
    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, 10: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, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02: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