?
Solved

What is wrong with my coding?

Posted on 2008-11-04
8
Medium Priority
?
188 Views
Last Modified: 2013-12-14
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

0
Comment
Question by:Noo2this
  • 4
  • 3
8 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 22883222
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

0
 
LVL 9

Expert Comment

by:mgonullu
ID: 22883236
Why not use cin and cout instead?
0
 
LVL 9

Expert Comment

by:mgonullu
ID: 22883255
By the way I found another solution using scanf and printf
change double n to float and it will works
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Expert Comment

by:mgonullu
ID: 22883259

#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

0
 

Author Comment

by:Noo2this
ID: 22891873
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?
0
 
LVL 9

Accepted Solution

by:
mgonullu earned 500 total points
ID: 22892343
Man remove the conio,
and remove the _getch()
in my code I used scanf and printf again, please reread my snippet
My code worked fine,
you cannot use switch case here as there are comparisons going on, the nested if works fine
0
 

Author Closing Comment

by:Noo2this
ID: 31513395
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?
0
 

Author Comment

by:Noo2this
ID: 22892492
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?
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question