-
Code error
:confused: :confused:
Code:
#include <iostream>
#include <fstream>
#include <conio2.h>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
int main()
{
vector <string> Words;
char letterlist[256];
char wordlist[256];
int wordcount = 0;
int x = 0, y = 0;
int inputlength;
int wordlength;
bool Positive = false, quit = false;
char exit;
while (quit == false)
{
cout << "Letter list: \n(All lowercase)";
gotoxy(14,1);
cin >> letterlist;
inputlength = strlen (letterlist);
ifstream OpenFile("dict.txt");
while(! OpenFile.eof())
{
x = 0;
y = 0;
wordcount++;
OpenFile.getline(wordlist, 256);
wordlength = strlen (wordlist);
while (x <= wordlength)
{
if (wordlist[x] != wordlist[y])
{
y += 1;
if (y == inputlength + 1)
{
Positive = false;
break;
}
}
else
{
++x;
Positive = true;
}//close else statement
}//close inner while loop
if (Positive == true)
{
Words.push_back(wordlist);
}
}//close outer while loop
clrscr();
cout << "Finished!\nWords Returned Positive:\n";
for (int x = 0; x < Words.size(); ++x)
{
cout << '\t' << Words[x] << '\n';
}
system("pause");
clrscr();
cout << "Quit Y/N?:";
cin >> exit;
if (exit == 'y' || exit == 'Y')
quit = true;
if (exit == 'n' || exit == 'N')
quit = false;
}//close biggest wile loop
}//close main()
it is supposed to find all the words that you can make with the letters that you input, and it uses dict.txt as a reference. but all it ends up doing is loading every word in dict.txt into vector "Words" and then printing to the screen. why wont it work?
-
Well firstly
Code:
if (wordlist[x] != wordlist[y])
should be
Code:
if (wordlist[x] != letterlist[y])
Also be aware that you can overrun your letterlist buffer, and should also check for errors when opening your input stream.
-
> and should also check for errors when opening your input stream.
yea, i guess ill add that later, but its just for personal use, nothing big
hahahah, cant believe that was the mistake :D :D :D
-
> while(! OpenFile.eof())
The reason you shouldn't do this is in the FAQ
-
i actually picked openfile.eof() up from another tutorial i was reading, but thanks
-
Also not to sound evil and mean but using a more descriptive title is nice as well. Like
Letter Comparision Error or something like that
-
oh ok, didnt really know it mattered, but i got another minor problem:
it will keep on pushing blank lines (like sometimes 10 at a time) into the vector and there are none in dict.txt :confused:
-
Can you post the updated code?
-
Code:
#include <iostream>
#include <fstream>
#include <conio2.h>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
int main()
{
vector <string> Words;
char letterlist[256];
char wordlist[256];
int wordcount = 0;
int x = 0, y = 0;
int inputlength;
int wordlength;
bool Positive = false, quit = false;
char exit;
while (quit == false)
{
cout << "Letter list: \n(All lowercase)";
gotoxy(14,1);
cin >> letterlist;
inputlength = strlen (letterlist);
ifstream OpenFile("dict.txt");
while(! OpenFile.eof())
{
x = 0;
y = 0;
wordcount++;
OpenFile.getline(wordlist, 256);
wordlength = strlen (wordlist);
while (x <= wordlength)
{
if (wordlist[x] != letterlist[y])
{
y += 1;
if (y == inputlength + 1)
{
Positive = false;
break;
}
}
else
{
++x;
Positive = true;
}//close else statement
}//close inner while loop
if (Positive == true)
{
Words.push_back(wordlist);
}
}//close outer while loop
clrscr();
cout << "Finished!\nWords Returned Positive:\n";
for (int x = 0; x < Words.size(); ++x)
{
cout << '\t' << Words[x] << '\n';
}
system("pause");
clrscr();
cout << "Quit Y/N?:";
cin >> exit;
if (exit == 'y' || exit == 'Y')
quit = true;
if (exit == 'n' || exit == 'N')
quit = false;
}//close biggest wile loop
}//close main()
i have only made DonFiasco's change so far, so there isnt a lot of change
-
> i actually picked openfile.eof() up from another tutorial i was reading, but thanks
I'll leave you to figure out why the last line of the file appears twice in your word list then.