How to Write a Program in C for Metric Conversions
- 1). Set up your program in your Visual C++ editor. Start by including the "iostream" and "string" libraries. The iostream library handles input and output processes. The string library is used to work with strings, which are data structures designed to store and handle words and text in general.
This is the code:
#include<iostream>
#include<string>
using namespace std;
int main()
{ - 2). Declare variables in your program. We need to declare variables for English units, metric units, a variable to choose a conversion, and a variable to check if the input is correct. Initialize two string arrays to keep the units (one array for English units, the other for metric units) and a double array to store the conversion factors.
These are the variable initializations:
double eng; // English units
double metric; // metric units
bool checkchoice=true; //to check the user's choice
int choice; //stores the user's choice
double conv[4] = {2.54, 1.609, 4.448, 1.609 }; //inch, mile, pound, mile per hour factors
string engUnits[4] = {"inches", "miles", "pounds", "miles per hour"};
string metUnits[4] = {"centimeters", "kilometers", "Newtons", "kilometers per hours"}; - 3). Display a menu for the user. The menu will ask the user to make a choice, and the program will execute the appropriate conversion based on the user's choice.
This is the code to display the menu:
cout << "Choose a conversion from the menu:" << endl;
cout << "1. Inches to centimeters" << endl;
cout << "2. miles to kilometers" << endl;
cout << "3. pounds to Newtons" << endl;
cout << "4. Miles per hour to kilometers per hour" << endl;
cout << "Your choice: ";
cin >> choice;
cout << endl; - 4). Check if the user's choice is correct. If the choice is incorrect, the value of checkchoice will change to "false."
This is the code:
if ( choice < 1 || choice > 4 ) //checks if the choice is correct
{ checkchoice=false; } - 5). Write a conditional branch using the value of checkchoice to decide the branch. If the user's choice is correct (checkchoice is "true"), execute the conversion; if not (checkchoice is "false"), end the program due to wrong input.
Here is the code:
if ( checkchoice )
{
cout << "Enter the quantity to convert: ";
cin >> eng;
cout << endl; //these lines "read" the user input
metric = eng * conv[choice-1]; //This is the actual conversion
cout << eng << " " << engUnits[choice-1] << " convert to ";
cout << metric << " " << metUnits[choice-1] << endl; //these lines output the conversions
}
else
{ cout << "Wrong input, the program will terminate" << endl; } - 6). End the program by returning any value. Use the "return()" command to do this.
Here is the code:
return(0);
} - 7). Copy and paste the complete code into the Visual C++ editor. Here is the complete code:
#include<iostream>
#include<string>
using namespace std;
int main()
{
double eng; // English units
double metric; // metric units
bool checkchoice=true; //to check the user's choice
int choice; //stores the user's choice
double conv[4] = {2.54, 1.609, 4.448, 1.609 }; //inch, mile, pound, mile per hour factors
string engUnits[4] = {"inches", "miles", "pounds", "miles per hour"};
string metUnits[4] = {"centimeters", "kilometers", "Newtons", "kilometers per hours"};
cout << "Choose a conversion from the menu:" << endl;
cout << "1. Inches to centimeters" << endl;
cout << "2. miles to kilometers" << endl;
cout << "3. pounds to Newtons" << endl;
cout << "4. Miles per hour to kilometers per hour" << endl;
cout << "Your choice: ";
cin >> choice;
cout << endl;
if ( choice < 1 || choice > 4 ) //checks if the choice is correct
{checkchoice=false;}
if ( checkchoice )
{
cout << "Enter the quantity to convert: ";
cin >> eng;
cout << endl; //these lines "read" the user input
metric = eng * conv[choice-1]; //This is the actual conversion
cout << eng << " " << engUnits[choice-1] << " convert to ";
cout << metric << " " << metUnits[choice-1] << endl; //these lines output the conversions
}
else
{ cout << "Wrong input, the program will terminate" << endl; }
return(0);
}
Source...