stuck looping w/o a break;

Hi this is part of an assignment I am working on for conversion of gallons to liters. My question is:

Is there a way to end this loop without a break? My teacher said we can't use a break in our program unless it is part of a switch statement.

Also I would like to know alternatives ways in writing this while keeping the loop working. I attempted to use it with this:

http://stackoverflow.com/questions/5131647/why-would-we-call-cin-clear-and-cin-ignore-after-reading-input

but the use was not the same.

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

int main()
{
    double lowGal;
    bool error, quitting = false;
    char charVal;

    while (!quitting)
        {
            cout << "Enter the lowest gallon value to display (q to quit): ";
            cin >> lowGal;
            cout << endl;

            if (cin.fail())
                {
                    cin.clear();
                    cin >> charVal; // if q in entered
                    if (charVal == 'q')
                    {
                        quitting = true; // quit the program
                        error = false;
                    }
                    else
                    {
                        cout << "You entered an illegal character: (" << charVal << ")" << endl;
                        cout << endl;
                    }
                }
                else{
                    }
        }
}
What is the problem you need to solve? In your case loop should terminate correctly if you enter 'q'. What is that use was not the same?

My teacher said we can't use a break in our program
You can also set quitting to true and continue instead of break.
If you just type q twice it will terminate correctly. I'm not sure that I see a problem here.
I need to make a user validation. If they enter a correct value, then the program will continue onto the next user input request.

If they enter an incorrect value (such as a negative number or a letter), display an error message such as "You entered an illegal character" or "value must not be negative".
So, what is the problem? YOu code already does that. You just need to add output when you enter negative value.
I want to use a do while to do the user input validation cin.fail() and cin.clear()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int inputValidation ()
 {
    cout << "Enter the lowest gallon value to display <q to quit>: ";
    cin >> lowGal;
    cout << "\nEnter the highest gallon value to display <q to quit>: ";
    cin >> hiGal;
do
{
    if (lowGal < 0)
    {
        cout << "\nError: low gallon value must not be negative.\n";
        cout << "Please re-enter low and high gallon values correctly.\n\n";
        quit = true;
        error = false;
    }else (quit = false);


also with this do-while loops, I am receiving and infinite loop.
Last edited on
My teacher said we can't use a break in our program unless it is part of a switch statement.
Just so you know, the real world doesn't make restrictions like this. If it were a good idea, then the language would forbid breaks inside loops.

You can honor the restriction by using functions and/or variables to set the loop condition. Here's one way to do it that uses both.
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
bool getDouble(const char *prompt, double &val)
{
    bool result=false;		// value to return
    bool done;			// loop condition
    char charVal;
    do {
	cout << prompt << " (q to quit): ";
	cin >> val;

	if (cin.fail()) {
	    cin.clear();
	    cin >> charVal; // if q in entered
	    if (charVal == 'q') {
		done = true;
		result = false;
	    } else {
		cout << "You entered an illegal character: (" << charVal << ")" << endl;
		cout << endl;
		done = false;
	    }
	} else {
	    result = done = true;
	}
    } while (!done);
    return result;
}


int main()
{
    double lowGal;

    while (getDouble("Enter the lowest gallon value to display", lowGal)) {
	cout << lowGal << '\n';
    }
}
Topic archived. No new replies allowed.