Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,100 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,680 people online right now. Registration is fast and FREE... Join Now!




Temperature Conversion

 
Reply to this topicStart new topic

Temperature Conversion, Conversion chart

Tara200
13 Oct, 2007 - 11:46 AM
Post #1

New D.I.C Head
*

Joined: 13 Oct, 2007
Posts: 39


My Contributions
CODE
#include <iostream>

  using namespace std;


  int Cels = 0;
  float Faren = Cels * 9/5 + 32;
  char celsius = celsius;
  char farenheit = farenheit;
  
  int main ()
{
  
   float  Faren = Cels * 9/5 + 32;
   cout << " celsius   farenheit" << endl;
   cout << "--------------------" << endl;
   cout << "  0                 " << endl;
   cout << "  5                 " << endl;
   cout << " 10                 " << endl;
   cout << " 15                 " << endl;
   cout << " 20                 " << endl;
   cout << " 25                 " << endl;
   cout << " 30                 " << endl;

}


Hi, i am new to C++ and have to print out a chart that converts the celsius values above to their equivalent farenheit numbers. The print out has to be in the form of a chart with celsius in one column and farenheit in the other. My question is i am not sure how to convert the celsius numbers above in code as the code i have written here prints out the celsius numbers (given numbers) in a list with celsius at the head of it and farenheit prints out as a title in the next column. but i don't know how to get the celsius numbers to convert to farenheit and print out
in the rest of the chart. Any help would be greatly received as this is an assignment that has to be in this Thursday blink.gif

thankyou!
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Temperature Conversion
13 Oct, 2007 - 12:38 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
tempinF = (9/5)*tempinC + 32

Apply this formula for each value.

You need to input the Celsius temperatures to that formula, like so:

CODE

cout<<"  0                 "<<(9/5)*0 + 32<<endl;

and so on...

As an FYI, your program will not compile with your current variable declarations.

A more efficient way to complete the program would be to either place the Celsius temperatures in an array and loop through it, doing the conversions, or step through a loop with an incrementer of 5, as you seem to have a constant increment, and convert that.
User is online!Profile CardPM
+Quote Post

1lacca
RE: Temperature Conversion
13 Oct, 2007 - 12:45 PM
Post #3

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 11 times
My Contributions
The "\t" (tabulator) escape character might help format the columns.
User is offlineProfile CardPM
+Quote Post

gsewell
RE: Temperature Conversion
13 Oct, 2007 - 01:03 PM
Post #4

New D.I.C Head
*

Joined: 12 Oct, 2007
Posts: 9


My Contributions
Dennis Ritchie, one of C's authors, addresses this subject in his exposition "The C Programming Language. Here is an example from section 1.2 which solves your mathematics as well as formatting issues:
CODE


#include <stdio.h>

main()
{
    float fahr, celsius;
    int lower, upper, step;

    lower = 0;
    upper = 300;
    step = 20;

    fahr = lower;
    while (fahr <= upper)
    {
        celsius = (5.0/9.0) * (fahr-32.0);
        printf("%3.0f %6.1f\n", fahr, celsius);
        fahr = fahr + step;
    }
}

Of course you can change the step and upper variables to suit your application.
-Gary

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Temperature Conversion
13 Oct, 2007 - 01:08 PM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
The code would also need to be translated to C++, which is of course easily done.

smile.gif
User is online!Profile CardPM
+Quote Post

Tara200
RE: Temperature Conversion
13 Oct, 2007 - 01:54 PM
Post #6

New D.I.C Head
*

Joined: 13 Oct, 2007
Posts: 39


My Contributions
CODE
#include <iostream>

  using namespace std;


  int tempinC = 0;
  int tempinF = tempinC * (9/5) + 32;
  char celsius = celsius;
  char farenheit = farenheit;
  
  int main ()
{
  
  
   cout << " celsius   farenheit " << endl;
   cout << "-------------------- " << endl;
   cout << "  0         " <<  0 *  9/5 + 32 << endl;
   cout << "  5         " <<  5 *  9/5 + 32 << endl;
   cout << " 10         " << 10 *  9/5 + 32 << endl;
   cout << " 15         " << 15 *  9/5 + 32 << endl;
   cout << " 20         " << 20 *  9/5 + 32 << endl;
   cout << " 25         " << 25 *  9/5 + 32 << endl;
   cout << " 30         " << 30 *  9/5 + 32 << endl;
  
                

}

Thanks Very , Very much !! i am much obliged , i have adjusted it to the above code and it compiles and runs fine! smile.gif
User is offlineProfile CardPM
+Quote Post

ahsesino
RE: Temperature Conversion
16 Oct, 2007 - 12:17 PM
Post #7

New D.I.C Head
*

Joined: 15 Oct, 2007
Posts: 10


My Contributions
QUOTE(Tara200 @ 13 Oct, 2007 - 02:54 PM) *

CODE
#include <iostream>

  using namespace std;


  int tempinC = 0;
  int tempinF = tempinC * (9/5) + 32;
  char celsius = celsius;
  char farenheit = farenheit;
  
  int main ()
{
  
  
   cout << " celsius   farenheit " << endl;
   cout << "-------------------- " << endl;
   cout << "  0         " <<  0 *  9/5 + 32 << endl;
   cout << "  5         " <<  5 *  9/5 + 32 << endl;
   cout << " 10         " << 10 *  9/5 + 32 << endl;
   cout << " 15         " << 15 *  9/5 + 32 << endl;
   cout << " 20         " << 20 *  9/5 + 32 << endl;
   cout << " 25         " << 25 *  9/5 + 32 << endl;
   cout << " 30         " << 30 *  9/5 + 32 << endl;
  
                

}

Thanks Very , Very much !! i am much obliged , i have adjusted it to the above code and it compiles and runs fine! smile.gif



Well here is another way to doi with a loop hope it helps you.


#include <iostream>
using namespace std;

int main()
{
double celsius;
double fahrenheit;

cout << "Fahrenheit to Celsius conversion." << endl;
cout << "\nCelsius: Fahrenheit:\n";
cout << "--------------------------\n";


for (fahrenheit = 0; fahrenheit <= 30; fahrenheit++)
{




cout << fahrenheit << "\t\t " << fahrenheit * 9/5 + 32 << endl;
}

return 0;

}
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:53PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month