Standard input of 3 variables in one function to the main function

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

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];

}

X ITM Cloud News

Marisa

Leave a Reply

Next Post

Disable and Enable Internet without disconnecting wifi

Wed Jul 1 , 2020
Spread the love          I need a way to disable and then enable internet access via command line while still remaining connected to wifi. X ITM Cloud News
X- ITM

Cloud Computing – Consultancy – Development – Hosting – APIs – Legacy Systems

X-ITM Technology helps our customers across the entire enterprise technology stack with differentiated industry solutions. We modernize IT, optimize data architectures, and make everything secure, scalable and orchestrated across public, private and hybrid clouds.

This image has an empty alt attribute; its file name is x-itmdc.jpg

The enterprise technology stack includes ITO; Cloud and Security Services; Applications and Industry IP; Data, Analytics and Engineering Services; and Advisory.

Watch an animation of  X-ITM‘s Enterprise Technology Stack

We combine years of experience running mission-critical systems with the latest digital innovations to deliver better business outcomes and new levels of performance, competitiveness and experiences for our customers and their stakeholders.

X-ITM invests in three key drivers of growth: People, Customers and Operational Execution.

The company’s global scale, talent and innovation platforms serve 6,000 private and public-sector clients in 70 countries.

X-ITM’s extensive partner network helps drive collaboration and leverage technology independence. The company has established more than 200 industry-leading global Partner Network relationships, including 15 strategic partners: Amazon Web Services, AT&T, Dell Technologies, Google Cloud, HCL, HP, HPE, IBM, Micro Focus, Microsoft, Oracle, PwC, SAP, ServiceNow and VMware

.

X ITM