0

Here is my code for a bmi calculator. I would like to add a question on the end that asks if the user wants to calculate another bmi or that the user wants to stop. I would like to have this question with a y or n answer.

  • y = calculate another bmi
  • n = goodbye

My code so far:

#include<stdio.h>;
void main()
{
 float w,h,bmi;
 printf("vul uw gewicht in in kilogram.");
 scanf("%f",&w);
 printf("vul uw hoogte in in meters. (bijvoorbeeld: 1.75)");
 scanf("%f",&h);
 bmi=w/(h*h);
 printf("bmi: %f",bmi);
 bmi<18.5?printf(" je bent best wel dun eet een burger :p"):(bmi<25)?printf(" lekker gewicht, blijf zo doorgaan"):(bmi<30)?printf(" ik zou wat minder gaan eten als ik jou was"):printf("Oh Oh, u bent in gevaar");
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Bas
  • 53
  • 1
  • 4
  • 1
    Use a `do while` loop around pretty much all of your current `main`. – Riley Sep 22 '16 at 15:29
  • See [What should `main()` return in C and C++](http://stackoverflow.com/questions/204476/) – Jonathan Leffler Sep 22 '16 at 15:33
  • 3
    And, for pity's sake, don't abuse the `?:` ternary operator like that — use `if (…) … else if (…) … else …`. Also, the semicolon at the end of the `#include` line is completely unnecessary — I'm a little surprised it compiles with that present. – Jonathan Leffler Sep 22 '16 at 15:35

1 Answers1

1

Try this

#include<stdio.h>;
void main()
{
    char opt='y';
    while(1)
    {
    float w,h,bmi;
    printf("vul uw gewicht in in kilogram.\n");
    scanf("%f",&w);
    printf("vul uw hoogte in in meters. (bijvoorbeeld: 1.75)\n");
    scanf("%f",&h);
    bmi=w/(h*h);
    printf("bmi: %f",bmi);
    bmi<18.5?printf(" je bent best wel dun eet een burger :p\n"):(bmi<25)?printf(" lekker gewicht, blijf zo doorgaan\n"):(bmi<30)?printf(" ik zou wat minder gaan eten als ik jou was\n"):printf("Oh Oh, u bent in gevaar\n");
        printf("calculate again : n for exit, y to continue?\n");
        scanf("%c&*c",&opt);
        if(opt=='n')
        {
            printf("EXIT\n");
            break;
        }
    }
}
hashdefine
  • 346
  • 2
  • 5