Dec 3, 2011 at 5:44am UTC
I'm creating a function that reads a file of words and adds it into an array of strings. Then I have to remove any word that are duplicates.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void diffwords(int readdata, string text [], string diffword [])
{
int counter=0, counter2=0;
string empty="" ;
ifstream cin ("C:\\c++ data\\HW - String.txt" );
while (!cin.eof()){
cin>>text[readdata];
readdata++; //amount of words
}
while (counter<readdata && diffword[counter2] == empty){
if (text[counter] != diffword[counter2]){
diffword[counter2] = text[counter];
}
counter++;
}
}
I can't seem to get rid of the words if there are duplicates. Anyone has an idea how?
Last edited on Dec 3, 2011 at 6:02am UTC
Dec 3, 2011 at 6:21am UTC
Removing a single element from the middle of an array is hard. Use a vector, or use memmove.
Dec 3, 2011 at 7:25am UTC
Yeek ! I'll give you 2 ways :
- Make a buffer string to copy the desired result (delete duplicates) from original string to it.Then copy to the target string.
- See how movemem() is implemented then apply it to string class.
Dec 3, 2011 at 7:28am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
void diffwords(int readdata, string text [], string diffword [])
{
int counter=0, counter2=0;
string contents="" , diffwords = "" ;
diffwords = diffword[0];
ifstream cin1 ("C:\\c++ data\\HW - String.txt" );
fstream cout1("C:\\c++ data\\HWout -String.txt" );
dare
while (!cin1.eof())
{
cin1>>text[0];
contents = text[0];
if (contents!= diffwords)
{
cout1<< contents;
}
}
cin1.close();
cout1.close();
move( "C:\\c++ data\\HWout -String.txt" , "C:\\c++ data\\HW - String.txt" );
}
Last edited on Dec 3, 2011 at 7:29am UTC
Dec 3, 2011 at 7:36am UTC
what is the point of having a string array? (this isn't attacking your code I am merely wondering)
Dec 3, 2011 at 9:46am UTC
yes there is not point .. in having the string array .. but i have just edited s0u1k33p3r code .. keeping the things to change by him .. :)
Dec 3, 2011 at 5:04pm UTC
yeah i get that just wanted to make sure I understand that correctly
Dec 3, 2011 at 11:15pm UTC
Thanks, I'll try that later on when I get home.
I decided to use a string array since I figured it would work best as I needed to check each word.
Dec 3, 2011 at 11:50pm UTC
When you read a word in, instead of automatically storing it in the array, you could scan the array and see if the word is already there. Only store new words if they're not in the array already.
Dec 4, 2011 at 4:56am UTC
That's what I was initially trying to do but I couldn't figure out how to code it. If you have a sample or an explanation on how to do that it would be extremely helpful.
Dec 4, 2011 at 6:33am UTC
I just show you how to get rid of duplicates, this works for array and easy to understand.
You can just change to adapt with file input :)
Dec 4, 2011 at 4:17pm UTC
Thanks but now I'm getting an error saying "no match for 'operator!=" in the for loop. My code is now
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
void diffwords(int readdata, string text [], string diffword [])
{
int counter=0, counter2=0;
string word;
ifstream cin ("C:\\c++ data\\HW - String.txt" );
while (!cin.eof()){
cin>>text[readdata];
readdata++; //amount of words
}
for (counter; text[counter] != NULL; counter++){
word = text[counter];
bool duplicate = false ;
for (counter2; diffword[counter2] != NULL; counter2++)
{
if (word == diffword[counter2])
{
duplicate = true ;
}
}
if (!duplicate)
diffword[counter2] = word;
}
cout<<diffword<<endl;
return ;
}
I tried looking up the problem but all I got was undeclared variables/headers and from what I can see, everything has been declared.
Last edited on Dec 4, 2011 at 4:23pm UTC
Dec 4, 2011 at 4:20pm UTC
try this one .
for (counter; text[counter] != NULL; counter++)
Last edited on Dec 4, 2011 at 4:20pm UTC
Dec 4, 2011 at 5:17pm UTC
Nope. I still get the same error.
30 C:\Users\Endre\Documents\CISC 1110\Chapter 8\hw#8.cpp no match for 'operator!=' in '*((+(((unsigned int)counter) * 4u)) + text) != 0'
33 C:\Users\Endre\Documents\CISC 1110\Chapter 8\hw#8.cpp no match for 'operator!=' in '*((+(((unsigned int)counter2) * 4u)) + diffword) != 0'
Dec 4, 2011 at 6:29pm UTC
try this one .
for (counter; text[counter] != NULL; counter++)
Sorry did not read the code properly ..
try this one .
for (counter; !text[counter].IsEmpty(); counter++)
similarly
for (counter2; diffword[counter2] != NULL; counter2++)
Last edited on Dec 4, 2011 at 6:30pm UTC
Dec 4, 2011 at 6:44pm UTC
Still gives me an error. I think the problem is trying to use either a string in a for loop since after I changed it to using a while loop it worked fine. Thanks for the help everyone =].
Dec 5, 2011 at 1:25pm UTC
Ah, I see. I managed to figure out how to work it before you made the post but thanks anyways =].