Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jun 2008
    Posts
    17
    Thanks
    7
    Thanked 0 Times in 0 Posts

    C Programming Project

    I'm using Visual Studio 2005 and need to write a program that allows you to input a number then receive a response based on the number you input. If I typed in 5.3, I should receive a response "rating causes some damage." Unfortunately, this is not the case. Below is what I've been working with. I already tried ((5.0 <= n) && (n < 5.5)) as well as (5.0 <= n && n < 5.5). None works.

    /* Earthquake based on the Richter Scale Characterizations */
    #include<stdio.h>
    int
    main(void)
    {
    double n; /* represents number registered */
    printf("Type the number registered on the Richter scale and press return> ");
    scanf("%1f", &n);

    if (n < 5.0)
    printf("%f causes little or no damage.\n", n);
    else if (5.0 <= n < 5.5)
    printf("%f rating causes some damage.\n", n);
    else if (5.5 <= n < 6.5)
    printf("%f rating does serious damage: walls may crack or fall.\n", n);
    else if (6.5 <= n < 7.5)
    printf("%f disastrous results: houses and buildings may collapse.\n", n);
    else
    printf("%f Catastrophic: most buildings are destroyed.\n", n);

    return (0);
    }

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    A comparison such as:

    Code:
    5.0 <= n < 5.5
    Will not do what you think it does. I'll spare you the deep technical details on why it doesn't work unless you actually want to know.

    You need to write it like this:
    Code:
    n >= 5 && n < 5.5
    Give that a shot.
    OracleGuy

  • #3
    New Coder
    Join Date
    Jun 2008
    Posts
    17
    Thanks
    7
    Thanked 0 Times in 0 Posts
    OracleGuy,

    I'll give it a shot and yes, I'd like the deep technical details please.

    Thanks

  • #4
    New Coder
    Join Date
    Jun 2008
    Posts
    17
    Thanks
    7
    Thanked 0 Times in 0 Posts
    OracleGuy,

    Just tried it and it did not work. I also tried it like this ((n >= 5) && (n < 5.5)). Any other recommendations?

    Noo2this

  • #5
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    What the C compiler will do with a statement such as this:
    Code:
    5.0 <= n < 5.5
    is convert it into two operations.

    Lets say n is equal to 5.2:
    Code:
    5.0 <= 5.2 < 5.5
    The blue colored area is the first operation, so that comparison will be preformed and become either a true (1) or a false (0).

    That leaves you with:
    Code:
    1 < 5.5
    Which will then again be evaluated to either true or false, and since 1 is less than 5.5, it will evaluate to true.

    And if n was say 8, the first operation would evaluate to a 0, which is still less than 5.5 thus making the if statement true.
    OracleGuy

  • Users who have thanked oracleguy for this post:

    Jus S (11-06-2008)

  • #6
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by Jus S View Post
    OracleGuy,

    Just tried it and it did not work. I also tried it like this ((n >= 5) && (n < 5.5)). Any other recommendations?

    Noo2this
    You should print out the value of n before the if statement to make sure it is a number you are expecting.
    OracleGuy

  • #7
    New Coder
    Join Date
    Jun 2008
    Posts
    17
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thanks for the details. As far as "You should print out the value of n before the if statement to make sure it is a number you are expecting." I'm not tracking. Are you saying I need to do a printf? Still wet behind the ears on this.

  • #8
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by Jus S View Post
    Thanks for the details. As far as "You should print out the value of n before the if statement to make sure it is a number you are expecting." I'm not tracking. Are you saying I need to do a printf? Still wet behind the ears on this.
    Yes, do a printf, you'll see that your scanf isn't working right and the value of n is not what you entered.
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •