i have a text file as follows;

james
nnnnnnnnn mmmmm
kristy
llllllllllllllllll oooooooo
james
pppppppp ppppppppppp
james
iiiiiii kkkkkkkkkkkk


now for the out put, i want it as follows ;;;;;;;

james
nnnnnnnnn mmmmm
pppppppp ppppppppppp
iiiiiii kkkkkkkkkkkk
kristy
llllllllllllllllll oooooooo

the code i wrote for this is as;;;;

void File()
{
    string fname[200];
    string lname[200];

    ifstream myfile;		
    myfile.open("names.txt");	

    if (!myfile)
    {
		cout << "File not found." << endl;
    }
    else
    {
		while (!myfile.eof() )
		{
			for(int i = 0; !myfile.eof(); i++)
			{
				getline(myfile, fname[i], '\n' );
				getline(myfile, lname[i], '\n');
			}
		}
    }
    myfile.close();

what i want it to do is to, display the above text file as the expected out put i have provided.
but i am clueless of the code, can some one please help me with the code to get my expected output .. pleaseeeeeeeeeeee, i tried all, and now i am completely lost ... please help :(

I've told you before, using EOF to control file i/o is a bad idea.

And I don't get your output. It isn't sorted alphabetically, after removing duplicates etc etc.

I've told you before, using EOF to control file i/o is a bad idea.

And I don't get your output. It isn't sorted alphabetically, after removing duplicates etc etc.

yeh, i should sort it alphabetically, also, what do u mean by not to use EOF, i am kind of new to C++, and please if u dont mind , tell me what and where should i change in order to get this program running .. pleaseeeee

First of all read in your text file line by line:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
  //read in file
  ifstream read("test.txt");
  string line;
  while ( getline(read, line, '\n'))
  {
    cout <<line <<endl;
  }
  read.close();
  cin.get();
  return 0;
}

Then put each line into a vector or array. Then try to sort them.

First of all read in your text file line by line:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
  //read in file
  ifstream read("test.txt");
  string line;
  while ( getline(read, line, '\n'))
  {
    cout <<line <<endl;
  }
  read.close();
  cin.get();
  return 0;
}

Then put each line into a vector or array. Then try to sort them.

yeh, that part works when i modify teh code as follows

while (!myfile.eof() )
		{
			for(int i = 0; !myfile.eof(); i++)
			{
				getline(myfile, fname[i], '\n' );
				getline(myfile, lname[i], '\n');
			}
                                  cout<<fname[i]<<endl;
                                   cout<<lname[i]<<endl;
		}

Now try sorting them using a vector i.e

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main()
{
  vector <string> array; 
  //read in file
  ifstream read ( "test.txt" );
  string line;
  while ( getline ( read, line, '\n' ) )
  {
    cout << line << endl;
    array.push_back ( line );
  }
  read.close(); 
  sort ( array.begin(), array.end() ); 
  //print out sorted vector

  cout << "\n\nAfter sorting...\n";
  for ( unsigned int i = 0; i < array.size(); i++ )
  {
    cout << array[i] << endl;
  }

  //now try removing duplicates
  //these should be next to one another 
  cin.get();
  return 0;
}

Now try sorting them using a vector i.e

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main()
{
  vector <string> array; 
  //read in file
  ifstream read ( "test.txt" );
  string line;
  while ( getline ( read, line, '\n' ) )
  {
    cout << line << endl;
    array.push_back ( line );
  }
  read.close(); 
  sort ( array.begin(), array.end() ); 
  //print out sorted vector

  cout << "\n\nAfter sorting...\n";
  for ( unsigned int i = 0; i < array.size(); i++ )
  {
    cout << array[i] << endl;
  }

  //now try removing duplicates
  //these should be next to one another 
  cin.get();
  return 0;
}

hey, it sorted off well, but once when v remove the duplicate once, how r v going to display it as i have shown in my expected out put.. (above)

and thanks alot for the sorting thing

I would then use std::unique as follows:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>

using namespace std;

int main()
{
  vector <string> array;

  std::ostream_iterator< string > output ( cout, "\n" );

  ifstream read ( "test.txt" );
  string line;
  while ( getline ( read, line, '\n' ) )
  {
    cout << line << endl;
    array.push_back ( line );
  }
  read.close(); 
  sort ( array.begin(), array.end() ); 

  std::vector< string >::iterator endLocation;
  endLocation = std::unique ( array.begin(), array.end() );

  cout << "\n\n\nAfter sorting and removing duplicates\n";
  std::copy ( array.begin(), endLocation, output ); 

  cin.get();
  return 0;
}

Congratulations, you have successfully tricked me into doing your homework!

I would then use std::unique as follows:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>

using namespace std;

int main()
{
  vector <string> array;

  std::ostream_iterator< string > output ( cout, "\n" );

  ifstream read ( "test.txt" );
  string line;
  while ( getline ( read, line, '\n' ) )
  {
    cout << line << endl;
    array.push_back ( line );
  }
  read.close(); 
  sort ( array.begin(), array.end() ); 

  std::vector< string >::iterator endLocation;
  endLocation = std::unique ( array.begin(), array.end() );

  cout << "\n\n\nAfter sorting and removing duplicates\n";
  std::copy ( array.begin(), endLocation, output ); 

  cin.get();
  return 0;
}

Congratulations, you have successfully tricked me into doing your homework!

hey hey, there is one part left...
it doesn't get displayed as my out put ...... only this pleaseeeeeeeeeeeeeeeee

I don't get the meaning of your output?

now just think that this is a student registration system, and there may be students with the same first name and different surnames for example

james // this is the first name
pauly // surname

alex // first name
mathews // surname

james
stanley // now there could b another james with the same first name and different surname


what my out put should b is like

james //the first name
pauly // surname of first student
stanley //surname of the other student

alex
mathews // this is the other student

//please note that this should b arranged in alphabetical order


or there can be a text file like this

james
pauly
stanley

alex
mathews

james
buterfly

>>>>>>>>>>>>>>>

then the out put should be like

alex
mathews
james
pauly
stanley
buterfly

i want to out put this....please help me out

This article has been dead for over six months. Start a new discussion instead.