Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,755 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,442 people online right now. Registration is fast and FREE... Join Now!




double output

 
Reply to this topicStart new topic

double output

tygerberg
6 Feb, 2007 - 04:22 AM
Post #1

D.I.C Head
Group Icon

Joined: 14 Dec, 2006
Posts: 114


Dream Kudos: 25
My Contributions
Hi I've made a small program that calculates yourhealth according to your length and weight, it displays the right answer, but sometimes displayes two answers. Can someone help me

CODE
#include <stdio.h>
main()
{
      float len, mas, ans;
      
      do {
      
      printf("Please enter your height: ");
      scanf("%f", &len);
      
      printf("Please enter your weight: ");
      scanf("%f", &mas);
      
      ans = mas / ( len * len );
      
      /* < 18.5  under weight
         between 18.5 and 24.9  normal
         between 25 and 29.9  over weight
         else( > 29.9 )  obese
      */
      
      if ( ans < 18.5 ) printf("Under weight %.2f\n", ans);
      if ((ans > 18.5) && (ans <= 24.9)) printf("Normal %.2f\n", ans);
      if ((ans > 25) && (ans <= 29.9))  printf("Over weight %.2f\n", ans);
      else printf("Obese %.2f\n", ans);
      
      printf("\n");
      
      } while ( ans <= 999 );
      
}


User is offlineProfile CardPM
+Quote Post

horace
RE: Double Output
6 Feb, 2007 - 04:30 AM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
consider
CODE

    if ( ans < 18.5 ) printf("Under weight %.2f\n", ans);
      if ((ans > 18.5) && (ans <= 24.9)) printf("Normal %.2f\n", ans);
      if ((ans > 25) && (ans <= 29.9))  printf("Over weight %.2f\n", ans);
      else printf("Obese %.2f\n", ans);

assuming one of the first two conditions are true this will also execute the else
CODE

else printf("Obese %.2f\n", ans);

I think you need else statements with the previous ifs, e.g.
CODE

      if ( ans < 18.5 ) printf("Under weight %.2f\n", ans);
      else
        if ((ans > 18.5) && (ans <= 24.9)) printf("Normal %.2f\n", ans);
        else
           if ((ans > 25) && (ans <= 29.9))  printf("Over weight %.2f\n", ans);
           else printf("Obese %.2f\n", ans);




User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Double Output
6 Feb, 2007 - 04:30 AM
Post #3

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,153



Thanked: 44 times
Dream Kudos: 125
My Contributions
The else is only for the last if. So whenever it matches with the 1st or 2nd if obviously it is false in case of 3rd if so it goes to else automatically and prints again...

making it if...else if...else will solve your problem.

well is that fromula is correct... huh.gif [I should take care of my health then.] biggrin.gif


oh horace is few seconds ahead me...[biggrin.gif]

This post has been edited by AmitTheInfinity: 6 Feb, 2007 - 04:32 AM
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 05:54AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

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