Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,584 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,958 people online right now. Registration is fast and FREE... Join Now!




Nester If and function call problem

 
Reply to this topicStart new topic

Nester If and function call problem, Not understanding correct way to call function

willwi7830
27 Sep, 2008 - 10:34 PM
Post #1

New D.I.C Head
*

Joined: 27 Sep, 2008
Posts: 2

Thanks for any assistance. I a working a program that inputs a richter scale number, it should evaluate the number using a nested if within the function I call richter_d

code is below. I am not understanding how to correctly call the function so it uses my input "richter_info" I believe my if else is coded right...it is how it get into the function that I am not getting. My program opens in command and asks me to input the number I do and it doesn't appear to be going into the function...any suggestion or direction would be greatly appreaciate....I am even wondering if I need to put the if else into a function

cpp

/* This is Chpt 4 Project 5. The program implements a decision process to determine
* Earthquake Ritcher scale information
* Pre: The richter scale number is inputted at prompt
* Pos: Program displays the numbers and a description of the level of damage
*/

#include <stdio.h>

/* Function prototype */
double richter_d(double richter_info);

int
main (void)
{ /* Variable list */
double richter_info; /* input of richter scale number*/
double richter; /* call the function*/

/* Get richter number input*/
printf ("Enter the richter scale number>");
scanf("lf%",&richter_info);

/* evaluate richter number by goingt to richter_d if function */
richter;

return (0);
}

/* this is the function that will evaluated the richter scale number inputted */
double
richter_d(double richter_info)
{
double richter;

if (richter_info < 5.0) /* First Range*/
printf ("A richter number of that magnitude will cause little or no damage.\n", richter_info);

else if (richter_info >= 5.0 && richter_info < 5.5) /* Second Range*/
printf ("A richter number of that magnitude will cause some damage.\n", richter_info);

else if (richter_info >= 5.5 && richter_info < 6.5) /* Third Range*/
printf ("A richter number of that magnitude will cause Serious Damage: Walls may crack and fall.\n", richter_info);

else if (richter_info >= 6.5 && richter_info < 7.5) /* Fourth Range*/
printf ("A richter number of that magnitude will cause a Disaster: Houses and buildings may collapse.\n", richter_info);

else
printf ("A richter number of that magnitude will cause a Catastrophe: Most buildings destroyed.\n", richter_info);

return (richter);

}

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Nester If And Function Call Problem
27 Sep, 2008 - 11:06 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
I made a few modifications to your code to show how you can accomplish what you're looking for. You'll need to work on the if statements in richter_d to get it to display the proper response though

cpp

/* This is Chpt 4 Project 5. The program implements a decision process to determine
* Earthquake Ritcher scale information
* Pre: The richter scale number is inputted at prompt
* Pos: Program displays the numbers and a description of the level of damage
*/

#include <stdio.h>
#include <iostream>
#include <limits>

/* Function prototype */
void richter_d(double richter_info);

int main (void)
{ /* Variable list */
double richter_info; /* input of richter scale number*/

/* Get richter number input*/
printf ("Enter the richter scale number>");
scanf("lf%",&richter_info);

/* evaluate richter number by goingt to richter_d if function */
richter_d(richter_info);
//Clean the stream and ask for input
std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
std::cin.get();

return (0);
}

/* this is the function that will evaluated the richter scale number inputted */
void richter_d(double richter_info)
{
//double richter;

if (richter_info < 5.0) /* First Range*/
printf ("A richter number of that magnitude will cause little or no damage.\n", richter_info);

else if (richter_info >= 5.0 && richter_info < 5.5) /* Second Range*/
printf ("A richter number of that magnitude will cause some damage.\n", richter_info);

else if (richter_info >= 5.5 && richter_info < 6.5) /* Third Range*/
printf ("A richter number of that magnitude will cause Serious Damage: Walls may crack and fall.\n", richter_info);

else if (richter_info >= 6.5 && richter_info < 7.5) /* Fourth Range*/
printf ("A richter number of that magnitude will cause a Disaster: Houses and buildings may collapse.\n", richter_info);

else
printf ("A richter number of that magnitude will cause a Catastrophe: Most buildings destroyed.\n", richter_info);

//return (richter);

}

User is offlineProfile CardPM
+Quote Post

baavgai
RE: Nester If And Function Call Problem
28 Sep, 2008 - 03:01 AM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,031



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
You must call your function. This:
cpp

/* evaluate richter number by goingt to richter_d if function */
richter;

Doesn't do anything. Because you've declared a variable named richter it compiles, but when it gets to this line nothing will happend.

You want something like this
cpp

scanf("lf%",&richter_info);

/* evaluate richter number by goingt to richter_d if function */
richter_d(richter_info); // now you're calling the richter_d function and passing it a parameter.


For your function, an FYI.

You can simplify richter_d, demonstrating two principals. First principal is never do anything more than once if you can help it. Since every single one of your responses has the same preamble, we can use that.

Second, and more importantly, avoid redundant logic. When you seen something like this:
cpp

f (richter_info < 5.0) /* First Range*/
else if (richter_info >= 5.0 && richter_info < 5.5) /* Second Range*/

it's helpful to realize that if it gets to the second statement, then it must be >= 5.0. In which case, you can ignore it and make shorter code.

Here's an example:
cpp

void richter_display(double richter_info) {
printf ("A richter number of that magnitude will cause ");

if (richter_info < 5.0) {
printf ("little or no damage.");
} else if (richter_info < 5.5) {
printf ("some damage.");
} else if (richter_info < 6.5) {
printf ("Serious Damage: Walls may crack and fall.");
} else if (richter_info < 7.5) {
printf ("a Disaster: Houses and buildings may collapse");
} else {
printf ("a Catastrophe: Most buildings destroyed.");
}
printf ("\n");
}



User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:47AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month