#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
Need help with program that checks for duplicate namesI'm trying to make a program that reads all of the words from a te
Page 1 of 1
4 Replies - 1106 Views - Last Post: 20 January 2008 - 09:42 AM
#1
Need help with program that checks for duplicate names
Posted 19 January 2008 - 03:29 AM
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?
Replies To: Need help with program that checks for duplicate names
#2
Re: Need help with program that checks for duplicate names
Posted 19 January 2008 - 06:18 AM
Actually there is nothing wrong with your if statement. The problem lies in the line where you are trying to open the second file.
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 |
Now good luck fixing the other bugs.
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 |
myfile2.open("new_words.txt", ios::app | ios::out);
Now good luck fixing the other bugs.
#3
Re: Need help with program that checks for duplicate names
Posted 19 January 2008 - 01:55 PM
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:
"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:
#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 January 2008 - 01:56 PM
#4
Re: Need help with program that checks for duplicate names
Posted 20 January 2008 - 04:36 AM
OK, you are actually quite close. I'll just point out your mistakes.
There you go.
// 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.
#5
Re: Need help with program that checks for duplicate names
Posted 20 January 2008 - 09:42 AM
Hurray, it works! Quite unpractical though
, thanks!!
This post has been edited by devilsson2010: 20 January 2008 - 09:42 AM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|