Using C++ Write a program using vectors that allows a user to maintain an invent
ID: 3719003 • Letter: U
Question
Using C++
Write a program using vectors that allows a user to maintain an inventory of what they are carrying. The program should allow the user to see everything they are holding, add something to their inventory and drop something from their inventory.
So, code will say:
1.See inventory
2. Add to inventory
3. Remove from inventory.
One will print out what they currently have.
Two will say, "what will you want to add", and let them enter a string. It will then be added to inventory (push)
Three will say "Which do you want to drop" which will let them enter a number. It then will be removed from inventory (erase)
Explanation / Answer
Answer :
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int getMenuChoice(){
cout << "1. See inventory" << endl;
cout << "2. Add to inventory" << endl;
cout << "3. Remove from inventory" << endl;
cout << "4. Exit" << endl;
int choice;
while(true){
cout << "Enter your choice: ";
cin >> choice;
if(choice < 1 || choice > 4){
cout << "Invalid choice. Try again!!" << endl;
}
else{
return choice;
}
}
}
int main(){
vector<string> v;
string str;
int choice;
while(true){
choice = getMenuChoice();
if(choice == 1){
for(int i = 0; i < v.size(); ++i){
cout << v[i] << " ";
}
cout << endl;
}
else if(choice == 2){
cout << "what do you want to add: ";
cin >> str;
v.push_back(str);
}
else if(choice == 3){
cout << "which one do you want to drop: ";
cin >> str;
vector<string>::iterator itr = find(v.begin(), v.end(), str);
v.erase(itr);
}
else{
break;
}
cout << endl << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.