Hello av16352,
Overall The program works. Good for learning, but not the best way to code it.
Technically speaking and somewhat trivial the "#include"s it makes no difference in which order they are in. Although I have found that:
1 2 3
|
#include <iostream>
#include <iomanip>
#include <string>
|
Tends to be helpful. "iomanip"only works with "iostream", so I keep them together. After that I found that alphabetical order tends to help when you forget something.
Lines 7 and 8 are not the best idea to have something like these variables as global variables. Any line of code the follows can change their values and it becomes difficult to track down where it was changed when it should not have been.
Lines 9 - 18 are fine as global variables, but these should be defined as constant variables. As:
1 2
|
constexpr double MERCURY{ 0.4155 };
constexpr double VENUS{ 0.8975 };
|
Also you should be trying to learn and use what became available in the C++ 2011 standards like: "constexpr" and the {}s known as the uniform initializer. These will take you farther than what you have done although technically correct and legal code they are old.
In the function "userInput" a blank line between lines 23 and 24 really help make the code more readable and this is what you should strive for. Also this function is where I would check for valid user input of the planet name and not wait until the next function to check this. It may be my way of thinking, but I think you should validate input when it is input and not wait until later.
Next would be to deal with an invalid weight. The formatted input of
cin >> weight;
is expecting a numeric value and a whole number at that. Anything that is not a numeric value will cause "cin" to be put in a failed state and it will become unusable the rest of the program. I also realize that in the utopia of school nobody makes mistakes, so checking for errors is not necessary yet.
In the "calculatingWeight" function I did this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
void calculatingWeight()
{
double planetWeight{};
if (planet == "Mercury")
//cout << "Your weight: " << weight << " on Mercury is: "
//<< weight * MERCURY << endl;
planetWeight = weight * MERCURY;
// The rest of the else if statements.
// After the else statement, which you could do without.
cout << "\nYour weight of: " << weight << " lbs on " << planet << " is: " << planetWeight << " lbs.\n\n";
|
This way you only need 1 "cout" statement. You already have a value for "weight" and "planet", which are global variables and the function already has access to them. So you can use what you have instead of all the mostly duplicate "cout" statements. This will reduce your code in the function to just what you need making better use of the language and variables that yo have.
"main" is OK and will work.
If you are expecting to get a job in programing with code like:
1 2 3 4 5 6 7 8 9 10 11 12
|
cout << " ___________________________________" << endl;
cout << " |This program calculates the weight |" << endl;
cout << " |of a person in one of the celestial|" << endl;
cout << " |bodies: Mercury, Venus, Earth, Mars|" << endl;
cout << " |Jupiter, Saturn, Uranus, Neptune, |" << endl;
cout << " |Pluto, and Moon. |" << endl;
cout << " |-----------------------------------|" << endl;
cout << " | MENU |" << endl;
cout << " | 0: Quit Program |" << endl;
cout << " | 1: Calculate the weight |" << endl;
cout << " |-----------------------------------|" << endl;
cout << " | Enter your choice: ";
|
You may have a longer weight than others.
This is more efficient and makes better use of the language:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
cout <<
"\n"
" ___________________________________"
" |This program calculates the weight |\n"
" |of a person in one of the celestial|\n"
" |bodies: Mercury, Venus, Earth, Mars|\n"
" |Jupiter, Saturn, Uranus, Neptune, |\n"
" |Pluto, and Moon. |\n"
" |-----------------------------------|\n"
" | MENU |\n"
" | 0: Quit Program |\n"
" | 1: Calculate the weight |\n"
" |-----------------------------------|\n"
" | Enter your choice: ";
|
This may look like 13 lines, but in the end it is considered just 1 string. So you cave 1 "cout", 1 string and not "\n" or "endl" at the end. This puts your input on the same line as the prompt Without any extra coding. Also the "cin" will flush the buffer before any input is taken, so everything will print to the screen before andy input is allowed.
Andy