(unwanted) Non-deterministic behavior

The program (my first from scratch) is supposed to take 492 data pairs from a text file written in this format:

m
94.2
k
89.4
i
87.1
a
86.9

...and sort them into two arrays: consonants and vowels. It is then supposed to display each array (in a list with each pair formatted like so: m,94.2).

It compiles, and sometimes it works. But other times the arrays are missing entries, or are too large and overflow, or are too small and leave blank entries at the bottom (which will become a problem as I build off this). How can random behavior like this come from (what appears to me) entirely deterministic code? What have I done wrong?

Your help is much appreciated. Remember, I am a total noob.

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

using namespace std;

int main () {
  string line;
  string consonantBank [351][2];
  string vowelBank [141][2];
  const char baseVowels [] = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
  int y=0, q=0;
  ifstream myfile ("upsidtestcomma3.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
		getline (myfile,line);
		if (line.find_first_of(baseVowels) != line.npos)
			{
				vowelBank [q][0] = line;
				line.clear();
				getline (myfile,line);
				vowelBank [q][1] = line;
				line.clear();
				++q;
			}
		else {
				consonantBank [y][0] = line;
				line.clear();
				getline (myfile,line);
				consonantBank [y][1] = line;
				line.clear();
				++y;
			}
    }
    myfile.close();
  }

  else cout << "Unable to open file."; 

  for(int i=0; i<351; i++)
	{
	cout << consonantBank[i][0] << "," << consonantBank[i][1] << "\n";
	}

  cin.get();

  for(int i=0; i<141; i++)
  {
	  cout << vowelBank[i][0] << "," << vowelBank[i][1] << "\n";
  }

  cin.get();

  cout << vowelBank[20][0] << "," << vowelBank[80][0] << "," << vowelBank[110][0] << "\n";
  cout << consonantBank[20][0] << "," << consonantBank[150][0] << "," << consonantBank[300][0] << "\n";

  cin.get();

  return 0;
}
find_first_of expects a null terminated string. baseVowels is not null terminated. To fix this you could add '\0' as the last element in baseVowels or use double quotes like you normally do when defining strings.
const char baseVowels [] = "aeiouAEIOU";
Wow, I knew it would be something stupid and simple. Works now. Thank you, kind sir.
Topic archived. No new replies allowed.