Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- 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.
- Mercury 0.4155
- Venus 0.8975
- Earth 1.0
- Moon 0.166
- Mars 0.3507
- Jupiter 2.5374
- Saturn 1.0677
- Uranus 0.8947
- Neptune 1.1794
- Pluto 0.0899
- our program must accept a string planet name input from the user, and
- Using the string input from the user, determine which planet the user selected.
- You must use a switch statement with the planet enum type as the condition by which they calculate the weight.
- Use the following algorithm for your program:
- Get user's weight on Earth
- Get the user's planet choice as a string
- 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:
- bool ValidPlanet(string userInput, PlanetType& planet)
- If the user does not input a correct planet output an error message (hint: put this error message in your ValidPlanet() function)
- Keep prompting the user until you get a valid planet name (hint: use your ValidPlanet() function in a do-loop test condition):
- do { //Prompt user for a planet name } while (!ValidPlanet(...));
- 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)
- Using a switch statement and the planet enum variable that was set by ValidPlanet() calculate the user's weight on the selected planet
- Output the result
- 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.
- ____________________________________________________________________________________________________________________________________
- //Lab8-EnumeratedTypes.cpp -- Enumerated Types Lab
- //CSIS 111-02
- //Textbook
- //Include statements
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <iomanip>
- #include <cmath>
- using namespace std;
- //Global declarations: Constants and type definitions only -- no variables
- enum PlanetType { MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE, PLUTO };
- //Function prototypes
- bool ValidPlanet(string userInput, PlanetType& planet);
- int main()
- {
- //In cout statement below substitute your name and lab number
- cout << "Christopher Kheir -- Lab 8" << endl << endl;
- //I have read and understand the Lab Submittal Policy on BB.
- //Variable declarations
- float userWeight;
- string userInput;
- PlanetType planet;
- //Program logic
- cout << "Please enter your weight: ";
- cin >> userWeight;
- cout << endl;
- do
- {
- cout << "Please enter a valid planet name from the list as shown. MERCURY , VENUS , EARTH , MARS , JUPITER , SATURN , URANUS , NEPTUNE , PLUTO : ";
- getline(cin, userInput);
- } while (!ValidPlanet(userInput));
- //Closing program statements
- return 0;
- }
- bool ValidPlanet(string userInput, PlanetType& planet)
- {
- for (int i = 0; i < userInput.length(); i++)
- {
- cout << userInput.length(i);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement