...

View Full Version : C Programming Project



Jus S
11-06-2008, 04:09 AM
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);
}

oracleguy
11-06-2008, 05:45 AM
A comparison such as:


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:

n >= 5 && n < 5.5

Give that a shot.

Jus S
11-06-2008, 05:57 AM
OracleGuy,

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

Thanks

Jus S
11-06-2008, 06:04 AM
OracleGuy,

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

Noo2this

oracleguy
11-06-2008, 06:05 AM
What the C compiler will do with a statement such as this:
5.0 <= n < 5.5 is convert it into two operations.

Lets say n is equal to 5.2:

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:

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
11-06-2008, 06:06 AM
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.

Jus S
11-06-2008, 06:41 AM
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.

oracleguy
11-06-2008, 03:29 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum