Skip to content

Instantly share code, notes, and snippets.

@bllaude
Created July 27, 2022 13:52
Show Gist options
  • Select an option

  • Save bllaude/6c547f63b1f80cb71e3f2f6822f651c1 to your computer and use it in GitHub Desktop.

Select an option

Save bllaude/6c547f63b1f80cb71e3f2f6822f651c1 to your computer and use it in GitHub Desktop.
Database Program with Vectors (STL)
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
string temp;
int num;
bool run = true;
stringstream ss;
int main()
{
vector<string> names;
cout<<"Please enter names:\n";
while(temp != "done")
{
getline(cin, temp);
if(temp != "done")
{
names.push_back(temp);
}
}
while(run)
{
cout << "\n>> ";
ss.clear();
temp.clear();
getline(cin, temp);
ss << temp;
ss >> temp;
if(temp == "add" || temp == "enter")
{
ss >> temp;
names.push_back(temp);
}
if(temp == "exit")
{
run = false;
return 0;
}
if(temp == "print")
{
for(int i = 0; i != names.size(); i++)
{
cout<<"\n" << names[i];
}
}
if(temp == "size")
{
cout<<"\nSize of database is " << names.size() << endl;
}
if(temp == "search")
{
ss >> temp;
bool recFound = false;
cout << "Searching for " << temp << ".\n";
for (int i = 0; i != names.size(); i++)
{
if(names[i] == temp)
{
cout << "Match found in position " << i + 1<< ".\n";
recFound = true;
}
}
if(recFound == false)
{
cout << "No instances of "<<temp<<" was found in the database.\n";
}
}
}
cin.ignore();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment