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

Xeronator·3/18/2019, 11:35:12 PM·1 votes·1,273 views

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.

12 Comments

ModThe Djinn3/18/2019, 11:48:39 PM5 votes

When putting code up for review, it's always a good idea to start with a few things:

  1. The purpose of the program, with as much specific detail as possible.
  2. What should happen.
  3. What is happening.

That'll help when we try to look into your code.

KFCeytron3/19/2019, 12:46:20 AM3 votes
  1. We can't see your local PDF file
  2. This isn't Stack Overflow
  3. I charge $40 an hour for programming tutoring
  4. Comparing a boolean expression to true (e.g. "if ((x > 3) == true)") is unnecessary
  5. I helped a student with nearly this exact program last year, and currently display the one-line Python version on my homepage
Worst Brad Japan3/19/2019, 2:09:51 AM2 votes

Don't use C++ for scripting, use python instead.

notFREEfood3/19/2019, 3:57:35 AM2 votes
  1. For the love of god use formatting. I think there is a way to do that on boards as I recall having used it, but there is a better solution.
  2. Your comments get in the way. I don't know if this is you trying to explain it to us or some inane requirement your teacher set, but you should not have a 1:1 ratio of comments to lines of code. A good test is that if your comment is saying what, then it likely is a bad comment.

I personally haven't worked too much with c++, just c, but there's things you're doing that do not give me warm fuzzies.

TinkerTantrums3/19/2019, 12:00:33 AM2 votes

Dedicated C++ forums have nice code formatting and a trained audience. Might give them a post.

ModThe Djinn3/19/2019, 12:06:01 AM1 votes

So this has a few issues:

  1. Making it so you don't collect effectiveness and potency loss separately makes things confusing. I'd recommend one sentence per input, rather than two lines. Ask for one, check it, ask for the other, check it.
  2. You don't technically need to set your variables at the start because you reset them each time the do loop increments. Not actually a huge deal though.
  3. You do some really weird ordering things here. Calculating drug potency loss before you check your input means that you only get the output if the input is correct the first time.
  4. You use a ton of if statements when else-ifs could be sufficient. If drug_potency_loss < 0 is true, you don't need to check it against >100 as well -- it can't be both.
  5. You then run another check on all the variables you just checked, for reasons I'm not sure of -- you already know your variables are valid before you bring the drug effectiveness.
  6. You don't validate your loop input to continue, so entering anything other than 'y' or 'yy' causes an infinite loop to emerge.

There may be more, but that's what a quick look showed.

Xeronator3/18/2019, 11:45:52 PM1 votes

basically, I want my program to include

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;

even with all the 3 attempts