Link to home
Start Free TrialLog in
Avatar of Noo2this
Noo2this

asked on

What is wrong with my coding?

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.  Help!!
/* 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("%f", &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);
}

Open in new window

Avatar of Pratima
Pratima
Flag of India image

try this
#include<stdio.h>
int
main(void)
{
	double n;	/* represents number registered */
	printf("Type the number registered on the Richter scale and press return> ");
	scanf("%f", &n);
 
if (n < 5.0)
	printf("%f causes little or no damage.\n", n);
else if ((5.0 <= n) && (n < 5.5))
	printf("%f rating causes some damage.\n", n);
else if ((5.5 <= n) && (n < 6.5))
	printf("%f rating does serious damage: walls may crack or fall.\n", n);
else if ((6.5 <= n) && (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);
}

Open in new window

Why not use cin and cout instead?
By the way I found another solution using scanf and printf
change double n to float and it will works

#include <stdio.h>
#include "conio.h"
int
main(void)
{
       float n=0;       /* represents number registered */
        printf("Type the number registered on the Richter scale and press return> ");
      //  scanf("%f", &n);
		scanf("%f",&n);
		printf("You entered : %f\n",n);
 
if (n < 5.0)
        printf("%f causes little or no damage.\n", n);
else if (5.0 <= n && n < 5.5)
        printf("%f rating causes some damage.\n", n);
else if (5.5 <= n && n< 6.5)
        printf("%f rating does serious damage: walls may crack or fall.\n", n);
else if (6.5 <= n && n< 7.5)
        printf("%f disastrous results: houses and buildings may collapse.\n", n);
else
        printf("%f Catastrophic: most buildings are destroyed.\n", n);
 _getch();
return (0);
}

Open in new window

Avatar of Noo2this
Noo2this

ASKER

Pratima mcs: I already tried it the way you show, as well as the way Mgonullu shows it.  It still did not work.
Mgonullu: float, cin, getch, cout, and conio.h are not approved options for me to use.  
Will this work with nested statements or do you think switch statements will work instead?
ASKER CERTIFIED SOLUTION
Avatar of mgonullu
mgonullu
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
mgonullu.  My bad, I didn't see the extra scanf & printf.  Nor did I observe the "float n=0".  Can you tell me why double did not work and why did float need to equal 0?
Mgonullu,
Can you tell me why double did not work and why did float need to equal 0?  If I'm using real numbers, why doesn't double work?