
Infinitus
Veteran

Jun 29, 2006, 4:53 AM
Post #1 of 2
(473 views)
Shortcut
|
|
Fahrenheit to Celsius - Source Code
|
Can't Post
|
|
A program I wrote in C++ that allows a user to convert between Celsius and Fahrenheit and vice versa. The code isn't too complex, so read through it and learn something if you're still new to C++. Being able to read other people's code is equally important as being able to write it. ~edit~ Some of the formatting is screwed up because of this being a forum post. I have a lot more whitespace and indentation in my code than the forum shows. Sorry for the lessened readability. // Written by Rono - June 27, 2006#include <iostream> using namespace std; #include <iomanip> using std::setprecision;#include <string> int main() { double fahrenheitValue = 0; // The temperature in Fahrenheit double celsiusValue = 0; // The temperature in Celsius int unitChoice = 0; // Used to determine which the user wants to convert int whileLoop = 1; // Variable for the loop string badInput; // String used for any bad input handling from user while(whileLoop != 0) // Main loop for conversions { cout << "\nDo you want to convert Fahrenheit to Celsius or Celsius to Fahrenheit?\n\n"; cout << "Enter \"1\" for Fahrenheit to Celsius\n" ; cout << "Enter \"2\" for Celsius to Fahrenheit\n\n"; if(!(cin >> unitChoice)) // User enters choice (also error handling if user enters wrong data type) { cin.clear(); // Clears input stream cin >> badInput; // Stores bad input to a string to repair input stream cout << "\nInvalid data type entered!\n"; cout << "Please try again.\n" << endl; } else if(unitChoice == 1) //First conversion method - Fahrenheit to Celsius { cout << "\nYou have chosen to convert Farenheit to Celsius.\nPlease enter the temperature in Farenheit.\n"; if(!(cin >> fahrenheitValue))// Error handling again { cin.clear(); cin >> badInput; cout << "\nInvalid data type entered!\n"; cout << "Please try again.\n" << endl; } else // Conversion is performed for Fahrenheit to Celsius { celsiusValue = 5 * (fahrenheitValue - 32) / 9; // Conversion of fahrenheit to Celsius cout << "\nThe temperature for " << fahrenheitValue << " degrees Fahrenheit in Celsius is " << setprecision(4) << fixed << celsiusValue << " degrees.\n\n"; // Decimal precision is set to four places using fixed and setprecision from the std namespace and <iomanip> library cout << "To convert another temperature, enter \"1\". To end this program, enter \"0\".\n" << endl; cin >> whileLoop; // User enters whether or not they want to convert another temperature } } else if(unitChoice == 2) // Second conversion method - Celsius to Fahrenheit { cout << "\nYou have chosen to convert Celsius to Fahrenheit.\nPlease enter the temperature in Celsius.\n"; if(!(cin >> celsiusValue)) // Error handling again { cin.clear(); cin >> badInput; cout << "\nInvalid data type entered!\n"; cout << "Please try again.\n" << endl; } else // Conversion performed for Celsius to Fahrenheit { fahrenheitValue = 9 * celsiusValue / 5 + 32; // Conversion of Celsius to Fahrenheit cout << "\nThe temperature for " << celsiusValue << " degrees Celsius in Fahrenheit is " << setprecision(4) << fixed << fahrenheitValue << " degrees.\n\n"; cout << "To convert another temperature, enter \"1\". To end this program, enter \"0\".\n" << endl; cin >> whileLoop; } } else // If user enters an integer other than 1 or 2, the program loops and asks for another { cout << "You have entered an invalid choice.\n" << endl; whileLoop = 1; // Loop repeats } } return 0; }
(This post was edited by Infinitus on Jun 29, 2006, 4:55 AM)
|