Chris Pollett>Old Classes>PIC 10a, Spring 2000>Hw1>Hw1 Solutions

Spring 2000 PIC 10a HW1 Solutions Page

/* Program Name: pic10ahw1.cpp
 *
 *
 * Purpose:
 * ========
 * Perform conversions.  Present the user with a menu of four
 * conversion options:  slugs <-> kilograms and Kelvin <-> Farenheit.
 * Ask the user which of the conversions to do, ask for an input
 * value in any of the four units, and convert to the corresponding
 * unit.  Also suggests eating more if the weight is <= 22.275 kg
 * and wearing a sweater if the temperature is < 0 deg. F.  After
 * conversion, program exits.  Uses the following conversions:
 *
 *  1 Slug = 15.594 kg.
 *  x deg. K = 9(x-273)/5 + 32 deg. F
 *
 * Known Bugs: none
 * ===========
 */

#include <iostream.h>

int main()
{
	double		weight_in, weight_out ;	// Input, output weights
	double		slugs2kg = 15.594 ;	// Slugs -> kg. conversion
											// factor

	int			temp_in ;	// Input temperature
	double		temp_out ;		// Output temperature

	int			choice ;

	// Which conversion?
	cout << "What would you like to convert?\n" ;
	cout << "1.  Slugs to kilograms.\n" ;
	cout << "2.  Kilograms to slugs.\n" ;
	cout << "3.  Degrees Kelvin to degrees Farenheit.\n" ;
	cout << "4.  Degrees Farenheit to degrees Kelvin.\n" ;

	cout << "Enter your choice -> " ;
	cin >> choice ;

	if (choice == 1) {
		// Slugs -> Kilograms
		cout << "How many slugs do you weigh? " ;
		cin >> weight_in ;
		weight_out = weight_in*slugs2kg ;
		cout << "You weigh " << weight_out << " kilograms.\n" ;
		if (weight_out <= 22.725) cout << "You need to eat more!\n" ;
	}
	else if (choice == 2) {
		// Kilograms -> Slugs
		cout << "How many kilograms do you weigh? " ;
		cin >> weight_in ;
		weight_out = weight_in/slugs2kg ;
		cout << "You weigh " << weight_out << " slugs.\n" ;
		if (weight_in <= 22.725) cout << "You need to eat more!\n" ;
	}
	else if (choice == 3) {
		// Kelvin -> Farenheit
		cout << "What is the temperature in degrees Kelvin? " ;
		cin >> temp_in ;
		temp_out = 9*(temp_in-273)/5.0+32 ;
		cout << "That is " << temp_out << " degrees Farenheit.\n" ;
		if (temp_out < 0) cout << "You should probably wear a sweater today.\n" ;
	}
	else if (choice == 4) {
		// Farenheit -> Kelvin
		cout << "What is the temperature in degrees Farenheit? " ;
		cin >> temp_in ;
		temp_out = 5*(temp_in-32)/9.0 + 273 ;
		cout << "That is " << temp_out << " degrees Kelvin.\n" ;
		if (temp_in < 0) cout << "You should probably wear a sweater today.\n" ;
	}
	else {
		cout << "That is not one of the choices!\n" ;
	}

	return 0 ;
}