Temperature Converter with a primed loop

Feb 26, 2017 at 7:44pm
Hi there. I am currently working on this assignment and am currently lost. I would appreciate anyone help me get to the right direction.

Math equations for the temperatures:
Kelvin - F = (K - 273.15) * 1.8000 + 32

Celsius - F = C * 9/5 + 32

Newton - F = N * 60 / 11 + 32

Required:
You are not allowed to use a do-while loop for this program.

Use a while loop in conjunction with a switch statement. Please review the information on primed loops.

You must prime your loop with input from the keyboard before the while loop starts.

The program should continue converting temperatures until X is entered as a conversion type.

The output look something like this:
This temperature conversion program converts other temperature types to Fahrenheit.
The temperature types are:
C - Celsius
K - Kelvin
N - Newton
X - eXit

To use the converter, you must input a value and one of the temperature types.
For example, 32 C converts 32 degrees from Celsius to Fahrenheit

Please enter a value and its type to be converted
38 C
38C is 100.4 in Fahrenheit.
Please enter a value and its type to be converted
100 K
100K is -279.67 in Fahrenheit
Please enter a value and its type to be converted
0 X
Press any key to continue . . .

This is my code so far.
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
#include <iostream>

using namespace std;

int main()
{
	char temp;
	double value;


	cout << "This temperature conversion program converts other temperature types to Fahrenheit" << endl;
	cout << "The temperature types are: \nC - Celsius\nK - Kelvin\nN - Nexon\nX - eXit" << endl << endl;

	cout << "To use this converter, you must input a value and one of the temperature types." << endl;
	cout << "For example, 32 C converts to 32 degrees from Celsius to Fahrenheit" << endl << endl;

	cout << "Please enter a value and its type to be converted" << endl;
	cin >> value;
	cin >> temp;

	while(temp != X)


	return 0;
}
Feb 26, 2017 at 9:32pm
Hi, Sentoo.
One possible solution could be to decide what should stay inside the while loop.
For example, should the explanatory text be repeated every time? If yes, it should be included in the while.

Inside the while you could evaluate what the user has chosen, which is in the variable “temp”.
So:
switch(temp)
To “react” to the user input, you need cases:
1
2
case ‘C’:
... 

Don’t forget to end every “case” with a “break”!
In every case you could invoke a different function, sending “value” as parameter. Each function could perform a different computation.
Collecting the result in a variable, you can “cout” it to the user.

Having said that, what follows is one possible solution.


* * *
SPOILER! :-)
Do not read any more if you want to solve the code by yourself.
* * *






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
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

double fromKelvin(double kelv);
double fromCelsius(double cels);
double fromNewton(double newt);

int main()
{
    cout << endl << endl;
    cout << "This temperature conversion program converts other temperature "
            "types to Fahrenheit" << endl;
    cout << "The temperature types are: \nC - Celsius\nK - Kelvin\nN - "
            "Nexon\nX - eXit" << endl << endl;
    cout << "To use this converter, you must input a value and one of the "
            "temperature types." << endl;
    cout << "For example, 32 C converts to 32 degrees from Celsius "
            "to Fahrenheit" << endl << endl;

    char temp = 'A';
    while(temp != 'X') {
        cout << "\nPlease enter a value and its type to be converted: ";
        double value=0, answer=0;
        cin >> value;
        cin >> temp;
        switch(temp) {
            // Celsius to Fahrenheit
            case 'c':
            case 'C':
                answer = fromCelsius(value);
                break;
            // Kelvin to Fahrenheit
            case 'k':
            case 'K':
                answer = fromKelvin(value);
                break;
            // Newton to Fahrenheit
            case 'n':
            case 'N':
                answer = fromNewton(value);
                break;
            // default (include 'X')
            default:
                break;
        }

        cout << value << temp << " is " << answer << " in Fahrenheit." << endl;
    }

    return 0;
}

double fromKelvin(double kelv)
    { return (kelv - 273.15) * 1.8000 + 32; }

double fromCelsius(double cels)
    { return cels * 9/5 + 32; }

double fromNewton(double newt)
    { return newt * 60 / 11 + 32; }


Last edited on Feb 26, 2017 at 9:33pm
Feb 26, 2017 at 11:02pm
Thank you! I really appreciate your work and time spent on trying to help me. I have been trying to do my homework for quite a few hours now so I didn't think twice about copy pasting your code. It worked fine and the way it should be. Even though I didn't really do it all myself, I think I learned from your code. Thanks again.
Topic archived. No new replies allowed.