Exiting a do while loop

I need to exit the 2nd loop after it meets the else if statement .. but how do i do that without having to enter q to exit? I will soon have to enter the conversion and a table of conversion;

if highest - lowest <= 10 increment is 1.0
if highest - lowest <= 20 increment is 5.0
if highest - lowest <= 50 increment is 10.0
if highest - lowest <= 1000 increment is 20.0

so it should look something like this ...

if i enter 1 for the lowest gallon
and 15 for the highest gallon the increment would be of 5.0

GALLONS TO LITERS
CONVERSION TABLE
Gallons Liters
======= ======
5.0 18.925
10.0 37.850
15.0 56.775

so my next question is ... how do i set up the increment ? is the formula i came up with right ? because if i put 1 = lowest gallon and 10 = highest gallon it would be ...


GALLONS TO LITERS
CONVERSION TABLE
Gallons Liters
======= ======
1.0 3.785
2.0 7.570
3.0 11.355
4.0 15.140
5.0 18.925
6.0 22.710
7.0 30.065
8.0 30.280
9.0 34.065
10.0 37.850

with an increment of 10.0 ... keep in mind the increment does not go over 20.0

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
57
58
59
60
61
62
63
64
#include <iostream>
#include <iomanip>

using namespace std;
int 
main ()
{
   double 
   highGallons = 0, 
   lowGallons =0,
   hiGallons = 0 , 
   loGallons = 0;
   char charVal;
   bool quitting;
   quitting = false;
   
    cout << "This program creates a gallons to liter conversion table." << endl;
    do {
        cout << "\nEnter the lowest gallon value to display <q to quit>: "; 
        cin >> lowGallons;

        if (cin.fail())
        {
           cin.clear();
           cin >> charVal;
           if (charVal == 'q')
           quitting = true;
           else cout << "You entered an illegal character: ("
           << charVal << ")" << endl;
        } 
        else if (lowGallons >= 0 and lowGallons <=1000)
        {    
        do
        {
             cout << "\nEnter the highest gallon value to display <q to quit>: "; 
             cin >> highGallons;
        
             if (cin.fail())
             {
                cin.clear();
                cin >> charVal;
                if (charVal == 'q')
                quitting = true;
                else cout << "You entered an illegal character: ("
                << charVal << ")" << endl;
             } 
             
             else if (highGallons >=0 and highGallons <=1000)  ;
             {  
                hiGallons = highGallons * 3.785 ;  
                loGallons = lowGallons * 3.785 ;   
                /*cout << loGallons;
                cout <<hiGallons;*/
             
             }
        } while (!quitting);
        }  
    } while (!quitting);

    cout << "\nAborting; no conversion performed" << endl;
    cin.ignore();
    cin.get();
    return(0);
}
can you use break command. im new too just guessing
you have while(!quitting) while not quiting set as true but no way to modify it so it becomes false. add an input that asks if you want to quit.
why not just have it run the loop till that statement goes true...

1
2
3
     do{
        //code
    }while(!(foo >= bar);
1
2
3
cout << "\nEnter the highest gallon value to display <q to quit>: "; 
             cin >> highGallons;
        


so while(highGallons!='q')


same principle for lowGallons and try to go from there


theres a problem here in design why not put the else if (lowGallons >= 0 and lowGallons <=1000)exception first as an if and then make the illegal character exception an else

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (cin.fail())
             {
                cin.clear();
                cin >> charVal;
                if (charVal == 'q')
                quitting = true;
                else cout << "You entered an illegal character: ("
                << charVal << ")" << endl;
             } 
             
             else if (highGallons >=0 and highGallons <=1000)  ;
             {  
                hiGallons = highGallons * 3.785 ;  
                loGallons = lowGallons * 3.785 ;   
                /*cout << loGallons;
                cout <<hiGallons;*/
             



this too should be reworked, maybe you could find a solution that uses only one do while loop?
1
2
3
4
5
6
7
8
9
10
11
     if (cin.fail())
        {
           cin.clear();
           cin >> charVal;
           if (charVal == 'q')
           quitting = true;
           else cout << "You entered an illegal character: ("
           << charVal << ")" << endl;
        } 
        else if (lowGallons >= 0 and lowGallons <=1000)
        {    



thats the most I can come up with, sorry
Last edited on
Topic archived. No new replies allowed.