Looping and order of operations help

Hello,
I am new to C++ and have been working through a bunch of problems for school. Im really stuck on a homework and Im hoping to get some guidance on what I should be doing.

The problem is converting celsius to Fahrenheit. I have created a loop and table and think I have my formulas worked out pretty well. However im really stuck on what to do with some added problems.

I need to have the user enter a start a stop temperature in Celsius which sets the parameters of the table. I think I got that part figured out, but this is where my trouble begins.

no matter which order the start and stop temperatures are entered, the lowest needs to be the start.

also negative numbers can be entered and the start temperature needs to round down to the nearest integer while the stop temperature needs to round up to the nearest integer.

I have tried a bunch of different stuff to figure this out but haven't had any luck. Any helpful advice would be greatly appreciated.

I was thinking of adding something like this for the order part

if (start > Stop) {
cout << Stop;
else
cout << Start;

and something like this for the rounding part

int = (int)(start + 0.5);
int = (int)(stop - 0.5);

Here is the code


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

using namespace std;
int main () {
    double C = 0;
    double Start;
    double Stop;
    double F;

    cout << fixed << showpoint;
    cout << endl;
    cout << "Enter Start Temp ";
    cin >> Start;
    cout << endl;
    
    cout << "Enter Stop Temperture";
    cin >> Stop;
    cout << endl;
    //Calulate
    if (Start < Stop){
        cout << Start;}
    else {
        cout << Stop;
    }

    cout << F << endl;

const int COL1 = 12;
const int COL2 = 12;
cout << right;
cout << setprecision(2);
cout << endl;
cout << setw(COL1 + COL2) << "Celsius to Fahrenheit Table" << endl;
cout << endl;
cout << setw(COL1) << "Celsuis" << setw(COL2) << "Fahrenheit" << endl;
cout << endl;

int table = 1;
while (table <= COL1 + COL2){
    cout << "=" ;
    table++;
}
cout << endl;

for (C = Start; C <= Stop; C++) {
    F = ((9.0 / 5.0) * C ) + 32;

    cout << setw(COL1) << C << setw(COL2) << F << endl;
    }


cout << endl;

    return 0;


}
Last edited on
Fix your code tags.
Did you not even bother to look at your post???
Yes, as dutch said you should fix your code tags first.

I'm not that good in C++ but I suggest you store the numbers input by the user in two different variables first, then compare them using if statement, the smaller one will be start, and the larger one will be stop.

I can't help you with the rounding part, but try to include the code you thought of into your program and run it to see for yourself, however you said start should be rounded down and stop should be rounded up, so the addition and subtraction of 0.5 should be the opposite.
@DJL,

PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/

HINT: you can edit your post and add code tags.
Sorry about that guys. I just fixed the code tags
Is that how you indent your code? A good format can help make a lot of simple mistakes easily apparent:

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

using namespace std;
int main()
{
   double C = 0;
   double Start;
   double Stop;
   double F;

   cout << fixed << showpoint;
   cout << endl;
   cout << "Enter Start Temp ";
   cin >> Start;
   cout << endl;

   cout << "Enter Stop Temperture";
   cin >> Stop;
   cout << endl;
   //Calulate
   if (Start < Stop)
   {
      cout << Start;
   }
   else
   {
      cout << Stop;
   }

   cout << F << endl;

   const int COL1 = 12;
   const int COL2 = 12;
   cout << right;
   cout << setprecision(2);
   cout << endl;
   cout << setw(COL1 + COL2) << "Celsius to Fahrenheit Table" << endl;
   cout << endl;
   cout << setw(COL1) << "Celsuis" << setw(COL2) << "Fahrenheit" << endl;
   cout << endl;

   int table = 1;
   while (table <= COL1 + COL2)
   {
      cout << "=";
      table++;
   }
   cout << endl;

   for (C = Start; C <= Stop; C++)
   {
      F = ((9.0 / 5.0) * C) + 32;

      cout << setw(COL1) << C << setw(COL2) << F << endl;
   }


   cout << endl;

   return 0;


}

Line 32, using an unitialized variable (F). Visual Studio 2019 flags that as an error and stops compilation.
For rounding, I suggest you look into std::round()
https://en.cppreference.com/w/cpp/numeric/math/round
Hello DJL,

Taking into account what Furry Guy said and TheToaster makes a good point about "found" I came up with this:
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
74
75
76
77
78
79
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	const int COL1 = 12;
	const int COL2 = 12;

	double celsius = 0;
	double start{};
	double stop{};
	double fahrenheit{};

	std::cout << std::fixed << std::setprecision(2);  // <--- Only needs done once.

	//cout << endl;
	cout << "\nEnter start Temp ";
	cin >> start;

	start = floor(start);

	//cout << endl;

	cout << "\nEnter stop Temperture: ";
	cin >> stop;

	stop = ceil(stop);

	//cout << endl;

	//Calulate  // <--- What does this calculate?
	if (start > stop)
	{
		//cout << "\n start Temp: " << start <<'\n';  // <--- What good is this?
		cout << "\n     start Temp is greater than stop Temp!\n\n Leaving program. Try again.\n";

		return 1;
	}
	//else
	//{
	//	cout << "\n stop temp: "<< stop << '\n';  // <--- What good is this?
	//}

	// <--- Is there a point to this line. Or is it used for testing?
	//cout << "\n Value of fahrenheit: " << fahrenheit << endl;  // <--- What is the value of "fahrenheit" here? Unknown it is an uninitialized variable and it is a compile error for me.


	//cout << right;  // <--- right is the default unless changed.
	//cout << setprecision(2);  // <--- Already done.
	cout
		<< setw(COL1 + COL2) << "Celsius to Fahrenheit Table\n"
		<< setw(COL1) << "Celsuis" << setw(COL2 + 4) << "Fahrenheit\n"  // <--- Changed "setw"
		<< endl;

	//int table = 1;

	//while (table <= COL1 + COL2)  // <--- Endless loop. table always greater than (0) zero.
	//{
	//	cout << "=";
	//	table++;
	//}

	//cout << endl;

	for (celsius = start; celsius <= stop; celsius++)
	{
		fahrenheit = ((9.0 / 5.0) * celsius) + 32;

		cout << setw(COL1) << celsius << setw(COL2) << fahrenheit << endl;
	}

	cout << endl;

	return 0;
}

Sorry it looks a mess, but I wanted you to see what you do not need. When you take out the code that is commented out and not needed it looks much better.

I used "floor" and "ceil" because that is what I thought of first, but you could just as easily use "round".

I changed the if statement at line 36 because it makes more sense.

You could put lines 21 - 42 in a do/while loop and exit the loop when start is less than stop.

The code works for both negative and positive numbers.

A note on variables. Regular variables generally start with a lower case letter. Classes and structs start with a capital letter. A variable in all upper case letters tends to mean that it is defined as a constant.

Also try to avoid single letter variable names. Give them a proper name that describes what it is or id used for.

The above code reflects this and you can see how it it easier to understand.

Andy
Andy,

Thanks so much for that feedback. It looks like I have defiantly been way over complicating the program and adding a ton of things that were not necessary. That may have come from the hope of covering all my basis but ended up just creating a confusing cluster.

I will take all this into to account and come at the problem fresh.
Hello DJL,

There is no need to rewrite the program. Start by removing what I put a comment on then look at what is left.

I will not say the what is left is the best code. It could be improved on, but it is a good place to start. And it does work.

Sometimes the hard part is learning to use what you have already written better.

Andy
So Im still trying to create the condition that regardless of what the user enters first the lowest number always starts the sequence. Ideally I would want this to happen automatically without the user needing to re enter the start and stop order.

This is the equation I put in which sorta works, however if the user puts in a larger number first only one result is created. So im unsure on what I need to do the switch the start and stop when start is entered as a larger number than stop.

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
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	const int COL1 = 12;
	const int COL2 = 12;

	double celsius = 0;
	double start;
	double stop;
	double fahrenheit;

	cout << fixed << setprecision(2);  


	cout << "Enter start Temp " << endl;
	cin >> start;

	start = floor(start);


	cout << "Enter stop Temperture " << endl;
	cin >> stop;

	stop = ceil(stop);

	// need to figure out how to create a loop here that makes the lowest number the start number regarless if entered first.
	if (start < stop){
		start = start;}
		else if (start > stop){
			start = stop;
		}
	

	

	
	cout
		<< setw(COL1 + COL2) << "Celsius to Fahrenheit Table" << endl
		<< setw(COL1) << "Celsuis" << setw(COL2 + 4) << "Fahrenheit "  << endl; 
		


	for (celsius = start; celsius <= stop; celsius++)
	{
		fahrenheit = ((9.0 / 5.0) * celsius) + 32;

		cout << setw(COL1) << celsius << setw(COL2) << fahrenheit << endl;
	}

	cout << endl;

	return 0;
}

1
2
3
4
5
6
7
8
9
10
11
    cout << "Enter start Temp: ";
    cin >> start;

    cout << "Enter stop Temp: ";
    cin >> stop;
    
    if (stop < start)
        swap(start, stop);

    start = floor(start);
    stop = ceil(stop);

dutch,

Thanks, I wasn't aware there was a swap function. That just made it super simple.
You can always make your own swap like this:

1
2
3
double temp = start;
start = stop;
stop = temp;

But it's so common that there's a standard function for it.
Topic archived. No new replies allowed.