Write a menu driven C++ program to store the data about the planets in a star sy
ID: 671410 • Letter: W
Question
Write a menu driven C++ program to store the data about the planets in a star system. The choices in the menu are (a) for add a planet, (s) for select a planet by number, (l) for list all planets, and (q) for quit. You do not need to create an option or code for deleting a planet. Selecting a planet will display all the data about a particular planet, and listing all planets will display all data about all planets stored in the database. You may use either parallel arrays or an array of structs to store the data about the planets. You can either calculate the habitability every time a planet is selected or listed, or you can store the habitability in an array or as part of the struct.
Example Output (User input in bold):
Computer. Working. Please make your selection: (a)dd a planet, (s)elect a planet, (l)ist all planets, (q)uit: l
There are no planets in the database.
Please make your selection: (a)dd a planet, (s)elect a planet, (l)ist all planets, (q)uit: a
Please enter the diameter of planet number 0: 6342.5
Please enter the average temperature of planet number 0: 65
Please enter the planets distance from its star in millions of miles: 96.4
Please enter the type of the planet: terrestrial
Please make your selection: (a)dd a planet, (s)elect a planet, (l)ist all planets, (q)uit: s
Specify the planet number: 1
That planet is not currently in the database.
Please make your selection: (a)dd a planet, (s)elect a planet, (l)ist all planets, (q)uit: s
Specify the planet number: 0
Data for planet 0:
Diameter: 6342.5 miles.
Distance for its star: 96.4 million miles.
Average Temperature: 65 degrees F.
Type: terrestrial.
Habitable: possibly.
Please make your selection: (a)dd a planet, (s)elect a planet, (l)ist all planets, (q)uit: a
Please enter the diameter of planet number 1: 45901
Please enter the average temperature of planet number 1: 210
Please enter the planets distance from its star in millions of miles 1: 450
Please enter the type of the planet: Jovian
Please make your selection: (a)dd a planet, (s)elect a planet, (l)ist all planets, (q)uit: l
Planets in the database:
Planet 0:
Diameter: 6342.5 miles.
Distance for its star: 96.4 million miles.
Average Temperature: 65 degrees F.
Type: terrestrial.
Habitable: possibly.
Planet 1:
Diameter: 45901 miles.
Distance for its star: 450 million miles.
Average Temperature: 210 degrees F.
Type: Jovian.
Habitable: impossible.
Please make your selection: (a)dd a planet, (s)elect a planet, (l)ist all planets, (q)uit: a
Please enter the diameter of planet number 2: 2500
Please enter the average temperature of planet number 2: -10
Please enter the planets distance from its star in millions of miles 2: 230
Please enter the type of the planet: dwarf
Please make your selection: (a)dd a planet, (s)elect a planet, (l)ist all planets, (q)uit: l
Planets in the database:
Planet 0:
Diameter: 6342.5 miles.
Distance for its star: 96.4 million miles.
Average Temperature: 65 degrees F.
Type: terrestrial.
Habitable: possibly.
Planet 1:
Diameter: 45901 miles.
Distance for its star: 450 million miles.
Average Temperature: 210 degrees F.
Type: Jovian.
Habitable: impossible.
Planet 2:
Diameter: 2500 miles.
Distance for its star: 230 million miles.
Average Temperature: -10 degrees F.
Type: dwarf.
Habitable: impossible.
Please make your selection: (a)dd a planet, (s)elect a planet, (l)ist all planets, (q)uit: q
Program termination.
Explanation / Answer
working code:
#include<iostream>
#include<string>
int main()
{
int num;
int plannum[10];
int i;
for (i=0;i<10;i++)
{
plannum[i]=-1;
}
float diameter[10];
float temp[10];
float distance[10];
string nature[10];
char choice;
cout <<"There are no planets in the database. ";
cout <<"Please make your selection: (a)dd a planet, (s)elect a planet, (l)ist all planets, (q)uit: ";
cin>>choice;
while (choice != 'q')
{
if (choice == 'a')
{
cout<<"Specify the planet number ";
cin>>num;
plannum[num] = num;
cout <<"Please enter the diameter of planet number "<<num;
cin>>diameter[num];
cout<<"Please enter the average temperature of planet number "<<num;
cin>>temp[num];
cout<<"Please enter the planets distance from its star in millions of miles "<<num;
cin>>distance[num];
cout<<"Please enter the type of the planet: "<<num;
cin>>nature[num];
cout <<"Please make your selection: (a)dd a planet, (s)elect a planet, (l)ist all planets, (q)uit: ";
cin>>choice;
}
else if (choice == 's')
{
cout<<"Specify the planet number:";
cin>>num;
if (plannum[num] == -1)
cout<<"That planet is not currently in the database.";
else
{
cout<<"Data for Planet";
cout<<diameter[num];
cout<<temp[num];
cout<<distance[num];
cout<<nature[num];
cout <<"Please make your selection: (a)dd a planet, (s)elect a planet, (l)ist all planets, (q)uit: ";
cin>>choice;
}
}
else if (choice == 'l')
{
cout<<"Planets in the database: ";
for (i=0;i<10;i++)
{
if(plannum[i] != -1)
{
cout<<"Planet "<<i<<": ";
cout<<"Diameter: "<<diameter[num]<<" miles. ";
cout<<"Distance for its star:"<<distance[num]<<" million miles. ";
cout<<"Average Temperature: 65"<<temp[num]<<" degrees F. ";
cout<<" Type: "<<nature[num]<<endl;
if(nature[num] == "terrestrial")
cout<<" Habitable: possibly. ";
else
cout<<" Habitable: impossible. ";
}
}
cout <<"Please make your selection: (a)dd a planet, (s)elect a planet, (l)ist all planets, (q)uit: ";
cin>>choice;
}
else
{
break;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.