Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 10th, 2010 | Syntax: Python | Size: 2.21 KB | Hits: 60 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. //Michaela Patrice Morrissey
  2. //Assignment 5 b
  3. //February 24, 2010
  4. //This program reads the file memberInfo.txt and runs in batch mode. It takes the information and computes information to display.
  5.  
  6. #include <stdio.h>
  7. #include <math.h>
  8. #define desiredbmi 25.0
  9. #define METERS_PER_INCH .0254
  10. #define POUNDS_PER_KILOGRAM 2.2046
  11.  
  12. int main()
  13. {//begins program
  14.  //State variables
  15.  int            member, m=1;
  16.  double inches, pounds, meters, kilograms, bmi, pweightgoal, kweightgoal;
  17.  char           gender;
  18.  //Open file
  19.  
  20.  FILE *bodyFile;
  21.  
  22.  bodyFile = fopen("bodyFile.txt","r");
  23.  
  24.  //Begin File Open Error Option
  25.         if (bodyFile == NULL)
  26.                 printf ("Whoops! There was an error opening the file.\n");
  27.         //Begin Good open with else
  28.         else
  29.         { //begins else
  30.        
  31.                 //print out header
  32.                         printf("Mem#    Height  Weight  Gender  BMI     Target Weight   Classification\n");
  33.                 //open a while statement
  34.                 //scan info from file and store in variables defined
  35.                 fscanf(bodyFile, "%d %lf %lf %c", &member, &inches, &pounds, &gender);
  36.                 while (inches!=0)
  37.                 { //opens while
  38.                         //print member number
  39.                                 printf("%03d", member);
  40.                         //print height in inches
  41.                                 printf("%5.2f", inches);
  42.                         //print weight in pounds
  43.                                 printf("%5.2f", pounds);
  44.                         //print gender using switch
  45.                                 switch (gender)
  46.                                 {//opens switch
  47.                                         case 'M':       printf("M");
  48.                                                 break;
  49.                                         case 'F':       printf("F");
  50.                                                 break;
  51.                                 }//closes switch
  52.                                 //calculate bmi
  53.                                         //Convert height to meters
  54.                                         meters = inches * METERS_PER_INCH;
  55.                        
  56.                                         //Convert weight to kilograms
  57.                                         kilograms = pounds / POUNDS_PER_KILOGRAM;
  58.                                         //Caculate B.M.I.
  59.                                         bmi = kilograms / (meters * meters);
  60.                         //print bmi
  61.                                 printf("%f", bmi);
  62.                                 //calculate target weight
  63.                                         //Caculate desired weight
  64.                                         kweightgoal = desiredbmi * meters * meters;
  65.                                         //Convert weight to pounds
  66.                                         pweightgoal = kweightgoal * POUNDS_PER_KILOGRAM;
  67.                         //print target weight
  68.                                 printf("%f", pweightgoal);
  69.                         //Print Classification using if-elseif-else statement PRINT LEFT JUSTIFIED!
  70.                         if (bmi < 25)
  71.                         printf("normal\n");
  72.                         else if ( bmi >= 30)
  73.                         printf("obese\n");     
  74.                         else
  75.                         printf("overweight\n");
  76.                         }//end while
  77.                        
  78.         }//end else
  79.                 //close file
  80.                 fclose(bodyFile);
  81.                        
  82.         return 0;
  83.         }