ive written this program that allows the user to input names and find and remove them in a vector. the insertion function is able to put them in alphabetical order but i am having a problem when i insert a name that is less than the previous name entered. for example, when i insert john and then insert adam, it returns the array as adam and adam instead of adam and john. i am certain that the error is in the insert function at the for loop, but i dont know what the error and cant fix it. ive been trying for hours, its probably something simple that i cant see, and any help would be great thanks!
Code:
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
using namespace std;
const int NOT_FOUND = -1;
/**
prints out all names in a vector, one per line
param names the vector to print
*/
void printNames(vector<string> names) {
for (unsigned int i=0; i < names.size(); i++) {
cout << names[i] << endl;
}
}
/**
finds location of a name in a vector of names
param names is the vector to search
param target is the name we are looking for
returns the location of target in the vector, or
NOT_FOUND if it's not present
*/
int lookup(vector<string> names, string target) {
unsigned int loc = 0;
while (loc < names.size() && names[loc] != target) {
loc++;
}
if (loc == names.size()) {
return NOT_FOUND;
}
else {
return loc;
}
}
/**
removes value at loc in names
param names is a vector where size() > 0
param loc is a location such that 0 <= loc < size()
*/
void removeAt(vector<string>& names, unsigned int loc) {
assert(0 <= loc && loc < names.size());
for (unsigned int i = loc; i < names.size()-1; i++) {
names[i] = names[i+1];
}
names.pop_back();
}
/**
removes a name from names vector. if target name not present does nothing
leaves remaining names in same order as before call.
param names is the vector
param target the name to remove
*/
void remove(vector<string> &names, string target) {
int loc = lookup(names, target);
if (loc != NOT_FOUND) {
removeAt(names,loc);
}
}
/**
inserts into a vector of strings in alphabetical order
if it's not already there.
param names is a list of strings in alphabetical order
param newName is a name to insert
returns false if no insert done (name is already in list), true otherwise
*/
bool insert(vector<string> &names, string newName) {
int loc = lookup(names,newName); // already present
if (loc == NOT_FOUND) { //
unsigned int newLoc = 0; // find correct location
while (newLoc < names.size() && newName > names[newLoc]) {
newLoc++;
}
names.push_back(newName);
for (unsigned int i = newLoc+1; i < names.size(); i++) {
names[i] = names[i-1];
}
//names[newLoc] = newName;
return true;
}
else {
return false;
}
}
int main() {
vector<string> names;
string name;
char cmd;
int loc;
cout << "i(nsert) r(emove) l(ookup) e(xit): ";
cin >> cmd;
while (cmd != 'e') {
switch (cmd) {
case 'i':
cout << "What name do you want to insert? ";
cin >> name;
insert(names,name);
break;
case 'r':
cout << "What name do you want to remove? ";
cin >> name;
remove(names,name);
break;
case 'l':
cout << "What name are you looking for? ";
cin >> name;
loc = lookup(names, name);
if (loc == NOT_FOUND) {
cout << name << " is not in the list" << endl;
}
else {
cout << name << " is " << loc+1 << "th in the list" << endl;
}
break;
default:
cout << "Invalid command." << endl;
}
cout << "There are " << names.size() << " names." << endl;
cout << "The names are..." << endl;
printNames(names);
cout << "i(nsert) r(emove) l(ookup) e(xit): ";
cin >> cmd;
}
return 0;
}