nested if else troubles :-[

Started by solsunny, July 10, 2014, 02:03:48 PM

Previous topic - Next topic

solsunny

Okay, so I am REALLY new to C, and have been stumped for over 6 hours on this realy (seemingly) basic
if...else program. Here's the whole shebang:

#include <stdio.h>
#include <math.h>

/*
* This program will classify varying resistances
* according to its inputed value,
* and utilize input validation for negative values.
*/

int main(void)
{
   double resist;

      printf ("Please enter a value for resistance: ");
      scanf ("lf", &resist);

      if (resist > 1000000) {
         printf ("That resistance is in MEGA-ohms!\n");
      }
      else if (resist >= 1000) {      /* This will determine kilo-ohms */
           printf ("That resistance is in kilo-ohms.\n");
      }
      else if (resist >= 1) {      /* This will determine "base-line" ohms */
           printf ("That resistance is in ohms.\n");
      }
      else if (resist < 1) {         /* This will determine if milli-ohms */
           printf ("That resistance is milli-ohms.\n");
      }   
      else if (resist <= 0) {   /* This will be input validation for zero or a negative value */   
           printf ("You must enter a value greater than zero!\n");
      }
      return (0);
}

JohnF


solsunny

okay, now I feel real dumb...

thanks very much though!

JohnF

Quote from: solsunny on July 11, 2014, 03:05:42 AM
okay, now I feel real dumb...

thanks very much though!

I know what it feels like but there's no need to feel bad, you wont forget that again. :)

John

czerny

Try to enter 1000000 and think about your output.

Me

Maybe you've figure it out on your own until now, but you also have a small bug - see what happens if you input negative values ... . Also, what @czerny said.