Homework Assignment (For Loops) Need HELP!

I don't understand how to do it, please help :(

Assignment
Plan and code a program utilizing one or more repetition structures to solve the following problem:
After caffeine is absorbed into the body, 13% is eliminated from the body each hour. Assume a person drinks an
8-oz cup of brewed coffee containing 130 mg of caffeine, and that the caffeine is absorbed immediately into the
body. Write a program to calculate the following values:

(a) The number of hours required until 65 mg (one-half the original amount) remain in the body.
(b) The amount of caffeine in the body X hours after the person drinks the coffee.

Input
Person’s name and how many hours they want to see the output for.

Output

For (a) above, just the number of hours required until 65 mg remain in the body.
For (b) above, a listing of caffeine remaining after each hour until the user’s limit is reached.

Note
Adequately check entered data for validity. Use adequate test data to process all valid data and representative
data to show how your program handles invalid data.
Label all output clearly. Be sure your output file contains user prompts and what was entered by the user in
addition to the results of your program processing.
BE SURE TO INCLUDE ADEQUATE ERROR HANDLING IN YOUR PROGRAM AND ERROR DATA WHEN YOU RUN THE
PROGRAM TO DEMONSTRATE ERROR HANDLING.

Last edited on
for error handling make sure that they are unsigned value ( not negative) no division by zero also. the for loop start with initialize value; condition( not under 65); substraction

start with that, show us after what you came with
 
(removed)


ignore the //comments i made, i need it to output an error message when the user enters a letter. I tried using "else" but it didnt work.
Last edited on by admin
Looks fine , I mostly like you put 0.87 instead of 1 - 0.13 , but you should mention it as comment , also I did some modification , so you can try with 10 hours or -3 hours , which you did not think to verify . Also you need to verify that there is 65 mg in the body system.


Anyway double check .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
	const double ic = 130.0, r = 0.13;
	double c, h;
	double hrs;
	std::string name;
	char YesNo;
	std::cout << "What's your name" << std::endl;
	std::cin >> name;


	do {
		do
		{
			std::cout << "Input hours: " << std::endl;
			std::cin >> hrs;
		} while (hrs <= 0);

		std::cout << "Your name is " << name << std::endl;
		std::cout << std::fixed << std::showpoint << std::setprecision(4);
		h = (log(0.5) / log(0.87)) - (log(ic / 130.0) / log(0.87));
		std::cout << "Hours required until 65mg: " << h << std::endl;

		for (int i = 1.0 ; i <= hrs; i += 1.0)
		{
			c = ic * pow((1 - r), i);
			if (c < 65.0) break;
			std::cout << "After " << i << " hours, " << c << "mg is remaining." << std::endl;
		}

		std::cout << "Want to run again? <Y> <N>" << std::endl;
		std::cin >> YesNo;
		YesNo = toupper(YesNo);
	} while (YesNo == 'Y');
Topic archived. No new replies allowed.