post #1 of 1
Thread Starter 
Hi Everyone,
I am have some trouble handling errors with my c code. I works fine when everything is entered correctly but a negative causes the whole thing not not work. Any help appreciated!!!
Code:
//
//  main.c
//  BMI Array
//
//  Created by 20143075 on 11/19/13.
//  Copyright (c) 2013 Andrew Less. All rights reserved.
//

#include <stdio.h>

typedef struct{
    char studentName[35];
    float weightInKilos;
    float heightInMeters;
}Person;

float bodyMassIndex (Person s)
{
    //function to calculate the body mass index
    
    float bmi;
    bmi = s.weightInKilos / (s.heightInMeters * s.heightInMeters);
    
    return bmi ;
} // end of bodyMassIndex function

float minCalc (float *bmiArray, int numberOfStudents){
    int min = bmiArray[0];
    for (int i = 0; i < numberOfStudents; i++) {
        if (min > bmiArray[i]){
            min = bmiArray[i];
        }
    }
    return min;
}
float maxCalc (float *bmiArray, int numberOfStudents){
    
    int max = bmiArray[0];
    for (int i = 0; i < numberOfStudents; i++) {
        if (max < bmiArray[i]){
            max = bmiArray[i];
        }
    }
    return max;
}
void printArray(float *studentArray, int numberOfStudents)
{
    //function to print the body mass index
    for (int i = 0; i < numberOfStudents; i++)
    {
        printf("Student #%d has a body mass index of:  %.2f\n", i+1, studentArray[i]);
    }
} // end of printArray function
float checkUserInput(float userInput, float warningLimit, float lowerLimit, float upperLimit){
    float validInput = 0.0; //0 means not valid, 1 means valid input
    int numberAttempts = 1;
    int maxAttempts = 2;
    float userInputCheck;
    const int ERROR = -1;
    
    userInputCheck = userInput;
    if (userInputCheck >= warningLimit && userInputCheck <= upperLimit)
        // weight is between 80 pounds (36.4 kg) and 400 pounds (181.4 kg)
        // weight is ok
    {
        printf("Validated\n");
        validInput = 1;
    }
    while (userInputCheck >= lowerLimit && userInputCheck <= warningLimit && numberAttempts < maxAttempts)
        // weight is between 60 pounds (27.2 kg) and 80 pounds (36.3 kg)
        // weight may be too low, ask the user to confirm
    {
        printf("Seems low, please re-enter to confirm:  ");
        scanf("%f", &userInputCheck);
        numberAttempts ++;
        printf("attempt # %d\n", numberAttempts);
        if (numberAttempts == maxAttempts && userInputCheck == userInput)
        {
            validInput = 1;
        }
    }  // end while
    while (userInputCheck < lowerLimit && numberAttempts < maxAttempts)
        // weight is less than 60 pounds (27.2 kg)
        // weight is too low, ask the user to re-enter
    {
        printf("%2.1f is Too low!  Please re-enter:  ", userInputCheck);
        scanf("%f", &userInputCheck);
        numberAttempts ++;
        printf("attempt # %d\n", numberAttempts);
        if (userInputCheck >= lowerLimit && userInputCheck <= warningLimit)
        {
            validInput = 1;
        }
    } // end while
    while (userInputCheck > upperLimit && numberAttempts < maxAttempts)
        // weight is greater than 400 pounds (181.4 kg)
        // weight is too high, ask the user to re-enter
    {
        printf("Too high!  Please re-enter:  ");
        scanf("%f", &userInputCheck);
        numberAttempts ++;
        printf("attempt # %d\n", numberAttempts);
        if (userInputCheck >= lowerLimit && userInputCheck <= upperLimit)
        {
            validInput = 1;
        }
    }  //end while
    if (validInput != 1)
        userInputCheck = ERROR;
    return userInputCheck;
} //end of checkUserInput
int main (int argc, const char * argv[])
{
    
    int numberOfStudents = 0;
    int count = 0;
    int count2 = 0;
    const int INPUTERROR = -1;
    const int ERRORWEIGHT = -1;
    const int ERRORHEIGHT = -1;
    const int WARNINGWEIGHT = 36.3;//80 pounds
    const float MINWEIGHTLIMIT = 27.2;//60 pounds
    const float MAXWEIGHTLIMIT = 181.4;//400 pounds
    const float MAXHEIGHTLIMIT = 2.13;//84 inches
    const float MINHEIGHTLIMIT = 1.02;//40 inches
    const float WARNINGHEIGHT = 1.37;//54 inches
    float height;
    float weight;
    float validWeightInKilos;
    float validHeightInMeters;
    float maxBmi = 0;
    float minBmi = 0;
    float studentBMI;
    
    printf("How many students do you have? ");
    scanf("%d", &numberOfStudents);
    
    Person error[numberOfStudents];
    Person classOfStudents[numberOfStudents];
    
    for (int count = 0; count < numberOfStudents; count++)
    {
        printf("Last Name:  ");
        scanf("%s", classOfStudents[count].studentName);
        printf("Please enter height in meters for %s:  ", classOfStudents[count].studentName);
        scanf("%f", &height);
        printf("You entered %3.1f\n", height);
        
        validHeightInMeters = checkUserInput(height, WARNINGHEIGHT, MINHEIGHTLIMIT, MAXHEIGHTLIMIT);
        printf("%f", validHeightInMeters);
        
        if (validHeightInMeters > ERRORHEIGHT){
            printf("Checked height is %3.1f\n", validHeightInMeters);
        }//end of if statement
        
        printf("Please enter weight in kilograms for %s  ", classOfStudents[count].studentName);
        scanf("%f", &weight);
        printf("You entered: %3.1f\n", weight);
        validWeightInKilos = checkUserInput(weight, WARNINGWEIGHT, MINWEIGHTLIMIT, MAXWEIGHTLIMIT);
        
        
        
        if (validWeightInKilos != ERRORWEIGHT && validHeightInMeters != ERRORHEIGHT) {
            classOfStudents[count].weightInKilos = validWeightInKilos;
            classOfStudents[count].heightInMeters = validHeightInMeters;
        }//end of if statement
        else{
            printf("Input error. Cannot compute BMI\n");
            for (int i = 0; i <= 35; i++) {
                error[count2].studentName[i] = classOfStudents[count].studentName[i];
                count2++;
                count = count - 1;
            }//end of for loop
            
        }//end of else statement
        
    }//end of for loop
    
    printf("Name\tHeight\tWeight\tBMI\n");
    
    for (count = 0; count < numberOfStudents; count++){
        studentBMI = bodyMassIndex(classOfStudents[count]);
        printf("%s\t%3.1f\t\t%3.1f\t%3.1f\n", classOfStudents[count].studentName, classOfStudents[count].heightInMeters, classOfStudents[count].weightInKilos, studentBMI);
        
        if ((validWeightInKilos != INPUTERROR) && (validHeightInMeters != INPUTERROR)) {
            //printArray(&studentBMI, numberOfStudents);
            maxBmi = maxCalc(&studentBMI, numberOfStudents);
            minBmi =  minCalc(&studentBMI, numberOfStudents);
            
        }//end of if statement
        else
            printf("Input error cannot compute BMI.\n");
    }//end of for loop
    
    if (count2 > 0) {
        for (count = 0; count < count2; count++) {
            printf("%s had an error\n", error[count].studentName);
        }//end of for loop
    }//end of if statement
    return 0;
} //end of main