Loop Help

I'm wondering how I get a message to repeat over and over again if a certain condition is true/false. In the project I'm working on, if the fill rate of the hose is less than 0.5 or greater than 2.25, I need to prompt an error message and have them re-enter the values. How do I get this message to repeat infinitely and have them re-enter if they continue to enter values outside of the allowed parameters?

Here is what I have:

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

int main()
{
	// Variables
	double fill_rate;			// More than 0.5"/hr, no more than 2.25"/hr
	double poolLenth = 40.0;
	double poolWidth = 20.0;
	double poolDepth = 6.0;
	double totalVolume = (poolDepth * poolLenth * poolWidth);
	int counter = 0;
	const string fillMessage = "What is the fill rate of your hose?";

	cout << fillMessage << endl;
	cin >> fill_rate;
	if (fill_rate < 0.5 || fill_rate > 2.25){
		cout << "ERROR. Your fill rate must be between 0.5 inches and\n"
			 << "2.25 inches. Please try again." << endl;
		cout << fillMessage << endl;
		cin >> fill_rate;
	}
	else {
		cout << "Great! You have a fantastic hose." << endl;
	}
	


	system("Pause");
    return 0;
}


So far, I have the error message and prompt to re-submit once. How can I make this continuous as long as they enter values that aren't allowed? Thanks for any advice!
you declared counter are you supposed to be using a counter?
Yes, later in the instructions for the project I'm doing I am. But for now I just need help repeating the error message over and over as long as the user enters values outside the allowed 0.5-2.25 parameters.
If it helps, here are the instructions to my project.

"Write a program to show the increase in water depth for a swimming pool that is 20' x 40' [20 feet x 40 feet]. Assume that the pool is 6' deep in all places and you need to fill the pool to within 6" (6 inches) of the top. Hence, the pool is 6' deep [Note: was 3’], 20' wide and 40' long but you must stop the water before it gets within 6” of the top (water would then be 2.5’ deep). You are anticipating a fun-filled spring and summer so you must fill the pool with your garden hoses.
Here are the details for your calculations and assumptions:
1) The pool fills at no less than 0.5" per hour and no more than 2.25” per hour, the fill-rate. Hence, you’ll need to ask the user for the input of the amount per hour. Allow up to 2 decimals and not more than 2.25” per hour.
a. Compute the gallons per hour and fill-rate into the pool. You will need to calculate the volume of the pool and the gallons in that volume per hour. See the Internet!
2) You need to determine the gallons the pool has after EACH 2” [Note: was ‘hour’] until the pool is full. [Note: 2” per hour fill-rate is easy but you’ll need to calculate the fill-rate when the fill-rate is NOT 2” per hour. The fill-rate is a variable – user-entered.] Display your results as SETs of output of twenty (20) per display page in increments of 2” displays (cout). The user MUST select Y or y for continuation until the next 20. Make sure you terminate the output once completed. That is, don't repeat the last line and don't print blank lines once you've reached the final calculation.
a. Display Counter, Time, Time, Water Depth, Gallons, Weight
i. Format:
1. Counter: nn [This is a counter which is an associated counter = the row’s output. That is, what row is this for THIS output. You’ll need to reset this to 1 when you display a new screen for data.
2. Time [Hours]: hours.decimal [hh.nnn] 3 decimals
3. Time [Hours]: Standard hours & minutes format of hh.mm.ss.nnn [3 decimals]
4. Water depth: feet.decimal [nn.nnnn] 4 decimals associated with the current Counter and Time.
5. Gallons: [nnnnn.nnnnn] 5 decimals; in pool associated with the current Counter and Time
6. Weight: What is the WEIGHT of the water in the pool? [Note: you will need to convert the volume of the water to weight.]
3) Save the program as a Notepad program with the format of
a. CSCI155 Lastname, Firstname Project 1 – Pool Fill Project.txt
4) Use C++ you’ve learned through Chapter 5.

Some hints and observations:
a) Each output line is to be every 2” so the Time calculations are based upon how long it takes to fill 2” of volume at the user-entered fill-rate. You’ll need to validate what happens when the user-enters 1.25” per hour and then you display every 2”. You’ll need to convert the time to fill 2” at 1.25” per hour to hh:mm:ss.nnn format and the hh.nnn format.
b) Water depth is to be converted to feet with associated decimals.
"
Instead of an if statement, use a while loop.
1
2
3
4
5
6
7
8
9
10
11
cout << "enter stuff"<< endl;
cin >> stuff;

// Use the condition here to include all BAD conditions (less than too_little, more than too_much)
while (stuff < too_little || stuff > too_much){
// That way it will run errors on BAD conditions and GOOD conditions will escape.
    cout << "ERROR. You entered bad stuff" << endl;
    cout << "enter stuff"<< endl;
    cin >> stuff;
}
cout << "Great! You have the right stuff." << endl;
Thank you so much!
Topic archived. No new replies allowed.