I need it in C Write a program that asks the user if he or she wants to convert
ID: 3779389 • Letter: I
Question
I need it in C Write a program that asks the user if he or she wants to convert values that are lengths or weights. If the user chooses lengths, the program calls a stub function convert_lengths that simply indicates the user's choice. If the user chooses weights, the program calls a stub function convert_ weights that simply indicates the user's choice. Use the value 1 to indicate lengths and the value 2 to indicate weights. If the user enters 0, terminate the program. The program should continue to prompt the user for input until he or she correctly enters a value of 0, 1, or 2. In addition, the program should allow the user to call convert_ lengths or convert_weights as many times as desired (i.e., until a value of 0 is input, indicating that the user is finished). Notice that this style of loop does not require you to ask the user if they want to continue but instead assumes they want to continue until they select 0. From now on, please always write comments for each function and explain the function parameters. You should write comments for other part of your code when needed. An example output: convert lengths convert weights Exit Please choose from (1, 2, 0):1 The user wants to convert_lengths. convert lengths convert weights Exit Please choose from (1, 2, 0): 2 The user wants to convert_welghts. convert lengths convert weights Exit Please choose from (1, 2, 0):0 User chose to exit.Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void convert_lengths(float l)
{
/*in this question you don't mention what you want to do inside the function ,
so i read the parameter from the caller function
and print here please write your operation*/
printf(" Inside the Convert_Lengths Function ");
printf("%f",l);
printf(" ");
}
void convert_weights(float w)
{
/*in this question you don't mention what you want to do inside the function ,
so i read the parameter from the caller function
and print here please write your operation*/
printf(" Inside the convert_weights Function ");
printf("%f",w);
printf(" ");
}
int main()
{
int ch;
float n;// beacause lenght or weightmay be in decimal also.
while(1)//this is an infinite loop it will continue untill user not enter the termination value here is 0
{
//below three lines are appear on the user screen when the menu is displayed.
printf("Press (1) For Convert Lengths ");
printf("Press (2) For Convert Weights ");
printf("Press (0) For Exit ");
printf("Please choose from(1,2,0) ");
scanf("%d",&ch);//user enter his choice
switch(ch)// map with relavent case
{
case 1:
printf(" User Wants to convert_lengths ");
printf(" Enter The Length which you want to convert ");
scanf("%f",&n);
convert_lengths(n);//call the convert_lengths function at starting and contrl goes over this function
break;
case 2:
printf(" User Wants to convert_weights ");
printf(" Enter The Weights which you want to convert ");
scanf("%f",&n);
convert_weights(n);//call the convert_weights function at starting and contrl goes over this function
break;
case 0:
exit(0);//when user enter the 0 the programe terminated
}
}
return 0;
}