WHY ISNT MY C++ CODE WORKING THE WAY IT SHOULD

here is the program file:///C:/Users/paulp/Desktop/HW06_S19_Part2.pdf here is my code: (Enter -4 50, 101, 100, and then 4) press y, enter 15 20, enter 101, enter 100, and then 38) #include IOSTREAM using namespace std; //Always include in C++ programming. int main() // Always include in C++ programming. { char answer; // answer variable defined a character. int month = 0; // defining months as an integer for my while loop. 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. float drug_potency_loss, target_effectiveness; // defining my variables as a float. do { //start do loop effectiveness = 100.0; month = 0; // I had to put my variables again bc the do loop runs the whole code once, otherwise it won't work properly as expected. cout << "Enter drug potency loss % and target effectiveness %: "; // cout statement tells the user to enter the input. cin >> drug_potency_loss >> target_effectiveness; // cin statement allows the user to enter the input. 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. cout << "" << endl; //this creates a blank line. if ((drug_potency_loss < 100 && drug_potency_loss > 0) == true) { cout << "Potency loss = " << drug_potency_loss / 100.00 << endl; // This will calculate the potency loss in decimal numbers instead of percentage numbers. cout << "Target effectiveness = " << target_effectiveness << endl; // cout statement tells the user the target effectiveness. cout << " " << endl; //creates a blank line. cout << "Determining drug effectiveness: ..." << endl; cout << "" << endl;\\ } **IDK WHY THIS PART OF THE CODE WONT SHOW WHEN I type in 3 attempts: -4 50, 101 50, 100 50, and then 4 50. It will only show if I type in 4 50 at least once!** while (effectiveness + 1 > target_effectiveness) { // A while loop. The loop will pretty much stop whenever the drug effectiveness is LESS than the target effectiveness. // 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. if (drug_potency_loss < 0) { cout << "Drug potency cannot be negative, please renter: "; cin >> drug_potency_loss; // If the user enters a number less than 0, it will tell the user to renter another number (potency loss) } if (drug_potency_loss > 100) { cout << "Drug potency has to be < 100%, please renter: "; cin >> drug_potency_loss; // If the user enters a number greater than 100, it will tell the user to renter another numnber (potency loss) } if (drug_potency_loss == 100) { cout << "Drug potency has to be < 100%, please renter: "; cin >> drug_potency_loss; } else if (drug_potency_loss == 0) { cout << "Drug has 0% potency loss, it will not expire" << endl; break; } else if (target_effectiveness < 25) { cout << "Effectiveness cannot be < 25%, please renter: "; cin >> target_effectiveness; // If the user enters a number less than 25, it will tell the user to renter another number (target effectiveness) } if (target_effectiveness >= 100) { cout << "Effectiveness cannot be >= 100%, please renter: "; cin >> target_effectiveness; cout << "" << endl; // If the user enters a number greater than 100, it will tell the user to renter another number (target effectiveness) } if (target_effectiveness == 100) { cout << "Effectiveness cannot be >= 100%, pleade renter: "; cin >> target_effectiveness; cout << "" << endl; // If the user enters 100, it will tell the user to renter another number (target effectiveness) } else if (target_effectiveness != 0) { // If the user ignores all the previous statements above, then the loop proceeds to do it's calculation, // and predicts when the drugs will expire according to months. cout << "For month " << month << " drug effectiveness is " << effectiveness << endl; // cout statement within the loop. // It will repeat until the drug effectiveness is les than the target effectiveness. effectiveness = effectiveness - (effectiveness * (drug_potency_loss / 100.00)); // This is the equation in order to calculate the drug effectiveness within the loop. month++; // Month++ adds the months starting from 0 until the drug effectivess is less than the target effectiveness. } } // puts this statement in here. 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. { cout << "" << endl; // Creates a blank line. cout << "Drug will be below its target effectiveness in " << month - 1 << " months and should be discarded." << endl; } // cout stattement. // 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. cout << "" << endl; // this creates a blank line 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. cin >> answer; // the user enters y or n if they want to run the program again. cout << "" << endl; } while (answer == 'y' || answer == 'Y'); //end do loop cout << " " << endl; cout << "End Program" << endl; // cout statement if they say n. return 0; // return statement ends function. } I got everything right for the most.
Share
Best New

We're testing a new feature that gives the option to view discussion comments in chronological order. Some testers have pointed out situations in which they feel a linear view could be helpful, so we'd like see how you guys make use of it.

Report as:
Offensive Spam Harassment Incorrect Board
Cancel