1

How do you take a user input via sscanf() and transfer the variable data to main()?

For example I am writing a prototype for diet support application. In the prototype below, if the user enters height of 100, weight of 200 and BMI of 30, the information should be transferred to the main function. However, when I compiled the prototype shown below, the information was not transferred to the main function.

#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 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 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]; 
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Divik
  • 11
  • 3

0 Answers0