Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. Programming Problem 4 in Chapter 5 asked you write a C++ program that asks the user to enter his or her weight and the name of a planet. In Chapter 7, Programming Problem 2 asked you to rewrite the program using a Switch statement. Now, rewrite the program so it uses an enumerated type to represent the planet.
  2.  
  3. For ease of reference, the information for the original problem is repeated here. The following table gives the factor by which the weight must be multiplied for each planet. The program should output an error message if the user doesnโ€™t input a correct planet name. The prompt and the error message should make it clear to the user how a planet name must be entered. Be sure to use proper formatting and appropriate comments in your code. The output should be labeled clearly and formatted neatly.
  4.  
  5.  
  6.  
  7. Mercury 0.4155
  8. Venus 0.8975
  9. Earth 1.0
  10. Moon 0.166
  11. Mars 0.3507
  12. Jupiter 2.5374
  13. Saturn 1.0677
  14. Uranus 0.8947
  15. Neptune 1.1794
  16. Pluto 0.0899
  17.  
  18. our program must accept a string planet name input from the user, and
  19. Using the string input from the user, determine which planet the user selected.
  20. You must use a switch statement with the planet enum type as the condition by which they calculate the weight.
  21. Use the following algorithm for your program:
  22.  
  23. Get user's weight on Earth
  24. Get the user's planet choice as a string
  25. Use a function to validate the user's planet name and return true if the name is valid and false if not using the following prototype:
  26. bool ValidPlanet(string userInput, PlanetType& planet)
  27. If the user does not input a correct planet output an error message (hint: put this error message in your ValidPlanet() function)
  28. Keep prompting the user until you get a valid planet name (hint: use your ValidPlanet() function in a do-loop test condition):
  29. do { //Prompt user for a planet name } while (!ValidPlanet(...));
  30. Identify the user's selected planet and set your planet enum variable accordingly (hint: do this in ValidPlanet using the PlanetType variable you passed by reference)
  31. Using a switch statement and the planet enum variable that was set by ValidPlanet() calculate the user's weight on the selected planet
  32. Output the result
  33. You may not use a menu system whereby you display the planet names and ask the user to enter a number for the associated planet.
  34.  
  35.  
  36.  
  37. ____________________________________________________________________________________________________________________________________
  38.  
  39.  
  40.  
  41.  
  42. //Lab8-EnumeratedTypes.cpp -- Enumerated Types Lab
  43. //CSIS 111-02
  44. //Textbook
  45.  
  46.  
  47. //Include statements
  48. #include <iostream>
  49. #include <string>
  50. #include <fstream>
  51. #include <iomanip>
  52. #include <cmath>
  53.  
  54. using namespace std;
  55.  
  56. //Global declarations: Constants and type definitions only -- no variables
  57.  
  58. enum PlanetType { MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE, PLUTO };
  59.  
  60. //Function prototypes
  61.  
  62. bool ValidPlanet(string userInput, PlanetType& planet);
  63.  
  64. int main()
  65. {
  66. //In cout statement below substitute your name and lab number
  67. cout << "Christopher Kheir -- Lab 8" << endl << endl;
  68. //I have read and understand the Lab Submittal Policy on BB.
  69.  
  70.  
  71.  
  72. //Variable declarations
  73.  
  74. float userWeight;
  75. string userInput;
  76. PlanetType planet;
  77.  
  78. //Program logic
  79.  
  80. cout << "Please enter your weight: ";
  81. cin >> userWeight;
  82. cout << endl;
  83.  
  84. do
  85. {
  86. cout << "Please enter a valid planet name from the list as shown. MERCURY , VENUS , EARTH , MARS , JUPITER , SATURN , URANUS , NEPTUNE , PLUTO : ";
  87. getline(cin, userInput);
  88.  
  89.  
  90. } while (!ValidPlanet(userInput));
  91.  
  92.  
  93.  
  94.  
  95. //Closing program statements
  96. return 0;
  97. }
  98.  
  99. bool ValidPlanet(string userInput, PlanetType& planet)
  100. {
  101.  
  102. for (int i = 0; i < userInput.length(); i++)
  103. {
  104.  
  105. cout << userInput.length(i);
  106.  
  107. }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement