Celsius to Farenheit task

This is a discussion on Celsius to Farenheit task within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int main() { cout<< "Roll Cast's Celsius to Farenheit Converter\n Please enter the minimum ...

  1. #1
    Registered User roll cast's Avatar
    Join Date
    Nov 2011
    Location
    NI
    Posts
    9

    Celsius to Farenheit task

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout<< "Roll Cast's Celsius to Farenheit 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;
    }
    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.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    ... This is why the use of goto is discouraged ...

    What, a loop was too complicated for you?

    EDIT: And, remove that semicolon in line 18
    Last edited by GReaper; 11-03-2011 at 04:45 PM.
    Devoted my life to programming...

  3. #3
    Registered User roll cast's Avatar
    Join Date
    Nov 2011
    Location
    NI
    Posts
    9
    So I could use a loop and break the loop when the inputs are correct?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    Quote Originally Posted by roll cast View Post
    So I could use a loop and break the loop when the inputs are correct?
    That or loop while the inputs are wrong.
    Devoted my life to programming...

  5. #5
    Registered User roll cast's Avatar
    Join Date
    Nov 2011
    Location
    NI
    Posts
    9
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        double max, min;
        int steps;
        cout<< "RollCast's Celsius to Farenheit 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. #6
    Registered User roll cast's Avatar
    Join Date
    Nov 2011
    Location
    NI
    Posts
    9
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        double max, min;
        int steps;
        cout<< "RollCast's Celsius to Farenheit 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;
    }
    I edited the code to remove the break as I just realised that it is similar to goto

  7. #7
    Registered User roll cast's Avatar
    Join Date
    Nov 2011
    Location
    NI
    Posts
    9
    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 Farenheit 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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Celsius to Farenheit program
    By Cyberman86 in forum C++ Programming
    Replies: 2
    Last Post: 09-09-2008, 12:13 PM
  2. HDD Temperature 66 degrees celsius
    By abh!shek in forum Tech Board
    Replies: 15
    Last Post: 03-06-2008, 11:42 AM
  3. Replies: 2
    Last Post: 12-31-2007, 10:40 AM
  4. Converting Farenheit to Celcius: - Turbo C
    By EdwardElric in forum C Programming
    Replies: 11
    Last Post: 07-22-2007, 06:32 AM
  5. Function: same 'ol Farenheit to Celsius
    By MB1 in forum C++ Programming
    Replies: 2
    Last Post: 04-21-2005, 03:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21