Hi! Are you working as an architect? Or are you a carpenter? Well, you're on the right page. Have you ever felt the feelings when you need to convert length, but you are too busy to convert it by yourself? Here, I'll show you a code to convert a length from feet to meters and otherwise. You only need an online compiler and a code!
Here we'll use functions and switch-case statement to make the length converter. Functions are used in the program to keep the code organized, reusable, and easy to maintain. Instead of writing the conversion logic multiple times, functions allow the program to call them whenever needed, making updates or fixes simpler. The switch-case statement is used because it efficiently handles multiple choices, making the code faster and easier to read compared to multiple if-else statements. Each case corresponds to a specific action, ensuring the correct function is executed based on user input.
Here's the code of the program:
//program lengthConverter.cpp
#include <iostream>
using namespace std;
double feet, meters;
void feet2meters(){
cout << "Enter length in feet:";
cin >> feet;
meters = feet*0.3048;
cout << feet << " feet is equal to " << meters << " meters." << endl;
}
void meters2feet(){
cout << "Enter length in meters:";
cin >> meters;
feet = meters*3.28084;
cout << meters << " meters is equal to " << feet << " feet." << endl;
}
int main () {
int choice;
cout << "Choose procedure type: \n";
cout << "1. feet to meters \n";
cout << "2. meters to feet \n";
cout << "Choose between 1 or 2! \n";
cout << "Enter your choice: my choice is ";
cin >> choice;
switch (choice) {
case 1:
feet2meters();
break;
case 2:
meters2feet();
break;
default:
cout << "!!!DON'T BE NAUGHTY, CHOOSE THE RIGHT ONE!!!" << endl;
cout << "-------!!!ERROR ERROR ERROR!!!--------" << endl;
}
}
In simple terms, void means that the function does something but doesn’t give anything back. Think of it like asking a friend to turn on the lights—they do it, but they don’t hand you anything in return. In this program, the feet2meters() and meters2feet() functions take user input, do the conversion, and show the result on the screen, but they don’t return any number to be used later. That’s why they use void—they just perform an action and finish.
The switch statement in the code is like a menu with different options. It looks at the user's choice and decides what to do. If the user picks 1, it runs the feet2meters() function to convert feet to meters. If they pick 2, it runs meters2feet() to convert meters to feet. If the user enters something else, the default case shows an error message, telling them to pick a valid option. The break; after each case makes sure the program only runs the chosen option and doesn’t continue checking other cases. It helps keep the code clean and organized instead of using long if-else statements.
Here I'll show you the output of the program!
First, you need to choose which converter you want to run. Here, I choose 1 which is I choose to convert feet to meter. Then I insert 9 as my input for length in feet. To convert feet to meters, 9 will be times by 0.3048. The result is 9 feet is equal to 2.7432 meters.
Here you go the length converter. I'm sure it'll help you a lot to convert length. Well, good luck!
Komentar
Posting Komentar