conversion program

Discussion in 'C++' started by chemr3, Feb 16, 2009.

  1. chemr3

    chemr3 New Member

    Joined:
    Feb 16, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello all. I have been trying to write a program that converts degrees fahrenheit to celsius.
    The way I have the program written now when you hit run it will just calculate conversions up to the max_fahrenheit = 100. I want to be able to ask the person using the program to enter the number of conversions they want to see. I have put a text block around the code that asks for input below. I need to figure out how to take that input and give the user back the number of conversions they want. Any help would be greatly appreciated.
    Code:
    #include<iostream>
    #include<iomanip>
    using namespace std;
     
    int main()
    {
        double celsius;
        int fahrenheit;
        const int far_value = 20;
        const int far_step = 4;
        int max_fahrenheit=100;
        int conversions;
     
        cout<<"This program is designed to convert values from fahrenheit to celsius\n"<<endl;
     
    /*cout<<"Please enter the number of conversions\n"<<endl;
        cin>>conversions;
    */
     
     
        fahrenheit = far_value;
     
        cout<< setiosflags(ios::showpoint)
            << setprecision(2);
     
        cout<< "\n Degrees       Degrees\n"
            << "Fahrenheit     Celsius\n\n";   
     
        while (fahrenheit <= max_fahrenheit)
         { 
     
     
              celsius =  (fahrenheit - 32.0) / (9.0/5.0);
     
              cout<< setw(5) << fahrenheit << fixed
                  << setw(15)<< celsius << endl;
     
     
              fahrenheit = (fahrenheit + far_step);
     
     
         }
     
          system("pause");
          return 0;
    }
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Put a for loop around the code you want to repeat:
    Code:
    cin<num_conversions;
    for (int i=0; i<num_conversions; i++)
    {
      // the code you want to repeat
    }
    
     
  3. chemr2

    chemr2 New Member

    Joined:
    Feb 16, 2009
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    Thanks so much
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice