can you help me figure this out. Ok for option (l) which means show all the plan
ID: 671572 • Letter: C
Question
can you help me figure this out. Ok for option (l) which means show all the planets in the database I need a way to check if the array is empty or not to output that there are no planets in the database, can you please help me , also Im not suppose to use include <string>
what else can I use to save the complete word ex terrestial
#include<iostream>
#include<string>
using namespace std;
int main()
{
//variable declarations.
int num = 0;
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;
//Menu driven program !
cout << "Please make your selection: (a)dd a planet, (s)elect a planet, (l)ist all planets, (q)uit: ";
cin >> choice;
while (choice != 'q')
{
//This choice its to add a planet!
if (choice == 'a')
{
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];
num++;// adds the num of the planet by 1!
cout << "Please make your selection: (a)dd a planet, (s)elect a planet, (l)ist all planets, (q)uit: ";
cin >> choice;
}
//Search a planet's characteristics by planet number!
else if (choice == 's')
{
cout << "Specify the planet number: ";
cin >> num;
//If the planet number dosnt match then there are no planets in the database!
if (plannum[num] == -1){
cout << "That planet is not currently in the database." << endl;
cout << "Please make your selection: (a)dd a planet, (s)elect a planet, (l)ist all planets, (q)uit: ";
cin >> choice;
}
//Otherwise cout all the characteristics inputed by the user!
else
{
cout << "Data for Planet" << num << ": " << endl;
cout << " Diameter: " << diameter[num] << "miles." << endl;
cout << " Distance for its star: " << temp[num] << " million miles." << endl;
cout << " Average Temperature: " << distance[num] << " degrees F." << endl;
cout << " Type: " << nature[num] << endl;
cout << "Please make your selection: (a)dd a planet, (s)elect a planet, (l)ist all planets, (q)uit: ";
cin >> choice;
}
}
//This is cout all the planet's charcteristics with their corresponding planet number in the database!.
else if (choice == 'l')
{
//In case there are no planets in the database yet! THIS IS NOT WORKING I NEED TO FIND OUT IF THE ARRAY IS EMPTY
if (plannum[i] != 0)
{
cout << "There are no planets in the database!" << endl;
cout << "Please make your selection: (a)dd a planet, (s)elect a planet, (l)ist all planets, (q)uit: ";
cin >> choice;
}
//If there are planets in the database then show them!
else
{
cout << "Planets in the database: " << endl;
for (i = 0; i < 10; i++){
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;
//This if statement its to output if the planet its habitable or not!
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;
}
}
}
}
Explanation / Answer
if you have noticed num is the counter represents how many planet in our database, if num = 0 means no planet in the database.
else if (choice == 'l'){
//In case there are no planets in the database yet! THIS IS NOT WORKING I NEED TO FIND OUT IF THE ARRAY IS EMPTY
if (num == 0){
cout << "There are no planets in the database!" << endl;
cout << "Please make your selection: (a)dd a planet, (s)elect a planet, (l)ist all planets, (q)uit: ";
cin >> choice;
}
//If there are planets in the database then show them!
else{
cout << "Planets in the database: " << endl;
for (i = 0; i < num; i++){
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;
//This if statement its to output if the planet its habitable or not!
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.