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

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




Need help with program that checks for duplicate names

 
Reply to this topicStart new topic

Need help with program that checks for duplicate names, I'm trying to make a program that reads all of the words from a te

devilsson2010
19 Jan, 2008 - 02:29 AM
Post #1

New D.I.C Head
*

Joined: 2 Jan, 2008
Posts: 46


My Contributions
I'm trying to make a program that reads all of the words from a text file, puts them all in an array, then checks to see if any are duplicates and if they are then they are discarded. However, for some reason it won't even open the file to read the text. (I have narrowed the problem down to the "&& !myfile1.eof() && myfile2.is_open()", but I need that). Anyone have any ideas?

CODE
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;

string sLine;
int iNumWords;
string sWord;
string sRead;
fstream myfile1;
fstream myfile2;
string sTempWord;


void NumWords()
{
myfile1.open("words.txt"); // wordlist.txt is the dictionary file
   if (myfile1.is_open() && !myfile1.eof())
   {  
    for (int x=1;;x++)
       {
       getline(myfile1,sLine);
            if (sLine == "")
               {
               iNumWords=x;
               break;
               }
       cout << sLine << endl;
       }
      myfile1.close();
   } else { cout << "words.txt does not exist!" << endl; }
}



int main()
{
    NumWords(); // Find out how many words are in the dictionary filev
    
    cout << "Please enter 'words' to start the filtering process.\n" << endl;
    cin >> sWord;
    
        if (sWord == "words")
           { // if words is entered
             myfile1.open("words.txt");
             myfile2.open("new_words.txt", ios::app);
                 if (myfile1.is_open() && !myfile1.eof() && myfile2.is_open())
                 { // If files are open
                      string iArray[iNumWords];
                      for (int m=0;m<=iNumWords;m++)
                      {
                      getline(myfile1,sRead);
                      iArray[m] = sRead;
                      }
                      myfile1.close();
                   for (int n=0;n<=iNumWords;n++)
                    {
                     sTempWord = iArray[n];
                         for (int w=0;w<=iNumWords;w++)
                         {
                         if (sTempWord == iArray[w] && n!=w)
                            {iArray[w] = "";}    
                         }      
                    }
                    
                   for (int u=0;u<=iNumWords;u++)
                      {
                         if (iArray[u] == "") {;}
                         else {  
                      myfile2 << iArray[u] << endl;}
                      }
                      myfile2.close();
                
                 } else {cout << "Files are unable to be opened!" << endl;} // End if files are open and start id uanble to be opened
          
          
          
           } // end if words is entered
    
    
    
    system("PAUSE");
    return 0;
} // End main function


User is offlineProfile CardPM
+Quote Post

Nayana
RE: Need Help With Program That Checks For Duplicate Names
19 Jan, 2008 - 05:18 AM
Post #2

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
Actually there is nothing wrong with your if statement. The problem lies in the line where you are trying to open the second file.
CODE

myfile2.open("new_words.txt", ios::app);

You are specifying the append flag, which sets the position indicator to the end of the file. You also need to specify the output flag. You can combine flags using the or operator, which is |

CODE

myfile2.open("new_words.txt", ios::app | ios::out);


Now good luck fixing the other bugs.
User is offlineProfile CardPM
+Quote Post

devilsson2010
RE: Need Help With Program That Checks For Duplicate Names
19 Jan, 2008 - 12:55 PM
Post #3

New D.I.C Head
*

Joined: 2 Jan, 2008
Posts: 46


My Contributions
Ok, I tried a lot of stuff and this didn't work. So I divided the program up, so now one program countes how many numbers there are, then another program will sort it A-Z. But the sorting program isn't working, it always ends up with
"hi
hi
hello
cat
puppy
dog
mommy
daddy
baby
bed
run
people"

Can anyone help me?

wordlist.txt:

hi
hi
hello
cat
puppy
dog
mommy
daddy
baby
bed
run
people


code:
CODE
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int iNumWords;
string sScramb;
string sRead;
string sTemp2;
ifstream myfile;
ofstream myfile2;
string wordA;
string wordB;


int main()
{
  cout << "Please enter 'sort' to sort wordlist.txt.\n" << endl;  
  cin >> sScramb;
  
  if (sScramb == "sort")
     {
    cout << "How many words?" << endl;
    cin >> iNumWords;
        myfile.open("wordlist.txt");
        myfile2.open("newwords.txt");
          if (myfile.is_open() && !myfile.eof() && myfile2.is_open())
             {
               string iArray[iNumWords];
                                                            
           for (int x=0;x<=iNumWords;x++)
               {
               getline(myfile,sRead);
               iArray[x] = sRead;
               }
               for (int i=0;i<=iNumWords;i++)
                   {
                   wordA = iArray[i];
                      for (int l=0;l<iNumWords;l++)
                          {
                          wordB = iArray[l];
                          if (wordA > wordB)
                          {sTemp2=wordA;
                          cout << iArray[i] << " " << iArray[l] << endl;
                          iArray[i] = iArray[l];
                          iArray[l] = sTemp2;
                          cout << iArray[i] << " " << iArray[l] << "\n" << endl;
                          }
                          }
                  
                   }
    cout << "Array is:" << endl;          
    for (int k=0;k<=iNumWords;k++)
        {
        cout << iArray[k] << endl;
        }
             }
            
     }
    
    system("PAUSE");
    return 0;
}


This post has been edited by devilsson2010: 19 Jan, 2008 - 12:56 PM
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Need Help With Program That Checks For Duplicate Names
20 Jan, 2008 - 03:36 AM
Post #4

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
OK, you are actually quite close. I'll just point out your mistakes.

CODE

// if iNumWords = 5, then this counts from 0 to 5, which is actually SIX items
// you should change the condition to: i<iNumWords
for (int i=0;i<=iNumWords;i++)  {
  // wordA = iArray[i]; // you don't need wordA or wordB
  // for the same reason as above, the condition should be: l<iNumWords-1
  for (int l=0;l<iNumWords;l++) {
    // wordB = iArray[l]; // don't need it
    if (iArray[i] > iArray[l]) { // I changed it to use the index instead of wordA/B
      cout << iArray[i] << " " << iArray[l] << endl;

      // I moved the swap operation into one block (instead of being seperated by cout)
      sTemp2=iArray[l]; // use iArray[l] instead of wordA
      iArray[i] = iArray[l];
      iArray[l] = sTemp2;

      cout << iArray[i] << " " << iArray[l] << "\n" << endl;
    }
  }
}


There you go.
User is offlineProfile CardPM
+Quote Post

devilsson2010
RE: Need Help With Program That Checks For Duplicate Names
20 Jan, 2008 - 08:42 AM
Post #5

New D.I.C Head
*

Joined: 2 Jan, 2008
Posts: 46


My Contributions
Hurray, it works! Quite unpractical though smile.gif, thanks!!

This post has been edited by devilsson2010: 20 Jan, 2008 - 08:42 AM
User is offlineProfile CardPM
+Quote Post

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

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