100% tevredenheidsgarantie Direct beschikbaar na je betaling Lees online óf als PDF Geen vaste maandelijkse kosten 4.2 TrustPilot
logo-home
Overig

Manipulating a Vector

Beoordeling
-
Verkocht
-
Pagina's
3
Geüpload op
06-11-2025
Geschreven 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.

Meer zien Lees minder
Instelling
Vak








Oeps! We kunnen je document nu niet laden. Probeer het nog eens of neem contact op met support.

Gekoppeld boek

Geschreven voor

Instelling
Vak

Documentinformatie

Geüpload op
6 november 2025
Aantal pagina's
3
Geschreven in
2025/2026
Type
Overig
Persoon
Onbekend

Onderwerpen

Voorbeeld van de inhoud

#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: ";
€13,72
Krijg toegang tot het volledige document:

100% tevredenheidsgarantie
Direct beschikbaar na je betaling
Lees online óf als PDF
Geen vaste maandelijkse kosten

Maak kennis met de verkoper
Seller avatar
tinahuynh

Maak kennis met de verkoper

Seller avatar
tinahuynh California State University, Long Beach
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
Nieuw op Stuvia
Lid sinds
1 maand
Aantal volgers
0
Documenten
68
Laatst verkocht
-

0,0

0 beoordelingen

5
0
4
0
3
0
2
0
1
0

Recent door jou bekeken

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo makkelijk kan het dus zijn.”

Alisha Student

Veelgestelde vragen