Help With Loops

Hello, So I'm supposed to write a program that tells the user how much caffeine is in their body every hour after consumption.
13% of caffeine is eliminated from the body each hour.
The program is to ask the user to Input their name, the amount of caffeine intake in mg, and how many hours they would like data for.
The Output must have a listing of caffeine remaining in the body after each hour until the users limit is reached. I'm to use at least 1 while loop, at least 1 for loop, and at least 1 do while loop.

I understand the basic concept of loops and so far I have a chunk of the program working, but I am unsure how to make the output for the remaining caffeine in the body stop when the user's input of hours is reached. As of right now I'm having the program output all the information until the remaining caffeine level is less than 1 mg. How can I fix this?

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

string Name;
float Caff;
int Hours;
int AfterHour;
float Remain;
const float Elim = .13;


int main()
{
	cout << fixed << showpoint << setprecision(4);
	
	cout << "What is your name?" << endl;
	cin >> Name;
	cout << "\nEnter the amount of caffine you drank in Mg (must be between 10 and 300 Mgs)" << endl;
	cin >> Caff;
		if (Caff < 10 || Caff > 300)
		{
			cout << "\nMgs must be between 10 and 300" << endl;
		}
		while (Caff < 10||Caff > 300)
		{
			cout << "\nEnter the amount of caffine you drank in Mg (must be between 10 and 300 Mgs)" << endl;
			cin >> Caff;
		}
		cout << "\nHow Many hours do you want Data for? (Must be betweet 1 and 10 hours)" << endl;
		cin >> Hours;
			if (Hours < 1 || Hours > 10)
			{
				cout << "\nMust be between 1 and 10 hours" << endl;
			}
			while (Hours < 1 || Hours > 10)
			{
				cout << "\nHow Many hours do you want Data for? (Must be between 1 and 10 hours)" << endl;
				cin >> Hours;
			}
			cout << "\nName:" << Name << endl;
			cout << "After Hour \t" << "\tCaffeine Remaining, Mg" << endl;
			cout << "Initial mg\t\t" << Caff << endl;
			for (AfterHour = 1; AfterHour <= Hours; AfterHour++)
			{
				cout << AfterHour << endl;
			}
			Remain = Caff - (Caff * Elim);
			do
			{
				cout << Remain << endl;
					Remain = (Remain - (Remain * Elim));
			} while (Remain > 1);
			
}
Last edited on
Topic archived. No new replies allowed.