how to write this spell check program in c++

Feb 26, 2009 at 5:46pm
ok so heres the assignment
Spell Checker
You will write a program that reads a file and performs a spell check on the file. You should specify the name of the file from the command line.
Example
SpellCheck example.txt
Your program should read in a word list to use for the spell check. You can find a word list online using google or some other search engine. A suitable word list should list a single word per line in the file. You will read this word list into some data structure. An array can be used, but you will need to calculate the size of the array before running the program. You may also want to look into an alternate data structure like a vector or list or hash table. Once you read the dictionary into memory, you will read the file to be checked, and compare each word to the dictionary to determine if the word is in the list. If the word is not in the list, you should print the word and its location in the text. You do not need to offer suggestions for the incorrect word, just identify it as misspelled.
For instance, given the following input:
I lvoe programming class. It is teh best!
You program should output
Misspelled Words
Location Word
2 lvoe
7 teh
__________________________________________________ ______________
so basically this is how far i got that i realized i was confused can some1 help me out but like keep the code as simple as possible
i know i have to add a array and some strings and chars and/or binaryfunction something im just not sure how to do it.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream> 
#include <conio.h> 
#include <fstream> 
#include <cstring>

using namespace std; 
int main() 
{ 
Int item[5]
Int I
ifstream inFile;
inFile.open("C:\\temp\\datafile.txt");
Char str[30];
Cin.get(str,31);
Last edited on Feb 26, 2009 at 5:46pm
Feb 26, 2009 at 6:40pm
You do realize why nobody is helping you out, right? This assignment is obviously a number of weeks into the course and your code makes it blatantly obvious that you have not even attempted this assignment. In fact, it makes me wonder if you even go to class.

That being said, I will still help you out; if you put some effort into it. Start by accepting the file name from the command line; you can do a search for examples on this site.
Feb 26, 2009 at 7:40pm
1
2
3
4
5
6
7
8
9
10
#include <iostream> 
#include <conio.h> 
#include <fstream> 
#include <cstring>

using namespace std; 
int main ( int argc, char *argv[] )
{ 
if ( argc != 2 )
cout<<"usage: "<< argv[0] <<" <C:\\temp\\datafile.txt>\n";



good so far ?

Last edited on Feb 26, 2009 at 7:41pm
Feb 26, 2009 at 7:48pm
That's a start. Assuming that return 0; } follows. I also don't see any reason to include cstring or conio.h at this point.

Now just open the ifstream with argv[1], similar to before.
Feb 26, 2009 at 8:25pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream> 
#include <conio.h> 
#include <fstream> 
#include <cstring>

using namespace std; 
int main ( int argc, char *argv[] )
{ 
if ( argc != 2 )
cout<<"usage: "<< argv[0] <<" <C:\\temp\\datafile.txt>\n";
return 0;
}
{
std::ifstream file; 
file.open(argv[1]);
}
Last edited on Feb 26, 2009 at 8:26pm
Feb 26, 2009 at 9:54pm
Don't forget to close the ifstream after its use.

Also, I just noticed, there will be two ifstreams; one to read in a hard-coded dictionary data file and then the one that opens the command line argument file to be spell-checked.

Then you'll need a way to store a "dictionary" of words. Any ideas? Something sorted with fast lookup, perhaps?
Feb 26, 2009 at 10:12pm
well he said that i dont have to make it fast..
the file with the dictionary is datafile.txt

i thought what i already coded was the dictionary alrdy in so i would just need to somehow add a command line that will let a user input words that r to be checked, something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream> 
#include <conio.h> 
#include <fstream> 
#include <cstring>
string words;
using namespace std; 
int main ( int argc, char *argv[] )
{ 
if ( argc != 2 )
cout<<"usage: "<< argv[0] <<" <C:\\temp\\datafile.txt>\n";
return 0;
}
{
std::ifstream file; 
file.open(argv[1]);
}
{
cout <<''enter words to be checked by spell checker";
getline(cin, words)
cout << words << "you typed"
}  


not sure if i did that right
Feb 27, 2009 at 12:12am
I was under the same impression, but after I re-read the assignment, I think the program should:

1. load datafile.txt into a data structure
1
2
3
ifstream data( "datafile.txt" );  // hard-coded dictionary file
// load words into data structure
data.close();


2. process the argv[1] file word by word and look up the words in that data structure
1
2
3
4
5
6
7
ifstream input( argv[1] );  // the file to be spell checked passed on the command line
string word;
while( input >> word )
{
    // look up word in dictionary structure
}
input.close();


If you want to accept a string entered by the user for now, that will work until you get it working...it's up to you.

The testing for the appropriate number of arguments and outputting the usage is also a good idea; just be sure to test it.
Last edited on Feb 27, 2009 at 12:16am
Feb 27, 2009 at 1:09am
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
#include <iostream> 
#include <conio.h> 
#include <fstream> 
#include <cstring>
string words;
using namespace std; 
int main ( int argc, char *argv[] )
{ 
if ( argc != 2 )
cout<<"usage: "<< argv[0] <<" <C:\\temp\\datafile.txt>\n";
return 0;
}
{
ifstream data( "datafile.txt" );
data.close();
}
{
cout <<''enter words to be checked by spell checker";
getline(cin, words)
cout << words << "you typed"
}  
ifstream input( argv[1] );  // the file to be spell checked passed on the command line
string word;
while( input >> word )
{
}
input.close(); 


is that correct so far and however you think i should do this is fine..
Feb 27, 2009 at 2:17am
Compile it before going any farther.
Feb 27, 2009 at 2:23am
i would but im on my normal computer i dont have c++ on it :( um ill do it tommorow i guess or you could just compile it..
Topic archived. No new replies allowed.