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 :(

Recommended Answers

All 11 Replies

Member Avatar for iamthwee

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

Member Avatar for iamthwee

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;
		}
Member Avatar for iamthwee

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

Member Avatar for iamthwee

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

Member Avatar for iamthwee

I don't get the meaning of your output?

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

Member Avatar for iamthwee

OK I've had enough of this nonsense, you're on your own now.

Ha ha ha.

commented: a gr8 C++ coder, helped me a lot.. thanx +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.