#Homework Help with Temperature Conversion

8 messages · Page 1 of 1 (latest)

proper coralBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

abstract herald
#

do your homework by yourself

smoky star
#

!f

proper coralBOT
#
// Prompt the user to choose: 1) Centigrade to Fahrenheit, 2) Fahrenheit to
// centigrade. Prompt the user for temp to convert. Calculate new temp2. The
// calculation of the new temp2 depends on choice 1 or 2 above. Print out temp2.
// Be sure to use the C and F temp notation.  (Hint: use decimal data types)

#include <iomanip>   //Set precision for decimal points
#include <iostream>  //For cout, cin libraries
int main() {
  int temp1;         // Initialize temperature
  int response = 0;  // Initialize option for conversion

  std::cout
      << "Please enter 1 to convert Centigrade to Fahrenheit or 2 to convert "
         "Fahrenheit to centigrade: ";  // Prompt user to choose Fahrenheit or
                                        // Fahrenheit to centigrade
  std::cin >> response;  // Stores input into response

  std::cout
      << "Please enter temperature you want to convert: ";  // Prompt user to
                                                            // choose Fahrenheit
                                                            // or Fahrenheit to
                                                            // centigrade
  std::cin >> temp1;  // Store first integer

  if (response == 1) {
    temp1 = (temp1 * 9 / 5.0) + 32;
    std::cout << std::setprecision(2) << temp1 << "F" << std::endl;

  } else {
    std::cout << float((temp1 - 32) * 5 / 9) << "C" << std::endl;
  }
}

//Hello, the calculation seems to be right, but I'm having a little issue with outputting a number with decimal points. I've tried using setprecision from the <<iomanip>> library as well as float, what am I doing wrong?

buk
smoky star
#

integers can't hold decimals

dry cradle
#

ah my initialized variable

#

thanks

proper coralBOT
#

@dry cradle

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.