Different words in file

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
Removing a single element from the middle of an array is hard. Use a vector, or use memmove.
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.
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
what is the point of having a string array? (this isn't attacking your code I am merely wondering)
yes there is not point .. in having the string array .. but i have just edited s0u1k33p3r code .. keeping the things to change by him .. :)
yeah i get that just wanted to make sure I understand that correctly
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.
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.
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.
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
27
28
29
30
// Demonstrate strtok(). 
#include <iostream> 
#include <cstring> 
 
using namespace std; 

int main() { 

	char notSorted[] = "abbbbccdddddeff";
	char sorted[80] = "";
	
	for(int i = 0;notSorted[i] != NULL;i++)
	{
	     char target = notSorted[i];
	     bool already = false;
	     int j = 0;
	     for(j = 0; sorted[j] != NULL; j++)
	     {
	            if(target == sorted[j])
	            {
	                   already = true;
	                   break;
	            }
	      }
	     if(!already)
	          sorted[j] = target;
	}
	using std::cout;
	cout << "Sorted : " << sorted;
}
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 :)
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
try this one .

for (counter; text[counter] != NULL; counter++)
Last edited on
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'
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
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 =].

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.


This is my best attempt at a hint.

1
2
3
4
     while (!cin.eof()){
           cin>>text[readdata];
           readdata++; //amount of words
     }


This is the loop that reads the data in. If you want to do something for each word you read in, as you read it, then it will have to go inside this loop.
Ah, I see. I managed to figure out how to work it before you made the post but thanks anyways =].
Topic archived. No new replies allowed.