100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Other

Manipulating a Vector

Rating
-
Sold
-
Pages
3
Uploaded on
06-11-2025
Written in
2025/2026

The provided code is a C++ program that manages a collection of "Box" objects using a vector. Here's a breakdown of its components and functionality: 1. **Includes and Namespaces**: - The program includes necessary libraries such as `<iostream>` for input/output operations, `<vector>` for using vectors, and `<string>` for string manipulation. The `std` namespace is used to simplify code. 2. **Box Structure**: - A `struct` named `Box` is defined, containing two properties: a `string` for the box's name and a `float` for its height. 3. **Function Declarations**: - Four functions are declared to handle the main operations: - `insertBox`: To add a new box to the collection. - `deleteBox`: To remove a box based on its name. - `displayBoxes`: To display information about all boxes in the vector. - `eraseAllBoxes`: To clear all boxes from the vector. 4. **Main Function**: - A loop allows the user to choose from a menu to add, delete, display boxes or quit the program. - User input is processed, and the appropriate function is called based on the user's choice. 5. **Insert Box Functionality**: - This function prompts the user for box details (name and height) and asks whether to add the box to the front or back of the vector. It handles edge cases, such as when the vector is empty. 6. **Delete Box Functionality**: - This function displays all boxes and asks the user for the name of the box they want to delete. If the box is found, it removes it and provides feedback. 7. **Display Boxes Functionality**: - This function shows the current capacity, size, and details of each box in the vector, including its name and height. 8. **Erase All Boxes Functionality**: - This function removes all boxes from the vector and provides feedback on the names of the boxes being erased, ensuring the vector is empty afterward. The program effectively demonstrates basic operations with vectors in C++, alongside user interaction and basic data management. It uses a simple command-line interface for users to manage their collection of boxes.

Show more Read less








Whoops! We can’t load your doc right now. Try again or contact support.

Document information

Uploaded on
November 6, 2025
Number of pages
3
Written in
2025/2026
Type
Other
Person
Unknown

Subjects

Content preview

#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct Box
{
string name;
float height;
};

void insertBox(vector<Box> & boxes);
void deleteBox(vector<Box> & boxes);
void displayBoxes(vector<Box> & boxes);
void eraseAllBoxes(vector<Box> & boxes);

int main()
{
char answer = 'Y';
vector<Box> boxes;
char choice;

do {
cout << "A. add a box to the vector" << endl;
cout << "B. delete a box from the vector" << endl;
cout << "C. display the vector of boxes" << endl;
cout << "D. quit" << endl << endl;
cout << "Enter choice: ";
cin >> choice;

cin.ignore();

switch(toupper(choice))
{
case 'A': insertBox(boxes);
break;
case 'B': deleteBox(boxes);
break;
case 'C' : displayBoxes(boxes);
break;
case 'D' : eraseAllBoxes(boxes);
return 0;
default : cout << endl << endl << "Error!" << endl;
}

cout << "Enter another choice (Y or N)? ";
cin >> answer;
} while (toupper(answer) == 'Y');
}

void insertBox(vector<Box> & boxes)
{
char choice;
Box box;

cout << "Enter a name for a box: ";
getline(cin, box.name);

cout << "Enter the height of the box in inches: ";
$15.49
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
tinahuynh

Get to know the seller

Seller avatar
tinahuynh California State University, Long Beach
View profile
Follow You need to be logged in order to follow users or courses
Sold
New on Stuvia
Member since
1 month
Number of followers
0
Documents
68
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions