Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Unit conversion.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- using namespace std;
- int KMtoM ()
- {
- float km;
- cout << "What is your distance in kilometers? ";
- cin >> km;
- double miles = km * 0.6214;
- cout << endl << "Your distance of " << km << " kilometers is " << miles << " miles." << endl << endl;
- return 0;
- }
- int MtoKM ()
- {
- float miles;
- cout << "What is your distance in miles? ";
- cin >> miles;
- double km = miles * 1.609344;
- cout << endl << "Your distance of " << miles << " miles is " << km << " kilometers." << endl << endl;
- return 0;
- }
- int CtoF ()
- {
- float celsius;
- cout << "What is your temperature in Celsius? ";
- cin >> celsius;
- double fahr = (celsius * 1.8) + 32;
- cout << endl << "Your temperature of " << celsius << "\370 Celsius is " << fahr << "\370 Fahrenheit." << endl << endl;
- return 0;
- }
- int FtoC ()
- {
- float fahr;
- cout << "What is your temperature in Fahrenheit? ";
- cin >> fahr;
- float celsius = (fahr - 32) * (5.0f/9.0f);
- cout << endl << "Your temperature of " << fahr << "\370 Fahrenheit is " << celsius << "\370 Celsius." << endl << endl;
- return 0;
- }
- int Conversion ()
- {
- float choice = 0;
- do
- {
- do
- {
- cout << endl << "Which conversion would you like to perform (1-5)?" << endl << endl;
- cout << "1. Celsius -> Fahrenheit" << endl;
- cout << "2. Fahrenheit -> Celsius" << endl;
- cout << "3. Miles -> Kilometers" << endl;
- cout << "4. Kilometers -> Miles" << endl;
- cout << "5. Return to Main Menu" << endl << endl;
- cout << "? ";
- cin >> choice;
- } while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5);
- cout << endl << "You chose number: " << choice << endl << endl;
- if (choice == 1)
- CtoF ();
- else if (choice == 2)
- FtoC ();
- else if (choice == 3)
- MtoKM ();
- else if (choice == 4)
- KMtoM ();
- } while (choice != 5);
- return 0;
- }
- int GuessGame ()
- {
- cout << "This is a guessing game. You will attempt to guess a random whole number " << endl;
- cout << "between and including 1 and 100. You can keep guessing until you guess right." << endl << endl;
- int Secret;
- int Guess;
- int Number;
- srand ( time(NULL) );
- Secret = rand() % 100 + 1;
- do
- {
- cout << "Guess the number (1 to 100): ";
- cin >> Guess;
- if (Secret < Guess)
- {
- cout << "The secret number is lower." << endl;
- Number++;
- }
- else if (Secret > Guess)
- {
- cout << "The secret number is higher." << endl;
- Number++;
- }
- else
- Number++;
- } while (Secret != Guess);
- cout << endl << "Congratulations. You have guessed the correct number. It took you " << Number << " guesses.";
- return 0;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- float choice = 0;
- do
- {
- do
- {
- cout << endl << "Would you like to play a game or do some unit conversions (1-3)?" << endl << endl;
- cout << "1. Play a game" << endl;
- cout << "2. Do a conversion" << endl;
- cout << "3. Quit" << endl << endl;
- cout << "? ";
- cin >> choice;
- } while (choice != 1 && choice != 2 && choice != 3);
- cout << endl << endl;
- if (choice == 1)
- GuessGame ();
- else if (choice == 2)
- Conversion ();
- } while (choice != 3);
- return 0;
- }
Add Comment
Please, Sign In to add comment