0

I am writing a prototype to a practice program in C language. In the prototype, I am asking the user to input their current weight, current height and target BMI loss as a single line of input in the terminal.

Example: 100 200 30

In this case the variables currentWeight should be read as 100, currentHeight as 200 and targetBMI as 30.

I created a sub function where the user inputs this and to transfer the information to the main function. However when I compiled the program, it read the variables incorrectly. If anybody could help to indicate what needs to be corrected in the code, it would be greatly appreciated.

The code is below:

/* pSteps3_and_4_getStdIn_5th_Updated.c */

/*******************************************************************************************************
* Prototypes for Steps 3 and 4
* Step 3 - Call getStdIn() function
* Step 4 - Get stdInText (including curent weight, current height and target BMI) from standard input
*******************************************************************************************************/

#include <stdio.h> 
#define maxLength 11

char getStdIn(int, int, int); 

int main()
{
    int currentWeight, currentHeight, targetBMI; 
    char stdInText; 
    int sscanfOutput[maxLength];
    
    sscanfOutput[maxLength] = getStdIn(currentWeight, currentHeight, targetBMI);
    
    /*******************************************************************************************************
    * The below 7 lines of code for printf will not be in the final program. 
    * They are just shown here to check if the variable values are transferred correctly to the main function.
    *******************************************************************************************************/
    printf("\n");
    printf("-----------------Start of main() function check results-----------------\n");
    printf("Current Weight in main() Function: %d kg. \n", currentWeight); 
    printf("Current Height in main() Function: %d kg. \n", currentHeight); 
    printf("Target BMI in main() Function: %d kg/cm^2. \n", targetBMI); 
    printf("-----------------End of main() function check results-----------------\n");
    printf("\n");
    
    return 0; 
}

char getStdIn(int currentWeight, int currentHeight, int targetBMI)
{
    char stdInText[maxLength]; 
    
    printf("Please enter Current Weight, Current Height and Target BMI. \n"); 
    
    fgets(stdInText, maxLength, stdin); 
    sscanf(stdInText, "%d %d %d", &currentWeight, &currentHeight, &targetBMI); 
    
    /*******************************************************************************************************
    * The below 7 lines of code for printf will not be in the final program. 
    * They are just shown here to check if the user input is correctly assigned to the respective variables.
    *******************************************************************************************************/
    printf("\n");
    printf("-----------------Start of getStdIn() function check results-----------------\n"); 
    printf("Current Weight in getStdIn() Function: %d kg. \n", currentWeight); 
    printf("Current Height in getStdIn() Function: %d cm. \n", currentHeight); 
    printf("Target BMI in getStdIn() Function: %d kg/cm^2. \n", targetBMI); 
    printf("-----------------End of getStdIn() function check results-----------------\n"); 
    printf("\n");
    
    return stdInText[maxLength]; 
}
New contributor
Divik is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Browse other questions tagged or ask your own question.