Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,909 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,181 people online right now. Registration is fast and FREE... Join Now!




Need help!

 
Reply to this topicStart new topic

Need help!, Code not doing what it should

devilsson2010
21 Jan, 2008 - 01:08 AM
Post #1

New D.I.C Head
*

Joined: 2 Jan, 2008
Posts: 46


My Contributions
This section of code should take all of the words from wordlist.txt, and filter out any duplicates, however it doesn't work. There are no error messages, but when I check del_wordlist.txt, it is the same as wordlist.txt. Does anyone know why this is happening?

CODE
} else if (sW=="delsort")
{
string temp;
int count=0;        
cout << "** Commencing Sorted Duplicate Word Deletion **" << endl;
vector<string> wordlist;
delwords.open("del_wordlist.txt");
wordslist.open("wordlist.txt");
while (!wordslist.eof())
      {
      wordslist >> temp;
      wordlist.push_back(temp);
      count += 1;
      cout << "** Gathering Word List, " << count << " Word(s) Gathered **" << endl;
      }
      wordslist.close();
      cout << "** Testing Word Duplication on all Words **" << endl;
      for (int p=0;p<wordlist.size();p++)
          {
          for (int k=0;k<15;k++)
              {
              if (wordlist[p]==wordlist[k] && p!=k)
                 {
                 cout << "** Duplicate Detection In Word " << p << ", (" << wordlist[p] << ")" << " **" << endl;                          
                 wordlist[k]="";
                 }  
              }                
          }
      cout << "** Writing Words to del_wordlist.txt **" << endl;
      for (int p=0;p<wordlist.size();p++)
          {
          if (wordlist[p]!="")
             {
          delwords << wordlist[p] << endl;
             }
          }
delwords.close();
cout << "** Word Duplication Detection Finished **" << endl;
break;

User is offlineProfile CardPM
+Quote Post

girasquid
RE: Need Help!
21 Jan, 2008 - 08:25 AM
Post #2

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,295



Thanked: 18 times
Dream Kudos: 725
My Contributions
Are you sure that you're doing your comparison properly?

I don't know if it's the same for C++, but I know in a few languages that using == on strings is generally a bad idea.
User is online!Profile CardPM
+Quote Post

no2pencil
RE: Need Help!
21 Jan, 2008 - 08:39 AM
Post #3

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,141



Thanked: 77 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
C++ offers a string operators in string.h

String Compare : strcmp is one of them.

CODE

/* strcmp example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char szKey[] = "apple";
  char szInput[80];
  do {
     printf ("Guess my favourite fruit? ");
     gets (szInput);
  } while (strcmp (szKey,szInput) != 0);
  puts ("Correct answer!");
  return 0;
}


Will output:
QUOTE

Guess my favourite fruit? orange
Guess my favourite fruit? apple
Correct answer!


This post has been edited by no2pencil: 21 Jan, 2008 - 08:39 AM
User is online!Profile CardPM
+Quote Post

Amadeus
RE: Need Help!
21 Jan, 2008 - 10:53 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
The == operator can be used for C++ string objects, but the .compare() method is the more robust and flexible option.

http://www.cplusplus.com/reference/string/...ng/compare.html

strcmp() is meant for C-style strings made of character arrays/pointers.
User is online!Profile CardPM
+Quote Post

devilsson2010
RE: Need Help!
21 Jan, 2008 - 03:23 PM
Post #5

New D.I.C Head
*

Joined: 2 Jan, 2008
Posts: 46


My Contributions
Ok, so this is what I have, basically the exact same thing. It sort of works, it deletes some of the duplicates, but there are still tons of them. I don't know wha's wrong with it.

CODE
} else if (sW=="delsort")
{
string temp;
string str1;
string str2;
int count=0;        
cout << "** Commencing Sorted Duplicate Word Deletion **" << endl;
vector<string> wordlist;
delwords.open("del_wordlist.txt");
wordslist.open("wordlist.txt");
while (!wordslist.eof())
      {
      wordslist >> temp;
      wordlist.push_back(temp);
      count += 1;
      cout << "** Gathering Word List, " << count << " Word(s) Gathered **" << endl;
      }
      wordslist.close();
      cout << "** Testing Word Duplication on all Words **" << endl;
      for (int i=0;i<wordlist.size();i++)
          {
          str1 = wordlist[i];
          cout << "** Testing Word " << i << " **" << endl;
          for (int k=0;k<15;k++)
              {
              str2 = wordlist[k];
              if (str1.compare(str2) == 0 && k!=i)
                 {
                 cout << "** Duplicate Detection In Word " << i << " with word " << k << ", (" << wordlist[i] << ")" << " **" << endl;                          
                 wordlist[k]="";
                 wordlist.erase (wordlist.begin()+k);
                 }
              }
          }
      cout << "** Writing Words to del_wordlist.txt **" << endl;
      for (int p=0;p<wordlist.size();p++)
          {
          if (wordlist[p]!="")
             {
          delwords << wordlist[p] << endl;
             }
          }
delwords.close();
cout << "** Word Duplication Detection Finished **" << endl;
break;

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 01:33PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month