getting stuck in a loop

I am trying to write a crude-ish t9 type of autocomplete program. The way it works so far is it populates dict with like 100000 words, input is meant to take 3 letter, just like on a phone, then it goes through the dict and finds all the words that start with those letters, after this, the vector gets resized to one bigger, another string of 3 letters is input, then it gets stuck in the for ic1 and for ic2 loops

Can anyone see why it is getting hung up?

p.s. I am sure this method is barbaric, i just need to get it working.

Thank you all.

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
/*
 * autoc.cpp
 *
 *  Created on: Mar 15, 2013
 *      Author: michaela
 */

#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>

int main()
{
	std::vector<std::vector<std::string> > dict;
	std::vector<std::vector<std::string> > curPermut;
	std::vector<std::string>::iterator id, ic1, ic2;
	std::ifstream inf;
	std::string input, temp, word, sentence;
	bool done = false;
	int index;

	inf.open("dict.txt");
	dict.resize(26);

	while(!inf.eof())
	{
		inf >> word;
		dict[tolower(word[0]) - 97].push_back(word);
	}
	inf.close();

	temp = "";
	word = "";
	sentence = "";

	std::cout << "Press 1 to accept autocomplete word\n" <<
				  "Press 0 to exit\n" << std::endl;

	index = 0;

	while(!done)
	{
		curPermut.resize(index + 1);
		input = "";
		std::cin >> input;
		std::cout << input << std::endl;
		if(input == "0")
		{
			done = true;
		}
		else if(input == "1")
		{
			sentence += "";
			std::cout << sentence << std::endl;
			index = 0;
			curPermut.clear(); curPermut.resize(0);
		}
		else if(input != "0")
		{
			if(curPermut.size() > 1)
			{
				for(int i = 0; i < input.length(); ++i)
				{
					for(ic1 = curPermut[index - 1].begin(); ic1 != curPermut[index - 1].end(); ++ ic1)
					{
						temp = (*ic1).substr(0, index) + input[i];
						for(ic2 = curPermut[index - 1].begin(); ic2 != curPermut[index - 1].end(); ++ic2)
						{
							if(temp == (*ic2).substr(0, temp.length()))
							{
								curPermut[index].push_back(*ic2);
							}
						}
					}
				}
			}
			else
			{
				for(int i = 0; i < input.length(); ++i)
				{
					for(id = dict[tolower(input[i]) - 97].begin(); id != dict[tolower(input[i]) - 97].end(); ++id)
					{
						curPermut[index].push_back((*id));
					}
				}
			}
		}

		std::sort(curPermut[index].begin(), curPermut[index].end());
		for(ic1 = curPermut[index].begin(); ic1 != curPermut[index].end(); ++ ic1)
		{
			std::cout << (*ic1) << std::endl;
		}
		index += 1;
	}

	return 0;
}
Last edited on
Can you tell us which loop it gets stuck?(line numbers?)
BTW, this is barbaric indeed.:)
line 67 to 74.

What is a better approach for t9?
Where is index updated?

I see where it is set to 0. I don't see where it is ever set to anything other than 0, which makes constructs like curPermut[index - 1].begin() very suspicious.

Ahh, it's updated much later. This mouse button of mine is going to get me in trouble. This mouse needs to be replaced. Keeps double-clicking when I single-click, which doesn't go over so well with the find dialog.

It would appear, though, that index can be 0 when the code snippet quoted above occurs which would not be good.
Last edited on
Sorry, I figured it out.

I had one too many for loops. Bleh, that is what I get for not writing anything out first.

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
/*
 * autoc.cpp
 *
 *  Created on: Mar 15, 2013
 *      Author: michaela
 */

#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>

int main()
{
	std::vector<std::vector<std::string> > dict;
	std::vector<std::string> list1, list2;
	std::vector<std::string>::iterator id, ic1, ic2;
	std::ifstream inf;
	std::string input, temp, word, sentence;
	bool done = false;
	int index;

	inf.open("dict.txt");
	dict.resize(26);

	while(!inf.eof())
	{
		inf >> word;
		dict[tolower(word[0]) - 97].push_back(word);
	}
	inf.close();

	temp = "";
	word = "";
	sentence = "";

	std::cout << "Press 1 to accept autocomplete word\n" <<
				  "Press 0 to exit\n" << std::endl;

	index = 0;

	while(!done)
	{
		input = "";
		std::cin >> input;
		std::cout << input << std::endl;
		if(input == "0")
		{
			done = true;
		}
		else if(input == "1")
		{
			sentence += word;
			std::cout << sentence << std::endl;
			index = 0;
			list1.clear(); list2.clear();
		}
		else if(input != "0")
		{
			if(index > 0)
			{
				for(int i = 0; i < input.length(); ++i)
				{
					for(ic1 = list1.begin(); ic1 != list1.end(); ++ic1)
					{
						temp = (*ic1).substr(0, index) + input[i];
						if(temp == (*ic1).substr(0, temp.length()))
						{
							list2.push_back(*ic1);
						}
					}
				}
				list1.clear();
				list1 = list2;
				list2.clear();
			}
			else
			{
				for(int i = 0; i < input.length(); ++i)
				{
					for(id = dict[tolower(input[i]) - 97].begin(); id != dict[tolower(input[i]) - 97].end(); ++id)
					{
						list1.push_back((*id));
					}
				}
			}

			std::sort(list1.begin(), list1.end());
			word = list1[0];
			std::cout << word << std::endl;
			index += 1;
		}
	}

	return 0;
}


This is very crude, but I was just trying to figure out how t9 works. Then it gets pretty.
Topic archived. No new replies allowed.