i need to write a c++ function that could read in the values of a text file,

but in the text file we need to eliminate duplicate values;

for example we may have values like ;;;;

this is a text file, and it contains the above students, there first name and surnames

sam ; merdock
hill ; firewood
sara ; nikerman
sam ; shena
jillia // there is another word like this, but this is not the first name, the first name should always precede with the sign " ; "
kills
robert ; wood

hint :: v should use the " ; " sign to identify the first name (coz first name will precede with the mark " ; ")


now what i want it to do is, to display values as follows

hill firewood
sara nikerman
sam shena
jillia
kills
merlock // this has come from the above name "sam merdock", since sam is common v write the surnames of students as this
robert wood


i should write this on to another file as well,,

i hope i explained it well .. some one plzz help me with the code :) :)

Recommended Answers

All 14 Replies

i need to write a c++ function that could read in the values of a text file,

but in the text file we need to eliminate duplicate values;

for example we may have values like ;;;;

this is a text file, and it contains the above students, there first name and surnames

sam ; merdock
hill ; firewood
sara ; nikerman
sam ; shena
jillia // there is another word like this, but this is not the first name, the first name should always precede with the sign " ; "
kills
robert ; wood

hint :: v should use the " ; " sign to identify the first name (coz first name will precede with the mark " ; ")


now what i want it to do is, to display values as follows

hill firewood
sara nikerman
sam shena
jillia
kills
merlock // this has come from the above name "sam merdock", since sam is common v write the surnames of students as this
robert wood


i should write this on to another file as well,,

i hope i explained it well .. some one plzz help me with the code :) :)

I think you are going to have to ask a more specific question regarding what precisely you are having difficulty with. As I see it, there are several aspects to this assignment.

1) Open and read from a file.
2) Parse lines of a file into first and last names.
3) Check for duplicates.
4) Store names in a vector.
5) Alphabetize?
6) Display the names.

You'll probably need to set up a struct to store the first and last names. Have a vector of this struct. Is the above the game plan? Is not, what is? What in the above list do you know how to do, what do you need help on? We have no idea until you tell us and give us a better idea of how you want to approach the program.

I think you are going to have to ask a more specific question regarding what precisely you are having difficulty with. As I see it, there are several aspects to this assignment.

1) Open and read from a file.
2) Parse lines of a file into first and last names.
3) Check for duplicates.
4) Store names in a vector.
5) Alphabetize?
6) Display the names.

You'll probably need to set up a struct to store the first and last names. Have a vector of this struct. Is the above the game plan? Is not, what is? What in the above list do you know how to do, what do you need help on? We have no idea until you tell us and give us a better idea of how you want to approach the program.

i no how to read the data from the text file ehat i want to know is how to check for duplicates, and display it as shown

i no how to read the data from the text file ehat i want to know is how to check for duplicates, and display it as shown

OK, post some code please. Before you can check for duplicates, you need to have any struct and any vector set up, and the parsing code done. We can't help till we see that code. Checking for duplicates requires searching the vector.

Compare the values in the vector, I think you can use the == operator

void readDisplayFile()
{
    string fname;
    string surnames;

    ifstream myfile1;		
    myfile1.open("x.txt");	

    if (!myfile1)
    {
		cout << "File not found." << endl;
    }
    else
    {
		while (!myfile1.eof() )
		{
			for(int i = 0; !myfile1.eof(); i++)
			{
				getline(myfile1, fname, ' ' );
				getline(myfile1, surnames, '\n');
	
				cout<<fname<<endl;
				cout<<surnames<<endl;
			
			}
		}
	
    }
    myfile1.close();
	
}
void readDisplayFile()
{
    string fname;
    string surnames;

    ifstream myfile1;		
    myfile1.open("x.txt");	

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

this is what i have done so far, now i want to extract individual words and remove duplicated fnames, and duplicated surnames... what may be the code to achive this.... please help me :(

Member Avatar for iamthwee

> i want to extract individual words and remove duplicated

Depends are you allowed to use the <STL>? You could use std::unique, or write your own:

1) Sort values,
2) Remove all the ones repeated next to one another.

And using EOF is generally a bad idea to read in files.

> i want to extract individual words and remove duplicated

Depends are you allowed to use the <STL>? You could use std::unique, or write your own:

1) Sort values,
2) Remove all the ones repeated next to one another.

And using EOF is generally a bad idea to read in files.

yeh v r allowed to use it ...

void readDisplayFile()
{
    string fname;
    string surnames;

    ifstream myfile1;		
    myfile1.open("x.txt");	

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

this is what i have done so far, now i want to extract individual words and remove duplicated fnames, and duplicated surnames... what may be the code to achive this.... please help me :(

i am simply clueless can some help me oout from this point ...........

void readDisplayFile()
{
    string fname;
    string surnames;

    ifstream myfile1;		
    myfile1.open("x.txt");	

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

this is what i have done so far, now i want to extract individual words and remove duplicated fnames, and duplicated surnames... what may be the code to achive this.... please help me :(

i am simply clueless can some help me oout from this point ...........

void readDisplayFile()
{
    string fname;
    string surnames;

    ifstream myfile1;		
    myfile1.open("x.txt");	

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

this is what i have done so far, now i want to extract individual words and remove duplicated fnames, and duplicated surnames... what may be the code to achive this.... please help me :(

i am simply clueless can some help me oout from this point ...........

Member Avatar for iamthwee

I just need to clarify something. Tell me are you clueless?

One, you don't need to keep bumping your thread. Two, where is your vector? Three, are you using a struct? Four:

void readDisplayFile()

You have a void function that takes no parameters. Thus unless you are using global variables, all the work you do in this function is lost.

Five, I see nothing here that deals with semicolons and how to parse that part of the line. Postpone the part about dealing with duplicates. You need to step back and think about the program as a whole. You need to define a struct to hold a name, and declare your vector. You need to change this function prototype:

void readDisplayFile()

so you can call it from main. You need to have it return something or pass it something by reference.

Member Avatar for iamthwee

I agree, removal of duplicates isn't really the issue here, but rather the implementation of your struct/class.

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.