int8

C++ convert between kilograms and pounds

Sep 16th, 2025 (edited)
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | Source Code | 0 0
  1.  
  2. /*
  3.     kg_lb.cpp
  4.     g++ -o kg_lb kg_lb.cpp
  5.     kg_lb p 20 25
  6.  
  7.     https://www.metric-conversions.org/weight/kilograms-to-pounds.htm
  8.     1kg = 2.20462lb
  9.     1lb = 0.45359kg
  10. */
  11.  
  12. #include <iostream>
  13. #include <string>
  14. #include <cstdlib>
  15.  
  16. using namespace std;
  17.  
  18. 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";
  19. const char* unit[] = {"kilograms (kg)", "pounds (lb)"};
  20.  
  21. int main(int argc, char** argv)
  22. {
  23.     float input,output;
  24.     bool which;
  25.     if (argc < 3)
  26.     {
  27.         cout << message << endl;
  28.         return 0;
  29.     }
  30.     if (argv[1][0]=='k') which=0;
  31.     else if (argv[1][0]=='p') which=1;
  32.     else
  33.     {
  34.         cout << "first argument is invalid - " << argv[1] << endl << message << endl;
  35.         return 0;
  36.     }
  37.     for (int i=2; i<argc; i++)
  38.     {
  39.         input = atof(argv[i]);
  40.         if (which) output = input * 0.45359;
  41.         else output = input * 2.20462;
  42.         cout << input << " " << unit[which] << " = " << output << " " << unit[!which] << endl;
  43.     }
  44.     return 0;
  45. }
  46.  
Tags: C++
Advertisement
Add Comment
Please, Sign In to add comment