Thread: Having Trouble with a nested if else if statement.

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Norwich, CT
    Posts
    5

    Having Trouble with a nested if else if statement.

    I am working on a project for school and I am having difficulty getting this section of code to give the desired output. Can someone give me alittle guidance? No matter what value I input I get the first printf statement as a result.

    if (n<5.0)
    printf("There will be little or no damage with this earthquake.\n\n", n);

    else if (n>=5.0 && n<5.5)
    printf("There will be some damage with this earthquake.\n\n", n);

    else if (n>=5.5 && n<6.5)
    printf("There will be serious damage with this earthquake: walls may crack or fall.\n\n", n);

    else if (n>=6.5 && n<7.5)
    printf("Disastrous results will occur with this earthquake: houses and buildings may collapse.\n\n", n);

    else
    printf("Catastrophic results will occur with this earthquake: most buildings will be destroyed.\n\n", n);

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No matter what value I input I get the first printf statement as a result.
    Usually that symptom indicates you have a semicolon after your if-statement. Alternatively, you should check that you are assigning something to n.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Norwich, CT
    Posts
    5
    Here is what I have written so far, everything is compiling fine. Thanks for the quick response.

    Code:
    #include<stdio.h>
    
    /* Function prototypes*/
    void instruct_user(void);
    
    int
    main(void)
    {
    	double n; /* Represents the number registered on Richter Scale */
    
    	/* Display user instructions.*/
    	instruct_user();
    
    	/* Get user input*/
    	printf("Enter the number registered on the Richter scale and press return> ");
    	scanf("%1f", &n);
    	
    	/*The decision making process based on a Multiple-Alternative Decision order of conditions*/
    	/*A switch statement could not be used when inputing type double there for if else is more appropriate.*/
    
    	if (n<5.0) 
    		printf("There will be little or no damage with this earthquake.\n\n", n);
    
    	else if (n>=5.0 && n<5.5)
    			printf("There will be some damage with this earthquake.\n\n", n);
    
    	else if (n>=5.5 && n<6.5)
    			printf("There will be serious damage with this earthquake: walls may crack or fall.\n\n", n);
    
    	else if (n>=6.5 && n<7.5)
    			printf("Disastrous results will occur with this earthquake: houses and buildings may collapse.\n\n", n);
    
    	else 
    			printf("Catastrophic results will occur with this earthquake: most buildings will be destroyed.\n\n", n);
    	
    	return (0);
    }
    /* Displays user instructions*/
    void
    instruct_user(void)
    {
    	printf("This program will take input from the user and provide the user with the affects\n");
    	printf("of an Earthquake based on it's Richter scale.\n\n");
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    %lf
    and
    Code:
    %1f
    are not the same thing.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Location
    Norwich, CT
    Posts
    5
    Thanks so much I have been looking at this for a couple of hours, as I am a rookie I guess this would classify as a rookie mistake
    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested array vs. tree
    By KONI in forum Tech Board
    Replies: 1
    Last Post: 06-07-2007, 04:43 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  4. having trouble understanding this statement
    By kes103 in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2003, 09:00 AM
  5. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM