Answer to Question #279444 in C++ for Neo

Question #279444

Create a struct Boat include the length, name and how many nights the boat will be in the marina.

Ask the user for the length of the boat, the name of the boat and how many nights they will be staying. Store the info in a vector of boats but do not allow more than 20 boats because that is the capacity of the marina. Allow the user to print out all the boats that are 40 feet or above. Write a menu so the user can continue until they wish to quit.


1
Expert's answer
2021-12-14T07:19:34-0500
#include<iostream>
#include<vector>
#include<string>

using namespace std;

struct Boat
{
	string bName;
	double length;
	int nights; 
};

void AddInfo(vector<Boat>& vb)
{
	
	int numB;
	do
	{
		cout<<"How many boats do you want to add?\n";
		cin>>numB;
		if(numB>20)
			cout<<"Marine can contain up to 20 boats!\n";	
	}while(numB>20);
	
	Boat b;
	for(int i=0;i<numB;i++)
	{
		cout<<"What is the length of the boat "<<i+1<<" ?\n";
		cout<<"Your input: ";
		cin>>b.length;
		cout<<"What is the name of the boat "<<i+1<<" ?\n";
		cout<<"Your input: ";
		cin>>b.bName;
		cout<<"How many nights you will be staying on the boat "<<i+1<<" ?\n";
		cout<<"Your input: ";
		cin>>b.nights;
		vb.push_back(b);
	}
	
}

void Print40AndAbove(vector<Boat>& vb)
{
	vector<Boat>::iterator it;
	cout<<"\nName \tLength \tnights\n";
	for(it=vb.begin();it!=vb.end();++it)
	{
		if(it->length>=40)
		{
			cout<<it->bName<<"\t"<<it->length<<"\t"<<it->nights;
		}
	}
}
void Menu()
{
	cout<<"\nYou are welcome to marina!\n"
		<<"A - Add boats\n"
		<<"P - Print boats that are 40 feet or above\n"
		<<"Q - Quit program\n"
		<<"Your choice: ";
}

int main()
{
	vector<Boat>vb;
	vb.reserve(20);
	char ch;
	do
	{
		Menu();
		cin>>ch;
		switch(ch)
		{
			case 'A':case 'a':
			{
				AddInfo(vb);
				break;
			}
			case 'P':case 'p':
			{
				Print40AndAbove(vb);
				break;
			}
		}
	}while(ch!='Q'&&ch!='q');
		
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog