Loops to make 3 tables of Celsius, Fahrenheit and Kelvin

Ive gotten started on this hw homework problem due tonight but I am completely stumped.
Prompt:
Write a program that creates three tables of temperatures in Celsius, Fahrenheit and Kelvin. The program will prompt the user to enter the starting temperature, the ending temperature and the increment between values all in Celsius for each test case. The program will then convert Celsius to Fahrenheit AND Kelvin creating a table of temperatures. Your program will generate three tables - one for each type of loop.

TEST CASE LOOP TYPE START TEMP (C) END TEMP (C) INCREMENT (C)
ONE While Loop 10 50 5
TWO Do While Loop 25 250 25
THREE For Loop 0 100 10


Fahrenheit = 9.0/5.0 * Celsius + 32.0
Kelvin = Celsius + 273.15

Here is an example of the dialog between the program and user:

Prompt: Enter a starting temperature in degrees Celsius.
User enters: 10

Prompt: Enter an ending temperature in degrees Celsius.
User enters: 50

Prompt: Enter the increment between temperatures in degrees Celsius.
User enters: 5

Example output from the third test case.

FOR LOOP

Celsius Fahrenheit Kelvin

0.00 32.00 273.15
10.00 50.00 283.15
20.00 68.00 293.15
30.00 86.00 303.15
40.00 104.00 313.15
50.00 122.00 323.15
60.00 140.00 333.15
70.00 158.00 343.15
80.00 176.00 353.15
90.00 194.00 363.15
100.00 212.00 373.15

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

int main(void)
{
    double start, end, incr;
    double Celsius, Fahrenheit, Kelvin;

    cout << "Enter a starting temperature in degrees Celcius: " << endl;
    cin >> start, Celsius;
    cout << "Enter a ending temperature in degrees Celcius: " << endl;
    cin >> end, Celsius;
    cout << "Enter a ending temperature in degrees Celcius: " << endl;
    cin >> incr, Celsius;
}
This doesn't make much sense. If the input is 10 50 5, then why is the sample output from 0 to 100 by increments of 10? Is that output not for the sample input given?

Either way, the easiest way I see of doing this is just to make a loop, go through start to end by adding the increment. And for every increment, just use simple conversion formulas:

C to F is (C × 9/5) + 32 = F
C to Kelvin is C + 273.15 = K
a for loop is what you want.
the format is for(preconditions; condition; action)
for example from 1 to 10 is
for(int i = 1; i <= 10; i++)
----initialize i, loop until i is 10 or more, increment i

yours might look like
end += 0.00001; //a small hack to make sure you capture the endpoint due to floating point operations.
for(double tmp = start; tmp <= end; tmp+= incr)

this assumes start < end, whether positive or negative. You can swap start and end if they mess it up, or you can throw an error and do nothing :)
if you want to display negatives in a more human friendly way (instead of -100, -90, -80 you may want -80, -90, -100) then you need to add code to do that.

9/5 is exactly 1.8 ... not sure why they gave it like that.
Last edited on
Alright here's my code so far. This is how I am supposed to do the assignment, I just have an issue with an infinite loop. My output is just 32273.15 infinitely.
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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

int main(void)
{
    double start, end, incr;
    double Celsius, Fahrenheit, Kelvin;

    cout << "Enter a starting temperature in degrees Celsius: \n" << endl;
    cin >> Celsius >> start;
    cout << "Enter a ending temperature in degrees Celsius: \n" << endl;
    cin >> Celsius >> end;
    cout << "Enter the increment between temperatures in degrees Celsius: \n" << endl;
    cin >> Celsius >> incr;
    while(Celsius <= end)
    {
        Fahrenheit = 9.0/5.0 * Celsius + 32.0;
        Kelvin = Celsius + 273.15;
        cout << Fahrenheit << Kelvin << endl;
        incr++;
    }
    cout << "Enter a starting temperature in degrees Celsius: \n" << endl;
    cin >> Celsius >> start;
    cout << "Enter a ending temperature in degrees Celsius: \n" << endl;
    cin >> Celsius >> end;
    cout << "Enter the increment between temperatures in degrees Celsius: \n" << endl;
    cin >> Celsius >> incr;
    do 
    {
        Fahrenheit = 9.0/5.0 * Celsius + 32.0;
        Kelvin = Celsius + 273.15;
        cout << Fahrenheit << Kelvin << endl;
        incr++;
    }
    while (Celsius <= end);
    cout << "Enter a starting temperature in degrees Celsius: \n" << endl;
    cin >> Celsius >> start;
    cout << "Enter a ending temperature in degrees Celsius: \n" << endl;
    cin >> Celsius >> end;
    cout << "Enter the increment between temperatures in degrees Celsius: \n" << endl;
    cin >> Celsius >> incr;
    for(Celsius = start; Celsius <= end; Celsius = Celsius + incr)
    {
        Fahrenheit = 9.0/5.0 * Celsius + 32.0;
        Kelvin = Celsius + 273.15;
        cout << Fahrenheit << Kelvin << endl;
        incr++;
    } 

    return 0;
}

output is supposed to be:
1
2
3
4
5
6
7
8
9
10
11
12
13
Celsius Fahrenheit    Kelvin

  0.00        32.00       273.15
10.00        50.00       283.15
20.00        68.00       293.15
30.00        86.00       303.15
40.00      104.00       313.15
50.00      122.00       323.15
60.00      140.00       333.15
70.00      158.00       343.15
80.00      176.00       353.15
90.00      194.00       363.15
100.00     212.00      373.15

(Lined up in neat columns of course)
Last edited on
why do you read in celsius every cin statement? That can't be right.
line 18 is a loop that says

while(celsius <= end)
{
code that does not ever change celsuis
} //until celsius changed ... but it can't!
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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

int main(void)
{
    double start{0}, end{0}, incr{0};
    double celsius{0}, fahrenheit{0}, kelvin{0};

    cout << "Enter a starting temperature in degrees Celsius: ";
    cin >> start;
    cout << "Enter an ending temperature in degrees Celsius: ";
    cin >> end;
    cout << "Enter the increment between temperatures in degrees Celsius: ";
    cin >> incr;
    
    
    // LOOP TYPE 1
    celsius = start; // <--
    while(celsius <= end)
    {
        fahrenheit = 9.0/5.0 * celsius + 32.0;
        kelvin = celsius + 273.15;
        cout << celsius << '\t' << fahrenheit << '\t' << kelvin << endl;
        celsius += incr; // <--
    }
    
    // LOOP TYPE 2
    celsius = start;
    do
    {
        fahrenheit = 9.0/5.0 * celsius + 32.0;
        kelvin = celsius + 273.15;
        cout << celsius << '\t' << fahrenheit << '\t' << kelvin << endl;
        celsius += incr;
    }
    while (celsius <= end);

    // LOOP TYPE 3
    for(celsius = start; celsius <= end; celsius = celsius + incr)
    {
        fahrenheit = 9.0/5.0 * celsius + 32.0;
        kelvin = celsius + 273.15;
        cout << celsius << '\t' << fahrenheit << '\t' << kelvin << endl;
        //celsius += incr;
    }

    return 0;
}


Last edited on
I'm not really sure, how can I make the code to stop infinitely looping, ? I'm using a MacBook Pro and running VScode.
Last edited on
@againtry Nevermind I got it to work, thanks for the help!
Topic archived. No new replies allowed.