Reading from a text file and printing the names in alphabetical order using vectors

closed account (9wkE8vqX)
Hi, i'm having a problem on how to print the names in alphabetical orders using the vector. here's the code.. please help me :(

void Record2::view(vector<string> words, int x) //VIEWING THE RECORD'S LIST
{
setcolor(14); system("cls"); infile.open("AddressBook.txt"); //opening the file for reading only
rec.DisplayDate();
setcolor(15); cout << "\n____________________________________________________________________________________ADDRESS BOOK RECORD______________________________________________________________________________________\n\n";
cout << " Full Name: Contact Number: E-Mail Address: Address:";
cout << "\n---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n";
if (infile.is_open()) // verifying if file is open perform the instructions below
{
while (getline(infile, record.fullname), getline(infile, record.contact), getline(infile, record.email), getline(infile, record.address)) //reading the strings inside the text file
{
words.push_back(record.fullname);
for (vector<string>::iterator i = words.begin(); i != words.end(); i++){
setcolor(9); cout << setiosflags(ios::left) << setw(1) << ++x;
cout << setiosflags(ios::right) << setw(31) << *i;
cout << setiosflags(ios::right) << setw(28) << record.contact;
cout << setiosflags(ios::right) << setw(29) << record.email;
cout << setiosflags(ios::right) << setw(100) << record.address << endl; //printing the texts located inside the "AddressBook.txt" in console
}
}
infile.close(); //closing the file
}
else //if file is not open perform this
{
setcolor(12); cout << "File is not open!" << "\n"; rec.viewc2(choice); //calling the function
}
rec.viewc2(choice); //function calling
}
Last edited on
Put your code in code blocks. The <> button.. Also I have no clue what view is, post all relevant code. I also don't know if you are defining the definition and declaring your function Record2 all at the same time or what - but Record2 is not a valid function, where is the function prototype?

For example:

1
2
3
4
5
//Function Prototype as well as Function definition
void Record2()
{ 
  //do stuff 
};

Or alternatively:

1
2
//Function Prototype
void Record2();


1
2
3
4
5
//Function Definition
void Record2()
{
   //do stuff
}


Last edited on
closed account (9wkE8vqX)
Here's my entire code :D

http://pastie.org/10217290
Last edited on
Don't post the entire code, post the relevant code and use the code-tag <>

do you want the vector to be sorted after you printed the names or is it not important?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <algorithm>
#include <string>
#include <vector>

// somewhere else

std::vector<std::string> names = {...}; // imagine it to be filled with names

std::sort(names.begin(), names.end());

// c++98
for(std::vector<std::string>::iterator iter = names.begin(); iter != names.end(); ++iter)
    std::cout << *iter << std::endl;

// c++11
for(const auto& iter : names)
    std::cout << iter << std::endl;


I'm really sorry that I didn't read your code, I will do it after you format it with the code-tag:
[code ]your code[/code ] // without spaces
Last edited on
Topic archived. No new replies allowed.