A spell check...

Here's my code. I need a lot of help. Bonk.txt is the file that has to be read, and Dictionary.txt is the list of words. What I'm doing now is simply to make sure the void spell(string Bonk[200]) is at least opening the file Bonk.txt correctly. That's why everything else is commented out. I'm just not understanding what to make into arrays and how to call the functions. There are other things but one thing at a time I guess right? Oh and try not to beat me up too badly since I'm terrible with this stuff!



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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

//same problems here

//takes file name and spell checks it
void spell (string Bonk[200])
{
	ifstream bonkfile("Bonk.txt");
	bonkfile.open("Bonk.txt");
	cout<<bonkfile;
	
	for (int i = 0; !bonkfile.eof(); i++)
	{

		bonkfile.getline(bonkfile[i]);

	}
}

/*void deletePunctuation(string &word, char punctuation)
{
int b_character(0);
b_character = word.find(punctuation);
while (b_character >= 0)
{

word.replace (b_character, 1, "");
b_character= word.find(punctuation);
}
}

//This function takes a word from input file stream and removes punctuation
void wordfilter(string& word) {

deletePunctuation(word, '(');
deletePunctuation(word, ')');
deletePunctuation(word, '!');
deletePunctuation(word, ',');
deletePunctuation(word, '.');
deletePunctuation(word, '"');
deletePunctuation(word, '\'');
}

//This function returns true if a word in input file stream
bool inDictionary(string word, string dictionary[198])
{
word = "";

bool word_is_Found = false;
while(!dictionary[198].eof())//loop runs through all words in dictionary text file
{
string Dictionary_Word = "";

dictionary[198] >> Dictionary_Word;

if( word == Dictionary_Word) //if the word is in fact found in the dictionary text file
{
word_is_Found = true;//Condition is now true
break;// Break function stops while loop after word is found
}
}
dictionary[198].close();

if(!word_is_Found) 
{
cout << word << endl; //if the word is found under the false condition, print that word
}

}*/


int main () {

	string Bonkfile;
	cout<<"Enter the spell check file name"<<endl;
	cin >> Bonkfile;
	if (Bonkfile == "Bonk.txt")
	{
		spell(Bonkfile);

	}

	system("pause");
	return 0;


}
Last edited on
closed account (3CXz8vqX)
... There are code tags, you should use them.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
 #include <iostream>
#include <string>
#include <fstream>
#include <cstdio>


using namespace std;


//takes files name and spell checks it
void spell (string Bonk)
{
	string Bonkstring;
	ifstream Bonkstream;
	ifstream Dictionary;

	Bonkstream.open("Bonk.txt");
	Dictionary.open("Dictionary.txt");


	while(!Bonkstream.eof())//loop runs through all words in dictionary text file
	{
		getline(cin, Bonkstring);
		cout << Bonkstring;
	}
}

void deletePunctuation(string &word, char punctuation)
{
	int b_character(0);
	b_character = word.find(punctuation);
	while (b_character >= 0)
	{

		word.replace (b_character, 1, "");
		b_character= word.find(punctuation);
	}
}

//This function takes a word from input file stream and removes punctuation
void wordfilter(string& word) {

	deletePunctuation(word, '(');
	deletePunctuation(word, ')');
	deletePunctuation(word, '!');
	deletePunctuation(word, ',');
	deletePunctuation(word, '.');
	deletePunctuation(word, '"');
	deletePunctuation(word, '\'');
}

/**************************************************************
* This function returns true if a word in input file stream *
* (i.e. bonk.txt) is in the dictionary, false otherwise. *
* @param word a string that contains a word to be checked *
* @return the Boolean status of the word (i.e. true or false) *
**************************************************************/
/*bool inDictionary(string word, string dictionary[198])
{
	int index = 0;
	bool in_boolean = false;
	while (index < 198 && in_boolean == true)
	{
		in_boolean = strcmp(word, dictionary[index]);
		index++;
		
	}


	return in_boolean;

}*/


/**************************************************************
* This is the main function of the program. *
* @return a value to terminate the program successfully *
**************************************************************/
int main ()
{
	string Bonk;
	string word;
	char punctuation;
	string Bonkfile;
	cout<<"Enter the spell check file name"<<endl;
	cin >> Bonkfile;

	if (Bonkfile == "Bonk.txt")
	{
		deletePunctuation(word, punctuation);
		spell(Bonk);
		//another if in here for boolean
	}

	else 
	{
		cout<<"That is not the correct file name. Enter again.";
		cin>>Bonkfile;
	}


	return 0;

}
What exactly is the problem ? you looks to be opening the file correctly.
Yeah you can use the is_open(); member function to check whether the file is really open, to avoid crashing.

eg:
1
2
3
istream in;
in.open("test.txt");
if(in.is_open()) ......  //programme continues 


HTH,
Aceix.
Here's what I've come up with since then. I know its been a long long time. But im still working at it. The compiler says that the variable "punctuation" is being used with being initialized. I've underlined what is being underlined in red in c++.


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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <string>
#include <fstream>
#include <cstdio>


using namespace std;


//takes files name and spell checks it
void spell (string Bonk)
{
	string Bonkstring;
	ifstream Bonkstream;
	ifstream Dictionary;

	Bonkstream.open("Bonk.txt");
	Dictionary.open("Dictionary.txt");


	while(!Bonkstream.eof())//loop runs through all words in dictionary text file
	{
		getline(cin, Bonkstring);
		cout << Bonkstring;
	}
}

void deletePunctuation(string &word, char punctuation)
{
	int b_character(0);
	b_character = word.find(punctuation);
	while (b_character >= 0)
	{

		word.replace (b_character, 1, "");
		b_character= word.find(punctuation);
	}
}

//This function takes a word from input file stream and removes punctuation
void wordfilter(string& word) {

	deletePunctuation(word, '(');
	deletePunctuation(word, ')');
	deletePunctuation(word, '!');
	deletePunctuation(word, ',');
	deletePunctuation(word, '.');
	deletePunctuation(word, '"');
	deletePunctuation(word, '\'');
}

/**************************************************************
* This function returns true if a word in input file stream *
* (i.e. bonk.txt) is in the dictionary, false otherwise. *
* @param word a string that contains a word to be checked *
* @return the Boolean status of the word (i.e. true or false) *
**************************************************************/
bool inDictionary(string word, string dictionary[198])
{
	int index = 0;
	bool in_boolean = false;
	while (index < 198 && in_boolean == true)
	{
		in_boolean = strcmp(word, dictionary[index]);
		index++;
		
	}


	return in_boolean;

}


/**************************************************************
* This is the main function of the program. *
* @return a value to terminate the program successfully *
**************************************************************/
int main ()
{
	string Bonk;
	string word;
	char punctuation;
	string Bonkfile;
	cout<<"Enter the spell check file name"<<endl;
	cin >> Bonkfile;

	if (Bonkfile == "Bonk.txt")
	{
		deletePunctuation(word, punctuation);
		spell(Bonk);
		//another if in here for boolean
	}

	else 
	{
		cout<<"That is not the correct file name. Enter again.";
		cin>>Bonkfile;
	}


	return 0;

}
Topic archived. No new replies allowed.