Finding words in a dictionary file

Ask the user for the name of a file (dictionary) - "I don't need to do this as I am using a Mac.

You may assume that the given file contains only words, and that there are at most 1000 words in the file (there may be fewer)

Read all the words from the file, and store them in an array of type string.

Now ask the user to enter a word that you will search for in the array. If you find it, print the location at which it was found. Otherwise, print a message indicating that the word was not found.

You should consider the first word to be in position 1.

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
  //CSC150 Lab12
//Ian Heinze
//11/20/2015

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{

    string dictionary[1000], word;
    ifstream in_stream;
    in_stream.open("/Users/IanHeinze/Downloads/mydictionary11.txt");
    //cout << "File name: ";
    //cin >> filename;
    cout << endl;
    if (in_stream.fail())

    {
        cout << "Input file opening failed.\n";
        exit(1);
    }

    int i=0;
    while(! in_stream.eof())
    {
        getline(in_stream, dictionary[i]);

        i++;
    }

    int size=i;
    int a;
    bool found;

    while(word != "stop")

    {
        cout << "Search for: ";
        cin >> word;

        if(word =="stop")
            {
                cout << "Bye!" << endl;
                break;
            }

        for(int b=0; b < size; b++)
        {
            found = false;
            if (word == dictionary[b])
            {
                found = true;
                break;
                a=b;
            }
        }
        a=a+1;
        if (found==true)
        {
            cout << "'" << word << " was found in position " << a << endl;
        }
        else
        {
            cout << "Sorry, word not found." << endl;
        }
    }


}



So this is the code I have at this point. No matter what it prints out that the word was not found. The dictionary file I'm using has the words:

apple
bear
cat
dog
egg
file
google
hello
iphone
jeep

Now, if I take one of the '=' from the 'found == true' out, then it just acts as a counter. No matter what I enter it says it's found and just adds one to the count. I'm not sure what's wrong with this code, and my instructor believes it might be something with the fact that several others and I are using Mac's. Just looking for some help and other opinions on what we could all do to make our code work? Thanks a million!
I think the mistake is at line 57.

1
2
3
4
5
6
if (word == dictionary[b])
{
    found = true;
    break;
    a=b;
}


You assign b to a after the break statement, meaning the assignment will never even be executed, since the break statement jumps out of the loop already. Try moving the assignment up one place, like this:

1
2
3
4
5
6
if (word == dictionary[b])
{
    found = true;
    a=b;
    break;
}


That way the assignment will be executed and it will give you the requested index.
You may want to print out your dictionary after you read the file, to insure you're reading the file correctly. You may also want to consider using a std::vector instead of the array.

Edit: I didn't test your file reading code but this seems to work properly:
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
 //CSC150 Lab12
//Ian Heinze
//11/20/2015

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main()
{

    std::vector<string> dictionary {"apple", "bear","cat","dog",
                             "egg","file","google","hello",
                             "iphone","jeep"};


    string word;

    int size = dictionary.size();
    int a = 0;
    bool found = false;

    while(word != "stop")

    {
        cout << "Search for: ";
        cin >> word;

        if(word =="stop")
            {
                cout << "Bye!" << endl;
                break;
            }

        for(int b=0; b < size; b++)
        {
            found = false;
            if (word == dictionary[b])
            {
                found = true;

                a=b;
                break;
            }
        }
        a=a+1;
        if (found==true)
        {
            cout << "'" << word << " was found in position " << a << endl;
        }
        else
        {
            cout << "Sorry, word not found." << endl;
        }
    }


}
Last edited on
@jlb
Didn't you see this?
store them in an array of type string.


In a normal program a vector would be the best option, but in assignments it's better to follow the instructions.
Didn't you see this?

Yes, but what's your point? I skipped over half of the assignment so using the vector is a minor issue. My code was meant only to show that the OP's logic in the remaining code is sound. Even without moving that assignment you pointed out the find logic works, the "position" reporting was incorrect but the rest of the logic works.


I edited the break statement but still no luck. My instructor just sent me this in an email as to what the problem could be. Any ideas guys?

1. Try #include <string> and test your original code first,

if it does not work,

2. Try to use the compare function

Your code will be something like:

if(s.compare(dictionary[i]))
.......

assume s is the target string and dictionary is your dictionary array


I of course tried the #include <string> but to no avail. And I'm not exactly sure what the compare fuction is or how to implement it in my code..
Yes you should be #including the string header.

I doubt that the compare() member function will help, but you replace the operator== with the compare function.

You really need to check that you actually read the file correctly. As I stated in my previous post your "find" logic seems to be correct.
Does the dictionary file you use come from a Windows system? If so it might use \r\n as newline. A Mac system usually uses only a \n, meaning the \r isn't marked as part of the newline. So every string you read from your dictionary file will contain a trailing \r, which would cause the == operator to fail.

Windows systems automatically convert \r\n to \n when using standard library functions, while Mac systems will not. This might be causing the issue.
Last edited on
Yes it is a windows file, and I've made sure my code is reading the file correctly. If I add in a statement to print out the file contents, it does with no problem.

@Shadowwolf Since the file we were to be using came from a windows system, how exactly would I alter my code to make it work? Or would it be easier for me to just make a file on my Mac with the words and use that as my input file?
It would probably be easier to make the file on your Mac, but if you want to do it in the program you could check if the input ends with a carriage return, and if so, delete it. You could use something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int i=0;
while(! in_stream.eof())
{
    getline(in_stream, dictionary[i]);
    std::string::size_type length = dictionary[i].size();
    if(length > 0) //Check if we have at least one character
    {
        if(dictionary[i][length-1] == '\r') //Check if it ends with a carriage return
        {
            dictionary[i] = dictionary[i].substr(0,length-1); //Remove last character
        }
    }
    i++;
}


Optionally, if you have access to a C++11 compiler, you could use dictionary[i].pop_back(); instead of the dictionary[i] = dictionary[i].substr(0,length-1); line.
One of the reasons I said to verify your file is because of the differences with the line endings. The easiest way to tell if it is actually a line ending problem would be to print the string along with the length of the string. If the string appears to have one more character then it probably is a non-printing character in the stream. If your instructor is using a Windows machine he will not have this issue. If this is the case it would probably be easier to fix the line ending issue before you parse it with your program. Many text editors have the ability to fix this issue so you can first try that. If not look for programs with the names of dos2unix or possibly follow one of the methods in this link:
Yes it is a windows file, and I've made sure my code is reading the file correctly. If I add in a statement to print out the file contents, it does with no problem.
http://www.microhowto.info/howto/convert_the_line_endings_in_a_text_file_from_dos_to_unix_format.html
Topic archived. No new replies allowed.