Advertisement
Guest User

MY C++ code

a guest
Mar 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std; //Always include in C++ programming.
  3.  
  4. int main() // Always include in C++ programming.
  5. {
  6. char answer; // answer variable defined as a character.
  7. int month = 0; // defining months as an integer for my while loop.
  8. float effectiveness = 100.00; //drug effectiveness starts at 100. I am starting off the value as 100 and then it will decrease time over time.
  9. float drug_potency_loss, target_effectiveness; // defining my variables as a float.
  10.  
  11. do { //start do loop
  12.  
  13. effectiveness = 100.0;
  14. month = 0;
  15. // I had to put my variables again bc the do loop runs the whole code once, otherwise it won't work properly as expected.
  16.  
  17. cout << "Enter drug potency loss % and target effectiveness %: "; // cout statement tells the user to enter the input.
  18. cin >> drug_potency_loss >> target_effectiveness; // cin statement allows the user to enter the input.
  19. cout << "You entered potency loss = " << drug_potency_loss << "% " << "and Target effectiveness = " << target_effectiveness << "%" << endl; //cout statement tells the user that they entered the potency loss and the target effectiveness.
  20. cout << "" << endl; //this creates a blank line.
  21.  
  22. if ((drug_potency_loss < 100 && drug_potency_loss > 0) && (target_effectiveness > 25 && target_effectiveness < 100)) {
  23. cout << "Potency loss = " << drug_potency_loss / 100.00 << endl; // This will calculate the potency loss in decimal numbers instead of percentage numbers.
  24. cout << "Target effectiveness = " << target_effectiveness << endl; // cout statement tells the user the target effectiveness.
  25. cout << " " << endl; //creates a blank line.
  26. cout << "Determining drug effectiveness: ..." << endl; // cout statement.
  27. cout << "" << endl; // blank line.
  28. }
  29. while (effectiveness + 1 > target_effectiveness) { // A while loop. The loop will pretty much stop whenever the drug effectiveness is LESS than the target effectiveness.
  30. // I had to add the +1 in order for it to show the last result for the drug effectiveness whenever the loop works, otherwise it won't include the next month.
  31. if (drug_potency_loss < 0) {
  32. cout << "Drug potency cannot be negative, please renter: ";
  33. cin >> drug_potency_loss;
  34. // If the user enters a number less than 0, it will tell the user to renter another number (potency loss)
  35. }
  36. if (drug_potency_loss > 100) {
  37. cout << "Drug potency has to be < 100%, please renter: ";
  38. cin >> drug_potency_loss;
  39. // If the user enters a number greater than 100, it will tell the user to renter another numnber (potency loss)
  40. }
  41. if (drug_potency_loss == 100) {
  42. cout << "Drug potency has to be < 100%, please renter: ";
  43. cin >> drug_potency_loss;
  44. }
  45. else if (drug_potency_loss == 0) {
  46. cout << "Drug has 0% potency loss, it will not expire" << endl;
  47. break; // Out of all methods, I HAD to use a break statement in order for this particular part to work.
  48.  
  49. // if the drug potency loss is == 0, the cout statement will show and the break statement will stop it from going to an infinite loop.
  50. //It will then direct the user if they want to do the program again.
  51.  
  52. }
  53. else if (target_effectiveness < 25) {
  54. cout << "Effectiveness cannot be < 25%, please renter: ";
  55. cin >> target_effectiveness;
  56. // If the user enters a number less than 25, it will tell the user to renter another number (target effectiveness)
  57. }
  58. if (target_effectiveness >= 100) {
  59. cout << "Effectiveness cannot be >= 100%, please renter: ";
  60. cin >> target_effectiveness;
  61. // If the user enters a number greater than 100, it will tell the user to renter another number (target effectiveness)
  62. }
  63. if (target_effectiveness == 100) {
  64. cout << "Effectiveness cannot be >= 100%, please renter: ";
  65. cin >> target_effectiveness;
  66. // If the user enters 100, it will tell the user to renter another number (target effectiveness)
  67. }
  68. if (target_effectiveness != 0) { // If the user ignores all the previous statements above, then the loop proceeds to do it's calculation,
  69. // and predicts when the drugs will expire according to months.
  70.  
  71. effectiveness = effectiveness - (effectiveness * (drug_potency_loss / 100.00)); // This is the equation in order to calculate the drug effectiveness within the loop.
  72. cout << "For month " << month << " drug effectiveness is " << effectiveness << endl; // cout statement within the loop.
  73. // It will repeat until the drug effectiveness is les than the target effectiveness.
  74. month++; // Month++ adds the months starting from 0 until the drug effectivess is less than the target effectiveness.
  75.  
  76. }
  77.  
  78.  
  79. }
  80. // puts this statement in here.
  81. if (drug_potency_loss > 0 && drug_potency_loss < 100 && target_effectiveness > 25 && target_effectiveness < 100)// This will make sure the bottom output will not show if the user enters the drug potency loss = 0.
  82. {
  83. cout << "" << endl; // Creates a blank line.
  84. cout << "Drug will be below its target effectiveness in " << month - 1 << " months and should be discarded." << endl;
  85. }
  86. // cout stattement.
  87. // I had to add month - 1 in order for it to tell the user the correct month. If I didnt do month - 1, it will give the user the wrong month.
  88.  
  89. cout << "" << endl; // this creates a blank line
  90. cout << "Do you want to run the program again? Enter (y/n): "; // cout statement tells the user if they want to run the program again.
  91. cin >> answer; // the user enters y or n if they want to run the program again.
  92. cout << "" << endl;
  93.  
  94. } while (answer == 'y' || answer == 'Y'); //end do loop
  95.  
  96. cout << "End Program" << endl; // cout statement if they say n.
  97.  
  98. return 0; // return statement ends function.
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement