Can I code this better?

Few questions here, I thought I could use Nested else if statements here but I'm trying to go exactly what the program tells me to do. I may of over-complicated this program but I am looking for if people here can tell me if

1: I did my program right
2: Can my coding style be better with nested-if statements. Thank you.

--------------------------------------

Problem: The following table lists the freezing and boiling points of several substances. Write a
program that asks the user to enter a temperature and then shows all the substances
that will freeze at that temperature and all that will boil at that temperature. For example,
if the user enters −20 the program should report that water will freeze and oxygen
will boil at that temperature.

Substance Freezing Point (°F) Boiling Point (°F)
Ethyl alcohol -173 172
Mercury -38 676
Oxygen -362 -306
Water 32 212

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

int main()
{
	int Temperature;

	cout << "Please enter the Temperature!\n";
	cin >> Temperature;

	cout << "__________________________\n";

	cout << "Temperature that will Freeze!\n";
	cout << "____________________\n";
	
	if (Temperature < -173 && Temperature > -362)
	{
		cout << "Ethyl, Mercury, and Water will Freeze!\n";
	}
	else if (Temperature < -362)
	{
		cout << "Everything will Freeze!\n";
	}
	else if (Temperature < -38)
	{
		cout << "Mercury and Water will Freeze\n";
	}
	else if (Temperature < 32 && Temperature > -38)
	{
		cout << "Only Water will Freeze\n";
	}
	
	cout << "Temperature that will Boil!\n";
	cout << "______________________\n";

	if (Temperature > 212 && Temperature < 676)
	{
		cout << "Ethyl, Oxygen, and Water will Boil!\n";
	}
	else if (Temperature > 676)
	{
		cout << "Everything will Boil!\n";
	}
	else if (Temperature > -306 && Temperature < 172)
	{
		cout << "Oxygen will Boil\n";
	}
	else if (Temperature > 172 && Temperature < 212)
	{
		cout << "Oxygen and Ethyl will Boil\n";
	}

	return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct substance{
	double fusion_temp, ebullition_temp;
	std::string name;
	substance(std::string name, double fusion_temp, ebullition_temp):
		name(name),
		fusion_temp(fusion_temp),
		ebullition_temp(ebullition_temp)
	{}
};

int main(){
	substance substances[] = {
		{"Ethyl alcohol", -173, 172}
		{"Mercury", -38, 676}
		{"Oxygen", -362, -306}
		{"Water", 32, 212}
	};

	for(auto &s: substances)
		if(temperature < s.fusion_temp)
			std::cout << s.name << "will freeze\n";
}
Last edited on
Topic archived. No new replies allowed.