weight = -1; // start with something invalid
while ( weight <= 0 ) // keep looping menu and input whilst invalid
{
cout<<"Please enter your weight in KG: ";
cin>>weight;
if ( weight <= 0 ) cout << "Invalid input; please try again.\n"; // notify if invalid
}
choice = 0;
while ( choice < 1 || choice > 10 )
{
cout<<"***********************************\n";
cout<<"Please choose one of the following:\n";
cout<<"|-----------------|--------------------|"<<endl;
cout<<"| Planet | Force of gravity |"<<endl;
cout<<"|-----------------|--------------------|"<<endl;
cout<<"|1. Earth | 1.00 |"<<endl;
cout<<"|2. Jupiter | 2.65 |"<<endl;
cout<<"|3. Mars | 0.39 |"<<endl;
cout<<"|4. Mercury | 0.38 |"<<endl;
cout<<"|5. Neptune | 1.23 |"<<endl;
cout<<"|6. Pluto | 0.05 |"<<endl;
cout<<"|7. Saturn | 1.17 |"<<endl;
cout<<"|8. Uranus | 1.05 |"<<endl;
cout<<"|9. Venus | 0.78 |"<<endl;
cout<<"|-----------------|--------------------|"<<endl;
cout<<" 10. Exit "<<endl;
cout<<" "<<endl;
cout<<"Your choice: ";
cin>>choice;
if ( choice < 1 || choice > 10 ) cout << "Invalid input; please try again.\n"; // notify if invalid
}
Your code is quite repetitive. Consider:
- Eliminating all those different weight variables - you don't need a different one for each planet.
- Use either parallel arrays for planet name, relative gravitational attraction OR (better) structs containing these. Then you won't need a long line of if blocks.
This thread makes zero sense if you remove your original code. Please do not do that. For consistency I include your original code at the bottom of this post. I think my solution did all that was necessary for this.
If you want a simple struct then
1 2 3 4 5 6
struct Planet
{
string name;
double relativeGravity;
// add any other properties, like radius of orbit etc.
};
would be fine.
You can declare an array of planets like
1 2
constint NUM_PLANETS = 9; // if you insist on keeping Pluto
Planet parray[NUM_PLANETS] = { { "Earth", 1.0 }, { "Jupiter", 2.65 }, ... }
You can loop through the array of type Planets just like you would an array of type ints etc. Refer to the ith planet's name as parray[i].name.
"pseudocode" is not something you learn in class. It's just a common-sense way of describing to yourself in your own language or slang the order of operations in a program. It is programming-language agnostic.
Another alternative would be to put the menu in a function and call it when needed. Then you could do something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
do
{
MainMenu();
cout << "Your choice: ";
cin >> choice;
//codes
if (choice > 9 || choice < 1) //user input validation <--- The else will cause an error becuse there is no if.
{
cout << "Invalid Input\n";
//How to use do while loop to return to menu.
}
} while (choice > 9 || choice < 1);
Or what I like to do is have "MainMenu" return the choice back to where it was called and put a do.while or while loop in the function, so it only returns a correct answer.
The above code would still apply,but you will have to replace "MainMenu();" with all your cout statements and define "choice" above the do/while loop and return choice after the do.while loop. This way everything is done in one function and the function can be called from anywhere.
// How touse a do while loop to display a menu.cpp Entry point of program.
//
// Written 01/29/2017
//
// Finished //2017
//
// By Zoren http://www.cplusplus.com/forum/beginner/229334/
//
/*
*/
//
//
//
//
//
#include <iostream>
#include <chrono>
#include <thread>
int MainMenu();
int main()
{
int choice{}; // <--- Always good practice to initialize your variables.
int weight{};
std::cout << "*************************\n";
std::cout << " Welcome to Planetor!\n";
std::cout << "*************************\n";
std::cout << "Please enter your weight in KG: ";
std::cin >> weight;
choice = MainMenu();
// <--- You could use a switch here to use the value of "choice".
switch (choice)
{
case 1:
// do something.
break;
case 10:
break; // <--- Do nothing except the switch or put a return 0; here.
default:
break;
}
return 0;
} // End main
int MainMenu()
{
int choice{};
do
{
std::cout << "\n***********************************\n"; // <--- Added \n at the begining.
std::cout << "Please choose one of the following:\n";
std::cout << "|-----------------|--------------------|" << std::endl;
std::cout << "| Planet | Force of gravity |" << std::endl;
std::cout << "|-----------------|--------------------|" << std::endl;
std::cout << "|1. Earth | 1.00 |" << std::endl;
std::cout << "|2. Jupiter | 2.65 |" << std::endl;
std::cout << "|3. Mars | 0.39 |" << std::endl;
std::cout << "|4. Mercury | 0.38 |" << std::endl;
std::cout << "|5. Neptune | 1.23 |" << std::endl;
std::cout << "|6. Pluto | 0.05 |" << std::endl;
std::cout << "|7. Saturn | 1.17 |" << std::endl;
std::cout << "|8. Uranus | 1.05 |" << std::endl;
std::cout << "|9. Venus | 0.78 |" << std::endl;
std::cout << "|-----------------|--------------------|" << std::endl;
std::cout << " 10. Exit " << std::endl;
std::cout << " " << std::endl;
std::cout << "Your choice: ";
std::cin >> choice;
if (choice > 10 || choice < 1) //user input validation
{
std::cout << "\nInvalid Input\n"; // <--- Added \n at the begining.
std::this_thread::sleep_for(std::chrono::seconds(3)); // Requires header files "chrono" and "thread"
//How to use do while loop to return to menu.
}
} while (choice > 10 || choice < 1);
return choice;
} // End MainMenu
I did not see your if/else if statements until after I wrote this, but the switch will work in place of the if/else if statements.
Using the switch long with "choice" you could define the planet gravitates as constexprdouble planteGravity[10]{ 0, 1.0, 2.65, 0.39, 0.38, 1.23, 0.05, 1.17, 1.05, 0.78 };. Then in the switch refer to the needed gravity as planteGravity[choice] in your math. in the switch you can do: std::cout << "Your weight on Earth is: " << weight * planteGravity[choice]; and eliminate the need for all those different variables.