Guest User

Untitled

a guest
Aug 16th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. // Unit conversion.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <time.h>
  9.  
  10. using namespace std;
  11.  
  12. int KMtoM ()
  13. {
  14. float km;
  15. cout << "What is your distance in kilometers? ";
  16. cin >> km;
  17. double miles = km * 0.6214;
  18. cout << endl << "Your distance of " << km << " kilometers is " << miles << " miles." << endl << endl;
  19. return 0;
  20. }
  21.  
  22. int MtoKM ()
  23. {
  24. float miles;
  25. cout << "What is your distance in miles? ";
  26. cin >> miles;
  27. double km = miles * 1.609344;
  28. cout << endl << "Your distance of " << miles << " miles is " << km << " kilometers." << endl << endl;
  29. return 0;
  30. }
  31.  
  32. int CtoF ()
  33. {
  34. float celsius;
  35. cout << "What is your temperature in Celsius? ";
  36. cin >> celsius;
  37. double fahr = (celsius * 1.8) + 32;
  38. cout << endl << "Your temperature of " << celsius << "\370 Celsius is " << fahr << "\370 Fahrenheit." << endl << endl;
  39. return 0;
  40. }
  41.  
  42. int FtoC ()
  43. {
  44. float fahr;
  45. cout << "What is your temperature in Fahrenheit? ";
  46. cin >> fahr;
  47. float celsius = (fahr - 32) * (5.0f/9.0f);
  48. cout << endl << "Your temperature of " << fahr << "\370 Fahrenheit is " << celsius << "\370 Celsius." << endl << endl;
  49. return 0;
  50. }
  51.  
  52. int Conversion ()
  53. {
  54. float choice = 0;
  55. do
  56. {
  57. do
  58. {
  59. cout << endl << "Which conversion would you like to perform (1-5)?" << endl << endl;
  60. cout << "1. Celsius -> Fahrenheit" << endl;
  61. cout << "2. Fahrenheit -> Celsius" << endl;
  62. cout << "3. Miles -> Kilometers" << endl;
  63. cout << "4. Kilometers -> Miles" << endl;
  64. cout << "5. Return to Main Menu" << endl << endl;
  65. cout << "? ";
  66. cin >> choice;
  67. } while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5);
  68. cout << endl << "You chose number: " << choice << endl << endl;
  69. if (choice == 1)
  70. CtoF ();
  71. else if (choice == 2)
  72. FtoC ();
  73. else if (choice == 3)
  74. MtoKM ();
  75. else if (choice == 4)
  76. KMtoM ();
  77. } while (choice != 5);
  78. return 0;
  79. }
  80.  
  81. int GuessGame ()
  82. {
  83. cout << "This is a guessing game. You will attempt to guess a random whole number " << endl;
  84. cout << "between and including 1 and 100. You can keep guessing until you guess right." << endl << endl;
  85. int Secret;
  86. int Guess;
  87. int Number;
  88. srand ( time(NULL) );
  89. Secret = rand() % 100 + 1;
  90. do
  91. {
  92. cout << "Guess the number (1 to 100): ";
  93. cin >> Guess;
  94. if (Secret < Guess)
  95. {
  96. cout << "The secret number is lower." << endl;
  97. Number++;
  98. }
  99. else if (Secret > Guess)
  100. {
  101. cout << "The secret number is higher." << endl;
  102. Number++;
  103. }
  104. else
  105. Number++;
  106. } while (Secret != Guess);
  107. cout << endl << "Congratulations. You have guessed the correct number. It took you " << Number << " guesses.";
  108. return 0;
  109. }
  110.  
  111. int _tmain(int argc, _TCHAR* argv[])
  112. {
  113. float choice = 0;
  114. do
  115. {
  116. do
  117. {
  118. cout << endl << "Would you like to play a game or do some unit conversions (1-3)?" << endl << endl;
  119. cout << "1. Play a game" << endl;
  120. cout << "2. Do a conversion" << endl;
  121. cout << "3. Quit" << endl << endl;
  122. cout << "? ";
  123. cin >> choice;
  124. } while (choice != 1 && choice != 2 && choice != 3);
  125. cout << endl << endl;
  126. if (choice == 1)
  127. GuessGame ();
  128. else if (choice == 2)
  129. Conversion ();
  130. } while (choice != 3);
  131. return 0;
  132. }
Add Comment
Please, Sign In to add comment