isword program

Using the vector class, and a vector of strings, write a program to determine if the token entered is a word. Your program should prompt the user to repeatedly enter tokens until a token of just "*" is entered (without the quotes). A dictionary file is available on M:\ECE-264\public\sow-twl-crlf.txt. The first seven lines of thefile should be ignored. The last line of the file contains"***END***" (again without the quotes). Words in the file are a combination of both upper and lowercase. Your program should treat upper and lower case as the same.
SAMPLE RUN
Word lookup 1.0
Enter a token:
cat
cat is a word.
Enter a token:
DOG
DOG is a word.
Enter a token:
DoGgiE
DoGgiE is a word.
Enter a token:
viall
viall is not a word.
Enter a token:
*$*#)@%
*$*#)@% is not a word.
Enter a token:*
Exiting...

This is the instructs I was given. I am having trouble with one is recognizing that the word I entered is on the list and second making it recognize the word whether its in uppercase or lowercase. I know of islower, isupper, tolower, and toupper. But I'm having trouble figuring out where to incorporate that into my program. This is what I have so far.

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
  #include <iostream>
#include <vector>
#include <fstream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
using std::ifstream;
using std::getline;



int main()
{
	string token;
	string word;
	vector <string> d;

	
	
	cout<<"Enter a token (to exit enter *) : "<<endl;
	cin>> token;
	
	
		ifstream wordfile;
		wordfile.open("sow-twl-crlf.txt");
	
		if(!wordfile.is_open())
		{
			cout<<"You have a problem...";
			return 1;
		}

		for(int i=0; i<7;i++)
			getline(wordfile, word);

		getline(wordfile, word);
	
		while(word != "***END***")
		{
			d.push_back(word);
			getline(wordfile, word);
		}
		wordfile.close();
	
		if (word == token)
			cout<<token<<" is a word"<<endl;
		else
			cout<<token<<" is not a word"<<endl;
	
		return 0;
	
	
	
}
Hi there,

Shugo3 wrote:
I am having trouble with one is recognizing that the word I entered is on the list and second making it recognize the word whether its in uppercase or lowercase. I know of islower, isupper, tolower, and toupper. But I'm having trouble figuring out where to incorporate that into my program.


You will need to make both the word entered by the user as well as the word read in the dictionary. In order to do so, you need to traverse the string (ie access every little character). A neat little function to do so:

1
2
3
4
5
6
#include <ctype>
inline std::string to_lower(std::string input)
{
    for (char& c : input)  //or if you don't have C++11 for (size_t i=0; i<input.size(); ++i)
        c = tolower(c);
}


Once both the user token and the word in the dictionary are lowercase, you can compare them.
Hope that helps, please do let us know if you have any further questions.

All the best,
NwN
Topic archived. No new replies allowed.