Advertisement
Guest User

C++ 04/25

a guest
Apr 25th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6.  
  7. const int BMI_CONST=703;
  8.  
  9. // modern c++ compilers are mostly read-forward
  10. //function prototype - I promise I'll define my function later!
  11. double calcBmi(double j=0, double k=0); //- can't specify default args twice!
  12. // double calcBmi(double j, double k);
  13. // double calcBmi(double,double);
  14. //more than one fx prototype will work unforuntaely
  15.  
  16. //we ONLY have to do function prototypes when the function proceeds main instead of preceeding it!
  17.  
  18. void foo() {
  19. cout << "I'm before!";
  20. }
  21.  
  22. int main(){ //command line arguments
  23. foo();
  24. // BMI_CONST = BMI_CONST + 1; => ++ // no can do with a constant!
  25.  
  26. int i;
  27. cout << i; //0 because i has not been defined yet!
  28. i = 9;
  29.  
  30. cout<< "Hello World" << std::endl;
  31. cout<< "Our C++ version is:" << __cplusplus << "\n" << endl; // we covered how this varies with stdC++ flag
  32.  
  33. cout << "Enter weight and height" << endl;
  34. double height, weight;
  35. cin >> height >> weight;
  36.  
  37. // type identifier = expression
  38. // any operation to the right of the assignment operator (==) is considered an expression!
  39.  
  40. //TODO: discuss literal default values
  41. // "matt" -> char* arr
  42.  
  43. double blah = 1 + 2 ; //this is an expression!
  44. double bmi = calcBmi(height, weight); //make a copy of the data of our local variables and pass that into the function!
  45. //position based arguments.
  46.  
  47. if (int bmi2 = calcBmi(height, weight); bmi2 < 2000) {
  48. cout << "You're skinny!";
  49. } else cout << "You're overweight!";
  50.  
  51. cout << "Your BMI is: " << bmi << endl;
  52. cout << "Type cast " << (int) (bmi) << endl;
  53.  
  54. unsigned char uc = 149;
  55. signed char sc = (signed char) (uc);
  56. //truncation is the act of reducing bits which results in a loss of precision
  57. // 8bits unsigned => 8bits
  58. cout << (int) (sc); //numerical overflow! go from 8 bit signed char to 32 bit integer
  59.  
  60. //TODO
  61. switch((int) (calcBmi(height,weight))){
  62.  
  63. }
  64.  
  65.  
  66.  
  67. //TODO: default vals!
  68. //default arguments allow for magic!
  69. cout << "Default args magic!: " << calcBmi() << endl;
  70.  
  71. return 0;
  72. }
  73. //this is called copy by value!
  74. double calcBmi(double h, double w) { //doesn't matter if I name the params the same as the name of the variables passed in
  75. // h = 93209; // the value in main() does NOT change!
  76. return (BMI_CONST*w)/ (h);
  77. }
  78.  
  79. //Formula: 703 x weight (lbs) / [height (in)]2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement