How can I improve my Celsius to Fahrenheit Converter code?

  • Thread starter rollcast
  • Start date
  • Tags
    celsius
In summary: The conversation is about creating a temperature converter from celsius to fahrenheit and making sure the inputs are valid. In summary, the conversation includes the use of a while loop, if statements, and the importance of avoiding the use of goto and break in coding.
  • #1
rollcast
408
0
Code:
#include <iostream>

using namespace std;

int main()
{
    cout<< "Roll Cast's Celsius to Fahrenheit Converter\n Please enter the minimum temperature in celsius\n";
    double min;
    cin>> min;
    jump:
    cout<< "Please enter the maximum temperature in celsius\n";
    double max;
    cin>> max;
    cout<< "Please enter the number of steps for the temperature range, non zero integer\n";
    int steps;
    cin>> steps;

    if (min > max);
    {cout<< "Your values for max. and min. temperature are impossible please re-enter them.\n";
    goto jump;
    }


    cout<<"Press ENTER to continue\n";
    cin.get();
    cin.ignore();
    return 0;
}


I found a problem on another website to make a temperature converter, http://www.cprogramming.com/challenges/celsius_converter_table.html .

This is the code I have written so far, I haven't looked at the correct solution yet, this basically collects the users inputs and makes sure they are possible, ie. min < max. However when it gets to the goto and it re asks for the min and max it doesn't ask for the no. of steps and just goes into a loop by asking for the 2 inputs over and over.
 
Technology news on Phys.org
  • #2
rollcast said:
However when it gets to the goto and it re asks for the min and max it doesn't ask for the no. of steps and just goes into a loop by asking for the 2 inputs over and over.

That should tell you there is something wrong with the code that tests if min and max are valid, and indeed there IS something wrong with those lines of code.

Hint: there's something in your code that shouldn't be there, but I'm not going to tell you what it is, because you will learn more by finding it yourself.

You don't say what compiler you are using, but a good quality C or C++ compiler would probably give you a warning about what you did wrong, since it's "legal", but very rarely "useful".
 
  • #3
I'm using code:blocks to compile the code.

I know the ; after if (min > max) shouldn't be there?
 
  • #4
Also would using a loop be a better practice than using goto?
 
  • #5
Code:
#include <iostream>

using namespace std;

int main()
{
    double max, min;
    int steps;
    cout<< "RollCast's Celsius to Fahrenheit Converter \n";
    do
    {
    cout<< "Please enter the max. temp. in celsius.\n";
    cin>> max;
    cout<< "Please enter the min. temp. in celsius\n";
    cin>> min;
    cout<< "Please enter the number of steps for the range\n";
    cin>> steps;
    if (min <= max && steps >= 0)
    break;
    cout<< "The values you have entered are wrong please check them and re-enter\n";
    }
    while ((min > max || steps < 0));
    cout<< "Press ENTER to continue";
    cin.get();
    cin.ignore();
    return 0;
}

I changed the goto for a do while loop and have added conditionals which ensures that min is always less than or equal to max and that the number of steps between min and max is always a number greater than zero.
 
  • #6
Gotos are generally frowned upon. Often people will flatly say you should never use them. However, I feel a lot of people just say that because they hear others say it. In either case, they do lead to bad practices, and are rarely necessary. In my opinion you shouldn't use them until you understand the problems they lead to, and can justify the use.

Break is just a goto in disguise. As with goto, there are cases where it is justifiable to use it. I don't think this is such a case though. I recommend you attempt to reorganize your code so break isn't needed.

Also, unless it is just a formatting issue with the forum, loops should be indented. Get in the habit of indenting everything that is wrapped in curly braces.
 
  • #7
Thanks Dale, I think the indent was a formatting issue. I change the conditions in the if so I could remove the remove the break from the loop.

Code:
#include <iostream>

using namespace std;

int main()
{
    double max, min;
    int steps;
    cout<< "RollCast's Celsius to Fahrenheit Converter \n";
    do
    {
        cout<< "Please enter the max. temp. in celsius.\n";
        cin>> max;
        cout<< "Please enter the min. temp. in celsius\n";
        cin>> min;
        cout<< "Please enter the number of steps for the range\n";
        cin>> steps;
        if (min > max || steps < 0)
        {
            cout<< "The values you have entered are wrong please check them and re-enter\n";
        }

    }
    while ((min > max || steps < 0));
    cout<< "Press ENTER to continue";
    cin.get();
    cin.ignore();
    return 0;
}
 
  • #8
I have finished the conversion and step part of the code but I think I have used a number of work around which are probably not very good practice?

Code:
#include <iostream>

using namespace std;

int main()
{
    double maxc, minc, maxf, minf, stepc, stepfv, stepcv;
    int steps;
    cout<< "RollCast's Celsius to Fahrenheit Converter \n";
    do
    {
        cout<< "Please enter the max. temp. in celsius.\n";
        cin>> maxc;
        cout<< "Please enter the min. temp. in celsius\n";
        cin>> minc;
        cout<< "Please enter the number of steps for the range\n";
        cin>> steps;
        if (minc > maxc || steps < 0)
        {
            cout<< "The values you have entered are wrong please check them and re-enter\n";
        }

    }
    while ((minc > maxc || steps < 0));

    if (steps == 0)
    {
        maxf = 1.8 * maxc + 32;
        minf = 1.8 * minc + 32;
        cout<< "Max. Temp. in F ="<< maxf <<endl;
        cout<< "Min. Temp. in F ="<< minf << endl;
    }

    else
    {
        int stepdiv = steps += 1;

        for (int i = 0; i < steps; i++)
        {
            if (i == 0)
            {
                maxf = 1.8 * maxc +32;
                cout<< "Max. Temp. in F ="<< maxf <<endl;
            }
            if (i != 0)
            {
                stepc = (maxc - minc)/ stepdiv;
                stepcv = maxc - (stepc * i);
                stepfv = 1.8 * stepcv + 32;
                cout<< "Step in F ="<< stepfv<< endl;
            }
            if (i + 1 == steps)
            {
                minf = 1.8 * minc + 32;
                cout<< "Min. Temp. in F ="<< minf <<endl;
            }
        }
    }

    cout<< "Press ENTER to continue";
    cin.get();
    cin.ignore();
    return 0;
}
 
  • #9
Why are you doing this?
Code:
int stepdiv = steps += 1;
I'm not saying it's wrong, but the reason for doing this is not obvious to me, so a comment would be helpful to explain your reasoning.

Code:
        for (int i = 0; i < steps; i++)
        {
            if (i == 0)
            {
                maxf = 1.8 * maxc +32;
                cout<< "Max. Temp. in F ="<< maxf <<endl;
            }
            if (i != 0)
            {
                stepc = (maxc - minc)/ stepdiv;
                stepcv = maxc - (stepc * i);
                stepfv = 1.8 * stepcv + 32;
                cout<< "Step in F ="<< stepfv<< endl;
            }
            if (i + 1 == steps)
            {
                minf = 1.8 * minc + 32;
                cout<< "Min. Temp. in F ="<< minf <<endl;
            }
        }

The for loop above could be changed as follows, and should behave the same. Note that this for loop executes one time less.
Code:
        for (int i = 0; i < steps - 1; i++) // Revision here.
        {
            if (i == 0)
            {
                maxf = 1.8 * maxc +32;
                cout<< "Max. Temp. in F ="<< maxf <<endl;
            }
            else  // This branch executes if i != 0.
            {
                stepc = (maxc - minc)/ stepdiv;
                stepcv = maxc - (stepc * i);
                stepfv = 1.8 * stepcv + 32;
                cout<< "Step in F ="<< stepfv<< endl;
            }
         }
         // Moved this code outside of loop.
         minf = 1.8 * minc + 32;
         cout<< "Min. Temp. in F ="<< minf <<endl;
 
  • #10
Looks good overall. A few things I would note:
First, did you really mean to do this:
int stepdiv = steps += 1;

Break that apart into an assignment and increment statement if you really want to do that. It seems you could accomplish the same thing by changing your for loop to be <=

It seems like those three ifs near the bottom could be else if or nested.

No comments. While you don't need to comment every line, there should be some comments. I like to put comments on the opening curly brace lines {, to explain what that block does. This let's me fold the code up but still see what the blocks do.

Your variables aren't very descriptive. Frankly, I'm guilty of the same thing. Remember that it is easy for you to remember what they represent because you invented them. If you ever want to write code that will be read by others (eg professionally) you should get into the habit of writing out long descriptive names. When you write code that has to be maintained the bulk of its life is spent being read, not written. Therefore, it's best to take more time to write the code if it lessens the time to read it later.

If you plan to mostly write one time use code for yourself then maybe you can keep with the short variable habit. I like to add a comment to each variable deceleration line to explain what it is.
 

Related to How can I improve my Celsius to Fahrenheit Converter code?

1. How do I convert Celsius to Fahrenheit?

To convert Celsius to Fahrenheit, use the formula: (°C x 9/5) + 32 = °F. First, multiply the temperature in Celsius by 9/5, then add 32 to the result. This will give you the temperature in Fahrenheit.

2. Can I use a calculator to convert Celsius to Fahrenheit?

Yes, you can use a calculator to convert Celsius to Fahrenheit. Simply input the Celsius temperature, multiply it by 9/5, and then add 32 to the result. This will give you the temperature in Fahrenheit.

3. Why do we use different temperature scales?

Different temperature scales are used because they were developed by different scientists at different times. Celsius is used in most countries as the standard unit of temperature, while Fahrenheit is still used in the United States and a few other countries.

4. What is the freezing point of water in Fahrenheit?

The freezing point of water in Fahrenheit is 32 degrees. This means that at 32 degrees Fahrenheit, water will freeze and turn into ice.

5. Is there a quick way to estimate the conversion from Celsius to Fahrenheit?

Yes, there is a quick way to estimate the conversion from Celsius to Fahrenheit. Simply take the temperature in Celsius, double it, and then add 30. This will give you a rough estimate of the temperature in Fahrenheit. For example, if the temperature is 20 degrees Celsius, doubling it gives you 40, and adding 30 gives you an estimated temperature of 70 degrees Fahrenheit.

Similar threads

  • Programming and Computer Science
Replies
30
Views
2K
  • Programming and Computer Science
Replies
2
Views
893
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
Replies
10
Views
965
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
18
Views
10K
Back
Top