Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

modify the program to set valid and invalid values including character, integer/float for each option it should be able to handle wrong inputs by giving

modify the program to set valid and invalid values including character, integer/float for each option it should be able to handle wrong inputs by giving a message/re-type option.( using input validation)

# include

int main (void)

{

//The following lines of code are used to define characters and values that will be used in the program.

char m,s,L;

//changed variables -attempts for meaningful names

char input1, sub2, loop3,menuloop;

float value1, MeterInput, result;

// This statement will clear the screen, and the only thing that will show on the screen will be our program.

system("clear");

//This first set of print statements establishes our main menu, and gives options to the user to define the procedures desired.

printf("Welcome! This program will allow you convert units and add, subtract, multiply, and divide into meters, but remember All answers will be in meters!");

do

{

do

{

printf(" This program will: ");

printf("a. Add a unit of length to meters ");

printf("b. Subtract units of length from meters ");

printf("c. Multiply units of length by meters ");

printf("d. Divide units of length by meters. ");

// This printf statement sets up the user input as it requests the user to enter a character that is associated with a procedure.

printf(" What opperation do you want to perform? Select a-d: ");

scanf(" %c", &input1);

// If "a" is choosen from the main menu,the following code will be executed.

if(input1 =='a')

{

system("clear");

do

{

// The user will be notified to be select an option in the submenu. The selection will be used as a unit to be implemented into the

// calculation.

printf(" Please select a unit you would like to use: ");

printf("a. Meters ");

printf("b. centimeters ");

printf("c. feet ");

// This printf and scanf statement requests and scans the users choice from the submemu to perfrom the opperation.

printf(" Please select a - c: ");

scanf(" %c", &sub2);

if(sub2=='a'||sub2=='b'||sub2=='c')

{

// The program requests the user to enter the value that will be used in the calculation.

printf(" What is your value of the given unit: ");

scanf(" %f", &value1);

// This statement requests user input to define the additional value that will be used in the calculation.

printf(" How many meters do you want to add: ");

scanf(" %f", &MeterInput);

switch(sub2)

{

// Based upon your selection, the following lines show the calculations that will be used for each instance. If you choose a from the

// submenu, then case 'a' will be executed. This will continue for all three choices.

case'a':

result = value1 + MeterInput;

printf("%.2f meters + %.2f meters = %.2f meters", value1, MeterInput, result);

break;

case'b':

result = value1 / 100 + MeterInput;

printf("%.2f centimeters + %.2f meters = %.2f meters", value1, MeterInput, result);

break;

case'c':

result = value1 * 0.3048 + MeterInput;

printf("%.2f feet + %.2f meters = %.2f meters", value1, MeterInput, result);

break;

}

}

else

//system("clear");

printf(" ------Invalid Entry------- \tTry Again ");

} while(sub2!='a'&&sub2!='b'&&sub2!='c');

}

// This portion will exectute if option b is chosen from the main menu.

else if(input1 =='b')

{

system("clear");

do

{

// Following printf statements reestablish the submenu in which the user will select from.

printf(" Please select a unit you would like to use: ");

printf("a. Meters ");

printf("b. centimeters ");

printf("c. feet ");

printf(" Please select a - c: ");

scanf(" %c", &sub2);

if(sub2=='a'||sub2=='b'||sub2=='c')

{

// The program will prompt the user to enter two sets of values that will be used in the section.

printf(" What is the value of your unit: ");

scanf(" %f", &value1);

printf(" How many meters do you want to subtract: ");

scanf(" %f", &MeterInput);

switch(sub2)

{

// These statements will be executed depending on the option chosen in the submenu.

case'a':

result = value1 - MeterInput;

printf("%.2f meters - %.2f meters = %.2f meters", value1, MeterInput, result);

break;

case'b':

result = value1 / 100 - MeterInput;

printf("%.2f centimeters - %.2f meters = %.2f meters", value1, MeterInput, result);

break;

case'c':

result = value1 * 0.3048 - MeterInput;

printf("%.2f feet - %.2f meters = %.2f meters", value1, MeterInput, result);

break;

default:

printf("Invalid input, Please enter a valid option.");

}

}

else

//system("clear");

printf(" ------Invalid Entry------- \tTry Again ");

} while(sub2!='a'&&sub2!='b'&&sub2!='c');

}

// This portion of the program will be executed if option C in the Main menu is chosen.

else if (input1 =='c')

{

system("clear");

do

{

// The following establishes the sub-menu for which the user will make a selection

printf(" Please select a unit you would like to use: ");

printf("a. Meters ");

printf("b. centimeters ");

printf("c. feet ");

printf(" Please select a - c: ");

scanf(" %c", &sub2);

if(sub2=='a'||sub2=='b'||sub2=='c')

{

// User will be prompted to enter a set of values that will be used in the conversion to meters.

printf(" What is the value of your unit: ");

scanf(" %f", &value1);

printf(" How many meters do you want to multiply by: ");

scanf(" %f", &MeterInput);

switch(sub2)

{

// The following sections will be executed depending on the option chosen in the sub-menu.

case'a':

result = value1 * MeterInput;

printf("%.2f meters * %.2f meters = %.2f meters", value1, MeterInput, result);

break;

case'b':

result = (value1/100) * MeterInput;

printf("%.2f centimeters * %.2f meters = %.2f meters", value1, MeterInput, result);

break;

case'c':

result = (value1 * 0.3048) * MeterInput;

printf("%.2f feet * %.2f meters = %.2f meters", value1, MeterInput, result);

break;

default:

printf("Invalid input, Please enter a valid option.");

}

}

else

//system("clear");

printf(" ------Invalid Entry------- \tTry Again ");

} while(sub2!='a'&&sub2!='b'&&sub2!='c');

}

// This portion of the program will be executed if option D in the main menu is chosen.

else if (input1 =='d')

{

system("clear");

do

{

// The following establishes the sub-menu for which the user will make a selection.

printf(" Please select a unit you would like to use: ");

printf("a. Meters ");

printf("b. centimeters ");

printf("c. feet ");

printf(" Please select a - c: ");

scanf(" %c", &sub2);

if(sub2=='a'||sub2=='b'||sub2=='c')

{

// The user will be prompted to enter a set of values that will be used in the conversion to meters.

printf(" What is the value of your unit: ");

scanf(" %f", &value1);

printf(" How many meters do you want to divide by: ");

scanf(" %f", &MeterInput);

switch(sub2)

{

// The Following sections will be executed depending of the option chosen in the Sub-menu.

case'a':

result = value1 / MeterInput;

printf("%.2f meters / %.2f meters = %.2f meters", value1, MeterInput, result);

break;

case'b':

if( value1 < MeterInput)

{ printf(" Centimeter input must be greater than Meter input ");

}

else

{

result = (value1/100) / MeterInput;

printf("%.2f centimeters / %.2f meters = %.2f meters", value1, MeterInput, result);

}

break;

case'c':

result = (value1*0.3048) / MeterInput;

printf("%.2f feet / %.2f meters = %.2f meters",value1, MeterInput, result);

break;

default:

printf("Invalid Input, Please enter a valid option.");

}

}

else

//system("clear");

printf(" ------Invalid Entry------- \tTry Again ");

} while(sub2!='a'||sub2!='b'||sub2!='c');

}

else

//system("clear");

printf(" ------Invalid Entry------- \tTry Again ");

} while(input1!='a'&&input1!='b'&&input1!='c'&&input1!='d');

do

{

printf(" Do you want to calculate another number? (enter y or n) ");

scanf(" %c", &loop3);

if (loop3=='n'||loop3=='N')

{

printf (" Thank you for using the converter! ");

return 0;

}

else if(loop3 != 'y'||loop3 != 'Y'||loop3!='n'||loop3!='N')

printf(" ------Invalid Entry------- \tTry Again ");

}while( loop3 != 'y'&&loop3 != 'Y'&&loop3!='n'&&loop3!='N');

}while( loop3 == 'y'||loop3 == 'Y');

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 18th International Conference Dasfaa 2013 Wuhan China April 22 25 2013 Proceedings Part 2 Lncs 7826

Authors: Weiyi Meng ,Ling Feng ,Stephane Bressan ,Werner Winiwarter ,Wei Song

2013th Edition

3642374492, 978-3642374494

More Books

Students also viewed these Databases questions

Question

What are two sources of dioxins? Of PCBs?

Answered: 1 week ago