Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- kg_lb.cpp
- g++ -o kg_lb kg_lb.cpp
- kg_lb p 20 25
- https://www.metric-conversions.org/weight/kilograms-to-pounds.htm
- 1kg = 2.20462lb
- 1lb = 0.45359kg
- */
- #include <iostream>
- #include <string>
- #include <cstdlib>
- using namespace std;
- const char* message = "kg_lb\nusage: kg_lb x y\nwhere x equals k or p, for kilograms or pounds, and y equals the weight number.\nexample to convert 30 lb to kg: kg_lb p 30\nsupports multiple weight inputs";
- const char* unit[] = {"kilograms (kg)", "pounds (lb)"};
- int main(int argc, char** argv)
- {
- float input,output;
- bool which;
- if (argc < 3)
- {
- cout << message << endl;
- return 0;
- }
- if (argv[1][0]=='k') which=0;
- else if (argv[1][0]=='p') which=1;
- else
- {
- cout << "first argument is invalid - " << argv[1] << endl << message << endl;
- return 0;
- }
- for (int i=2; i<argc; i++)
- {
- input = atof(argv[i]);
- if (which) output = input * 0.45359;
- else output = input * 2.20462;
- cout << input << " " << unit[which] << " = " << output << " " << unit[!which] << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment