while loop questions

"You want to write a program to monitor the amount of remaining oil in a storage tank at the end of each day. The initial supply of oil in the tank and the amount taken out each day are the input values. The program should display the amount of oil left int the tank at the end of each day and it should display a warning if the amount oil is less or equal to 10% of the tank's capacity. At this point, no more oil can be removed until the tank is refilled."

Write a program using While/EndWhile and If.

This is what I did

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
#include <iostream>
using namespace std;

int initial, out, remaining, emergency, day;

int main()
	{
		cout<<"Enter initial supply of oil (Litre): ";
		cin>>initial;
		cout<<"Enter how many anount of oil taken out per day (Litre): ";
		cin>>out;
		
		emergency=0.1*initial;
		
		remaining=initial;
		day=1;
		while (remaining<=0)
			{
				remaining=remaining-out;
				cout<<"Day "<<day<< "amount of oil is "<<remaining<<" litre";
					if (remaining<=emergency)
						{
							cout<<"**Warning, oil capacity is less than 10%**";
						}
			day++;
			}
		
		return 1;
	
	}
	


What I just wrote here didn't display anything after while sentence. Can someone please show me the actual program code for this question
closed account (3TXyhbRD)
Why should it? The initial value is strictly positive right? Check the while condition.
Change line 17 from:
while (remaining<=0)
to
while (remaining>0)
The while loop runs as long as the loop condition is true. Your statement:

 
while(remaining <= 0)


...will run as long as the variable remaining is less then or equal to zero. However, you set the remaining value to the initial value, so remaining will never be less then zero at the on-set of the loop.

i think what you may want is:
 
while(remaining >= emergency)


This loop will run until the remaining value is less then the emergency threshold.

edit:
<sarcasm>I love unlimited spying!</sarcasm>
Last edited on
my bad. so I wrote 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
#include <iostream>
using namespace std;

int initial, out, remaining, emergency, day;

int main()
	{
		cout<<"Enter initial supply of oil (Litre(s)): ";
		cin>>initial;
		cout<<"Enter how many amount of oil taken out per day (Litre(s)): ";
		cin>>out;
		
		emergency=0.1*initial;
		
		cout<<"Initial supply of oil is "<<initial<<" litre(s)";
		cout<<"\nEmergency is at "<<emergency<<" litre(s))\n";
		
		remaining=initial;
		day=1;
		while (remaining>=emergency)
			{
				remaining=remaining-out;
				cout<<"\nDay "<<day<< "amount of oil is "<<remaining<<" litre(s)";
					if (remaining<=emergency)
						{
							cout<<"\n**Warning, oil capacity is less than 10%**";
						}
			day++;
			}
		
		return 1;
	
	}


It worked just fine, but there's a bit faulty.

I enter the initial input as 100, and out as 10
Then the output looked like this
------------------------------------------

Day 1 amount of oil is 90 litre(s)
Day 2 amount of oil is 80 litre(s)
Day 3 amount of oil is 70 litre(s)
Day 4 amount of oil is 60 litre(s)
Day 5 amount of oil is 50 litre(s)
Day 6 amount of oil is 40 litre(s)
Day 7 amount of oil is 30 litre(s)
Day 8 amount of oil is 20 litre(s)
**Warning , oil capacity is less than 10%**
Day 9 amount of oil is 10 litre(s)
**Warning , oil capacity is less than 10%**
Day 10 amount of oil is 0 litre(s)
------------------------------------------

Supposedly, the emergency amount based on the input is at 10litres

Can someone please correct my coding so that the if statement would ONLY appear after Day 9, and the loop should stop at Day 9 meaning Day 10 never existed

p/s: sorry for my bad explanation if you find it was hard to understand
Try 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
#include <iostream>
using namespace std;

int initial, out, remaining, emergency, day;

int main()
{
	cout<<"Enter initial supply of oil (Litre(s)): ";
	cin>>initial;
	cout<<"Enter how many amount of oil taken out per day (Litre(s)): ";
	cin>>out;
	
	emergency=0.1*initial;
	
	cout<<"Initial supply of oil is "<<initial<<" litre(s)";
	cout<<"\nEmergency is at "<<emergency<<" litre(s))\n";
	
	remaining=initial;
	day=1;
	while (remaining>0)
	{
		remaining -= out;
		
		if (remaining<=emergency)
			cout<<"\n**Warning, oil capacity is less than 10%**";

		cout<<"\nDay "<<day++<< "amount of oil is "<<remaining<<" litre(s)";
	}

	return 1;

}
remaining < emergency

if you do it like this:

remaining <= emergency

if remaining is less then or equal to emergency the statement becomes true. But you want it to only become true if remaining is less then the emergency value.
Topic archived. No new replies allowed.