1,105,340 Community Members

Problems with String deletion in a Vector.

garr55
Newbie Poster
3 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

Sorry I'm new here, so I might not be posting this correctly, tips for next time would be appreciated. I'm having a problem with my delete string script within my program, here's the code:

//program to input a list of names, display them, sort them, and edit the list.
#include <iostream>
#include <vector>
#include <string>

using namespace std;
void getnames(vector<string>&);
void displaynames(vector<string>&);
void sortnames(vector<string>& names);
void removename(string name,vector<string>& names);
void swap(int& x, int& y);

int main() {
    vector<string> names;
    string name;
    while (true) {//while loop to keep program running
          string choice;
          cout<<endl;
          cout<<"Enter function you wish to use"<<endl;
          cout<<"(getnames,displaynames,sortnames,findname,removename,quit): ";
          cin>>choice;
          if (choice=="getnames"){//if option to get input
             getnames (names);
             system ("PAUSE");
             }

          if (choice=="displaynames") {//if option to display names
             displaynames (names);
             system ("PAUSE");
             }

          if (choice=="sortnames"){//if option to sort the names by alphabetically
             sortnames (names);
             system ("PAUSE");
             }

          if (choice=="removename"){//if option to delete a name from the list
             cout<<"Enter Name to Remove";
             getline(cin,name);
             removename (names, name);
             system ("PAUSE");
             }

          if (choice=="quit") {break;}//if option to quit the program

    }
return 0;
}


void getnames (vector<string>& names) {//input names for the vector
     while (true) {
           string name;
           cout<<"Enter a name (quit to stop): ";
           cin>>name;
           if (name == "quit") break;
           names.push_back(name);
           }
}

void displaynames (vector<string>& names) {//displaying the names in order
     int i;
     for(i=0; i<names.size();i++){
         cout<<names[i]<<endl;
          }
     cout<<i<<" names entered"<<endl;
}

void swap(int& x, int& y)//simple swap function
{
     int temp;
     temp=x;
     x=y;
     y=temp;
}

void sortnames(vector<string>& names) { //sorting the names in alphabetical order
  for (int i = 1; i < names.size(); i++)
    for (int j = 0; j < names.size() - i; j++)
      if (names[j] > names[j+1]) swap(names[j], names[j+1]);
}

void removename (string Name, vector<string>& names) {//removing a name
     for (int i=0; i!=names.size(); i++)
     {
        if (names[i].Name == name)
        {
            names.erase(names.begin()+i,names.begin()+1+i);
            break;
        }
     }
}

and my error codes:
In function int main()': 40 conversion from std::vector<std::string, std::allocator<std::string> >' to non-scalar type std::string' requested In functionvoid removename(std::string, std::vector<std::string, std::allocator<std::string> >&)':
85 'struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >' has no member named 'Name'
85 `name' undeclared (first use this function)

garr55
Newbie Poster
3 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

I was wondering if anyone could tell me what I did wrong, I'm new to programming and I'm beyond lost.

Member Avatar
Ancient Dragon
Achieved Level 70
27,580 posts since Aug 2005
Reputation Points: 5,231 [?]
Q&As Helped to Solve: 3,028 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
1
 

on line 40 the parameters to removename() are reversed, you have them backwards.

garr55
Newbie Poster
3 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

slightly embarressed that I didn't catch that, thanks that got rid of a few errors, only the ones on line 86 remain.

Question Answered as of 1 Year Ago by Ancient Dragon
Member Avatar
Ancient Dragon
Achieved Level 70
27,580 posts since Aug 2005
Reputation Points: 5,231 [?]
Q&As Helped to Solve: 3,028 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

Same problem as the previous one.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: