Feel like im close, for loop is not displaying data

Hello -- I'm trying to output a conversion of liters to gallons, incremented by a variable which is determined based on the difference between two numbers (lowest and highest liters) entered by the user. The difference is not allowed to be more than 1000.

I'm sorry if my code is messy, I'm very new and this is essentially my first attempt at what is, to me, a complicated assignment.

My code will output the table as I expect it to, and my loops appear to be working correctly, until I hit the for loop near the very bottom of the code.


If I'm way off here then just tell me and I'll scrap it and go back to the drawing board. If I'm close, could someone help me out with why my for loop is not outputting data into my table?

Whatever info you might need to troubleshoot please just ask, thanks in advance!

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
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <iomanip>

using namespace std;
int
main ()
{
    const double LTG = 3.785;
    double highest, lowest, increment;
    char charVal;
    bool quitting;
    quitting = false;

    cout << "This program creates a gallons to liter conversion table.\n\n" << endl;

    do {
        cout << "Enter the lowest gallon value to display <q to quit>:";
        cin >> lowest;
        if (cin.fail())
        {
            cin.clear();
            cin >> charVal;
            if (charVal == 'q')
                quitting = true;
            else cout << "You entered an illegal character: (" << charVal << ")" << endl;
        }
        else
        {
            do
            {
                cout << "\nEnter the highest gallon value to display <q to quit>:";
                cin >> highest;
                if (cin.fail())
                {
                    cin.clear();
                    cin>> charVal;
                    if (charVal == 'q')
                        quitting = true;
                    else cout << "You entered an illegal character: (" << charVal << ")" << endl;
                }
                else if (lowest < 0 && highest < 0){
                cout << "Error low gallon value must not be  negative.";
                cout << "Error high gallon value must not be negative.";
                }
                else if (lowest < 0)
                cout << "Error low gallon value must not be  negative.";
                else if (highest < 0)
                cout << "Error: high gallon value must not be negative";
                else if (highest - lowest <= 10)
                increment = 1.0;
                else if (highest - lowest <= 20)
                increment = 5.0;
                else if (highest - lowest <= 50)
                increment = 10.0;
                else
                increment = 20.0;
                cout << showpoint << setprecision(1) << fixed << "\n\nThe conversion table will be created for the gallon range" << endl;
                cout << "of " << lowest << " to " << highest << " in increments of " << increment << endl;
                cout << setw(100) << "GALLONS TO LITERS" << endl;
                cout << setw(19) << "CONVERSION TABLE" << endl;
                cout << setw(10) << "Gallons" << "    " << "Liters" << endl;
                cout << setw(10) << "=======" << "    " << "=======" << endl;

                for (lowest; lowest >= highest; lowest + increment){
                        cout << lowest << (lowest * LTG);
                }
                quitting = true;
            } while (!quitting);
        }
        } while (!quitting);
    return (0);
}
I can't test your code at the moment but doesn't your for loop seems either impossible or infinite? I don't know if you meant

For (lowest; lowest <= highest; lowest + increment)

Or

For (lowest; lowest >= highest; lowest - increment)

But try it out. (The first option sounds better)
Hi MannedTooth thanks for the swift reply. I tried your suggestion and now have an infinite loop, i'll keep working on it.
The thing is, the way your loop is declared, it will never trigger since lowest is never higher than highest.

That's why I changed the comparaison sign.
Makes sense thank you MannedTooth, i've actually modified it to a while loop and am now getting proper data.... i think haha... now to format it correctly to see if it's right.

Here's what I changed it to:

while (lowest <= highest){
cout << lowest << (lowest * LTG);
lowest = lowest + increment;
}
Last edited on
It's working now thank you MannedTooth :)

while (lowest <= highest){
cout << setw(9) << lowest << setw(12) << (lowest * LTG) << endl;
lowest = lowest + increment;
}
That could have been done with a for loop, but for tonight I'm off. Glad you found a way to work it out and glad I could help. :)
Topic archived. No new replies allowed.