Please help with this. How do I write the code for outputting the mg caffeine column?

Write a program to prompt the user and input the number of cups of coffee consumed. Given that each cup contains 130 mg of caffeine, compute and output the amount of caffeine consumed.

Being that 13% of the caffeine is eliminated each hour (in other words, 87% is retained) calculate and output the amount of caffeine in the body at the end of each hour for 24 hours.

Sample Output:

Enter number of cups: 2
Caffeine consumed (mg): 260

After Hour mg caffeine
1 226.2
2 196.8
3 171.2
4 149.0
5 129.6
6 112.7
7 98.1
8 85.3
9 74.2
10 64.6
11 56.2
12 48.9
13 42.5
14 37.0
15 32.2
16 28.0
17 24.4
18 21.2
19 18.4
20 16.0
21 14.0
22 12.1
23 10.6
24 9.2
Press any key to continue . . .

Write main() and no other functions. Output formatted as shown. Actual width of columns not important as long as the decimal points are lined up and there is one point to right of decimal point for real numbers.
Last edited on
What have you tried so far?
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

//Declare variables
int numbofcups;
int caffeineconsumed;
int hour = 0;
//double caffeine;

//Input information
cout << " Enter number of cups:";
cin >> numbofcups;
cout << endl;

//Calculate caffeine consumed
caffeineconsumed = numbofcups * 130;
cout << "Caffeine consumed (mg): " << caffeineconsumed << endl << endl;

//Loop of hours
cout << "After hour" << setw(17) << "mg caffeine" << endl;
while (hour < 24)
{
hour++;
cout << hour << setw(17) << caffeineconsumed << endl;
}

return 0;

I've just written to this. Thinking about using for loop for the mg caffeine, but not so sure how.

Thanks,
I've figured it out :)

/*
Write a program to prompt the user and input the number of cups of coffee consumed. Given that each cup contains 130 mg of caffeine, compute and output the amount of caffeine consumed.

Being that 13% of the caffeine is eliminated each hour (in other words, 87% is retained) calculate and output the amount of caffeine in the body at the end of each hour for 24 hours.
*/

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

//Declare variables
int numbofcups;
double caffeineconsumed;
int hour = 0;
//double caffeine;

//Input information
cout << " Enter number of cups:";
cin >> numbofcups;
cout << endl;

//Calculate caffeine consumed
caffeineconsumed = numbofcups * 130;
cout << "Caffeine consumed (mg): " << caffeineconsumed << endl << endl;
cout << setprecision(1) << fixed;

//Loop of hours
cout << left << setw(17) << "After hour" << right << setw(17) << "mg caffeine" << endl;
for (hour = 1; hour <= 24; hour++)
{
caffeineconsumed = (caffeineconsumed) * 0.87;
cout << left << setw(17) << hour << right << setw(17) << caffeineconsumed << endl;
}

return 0;

}
Topic archived. No new replies allowed.