searching a text file

Hi all.

I'm trying to search a text file for certain characters using a switch statement and I cant figure out what's wrong here.

thanks for looking..

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


using namespace std;

int main ()
{
	int array [128];
	char letter;
	int i,k;
	for (i = 0; i < 128; ++i)
		array [i] = 0;
	ifstream fin("test.txt");
	fin >> letter;
	while (!fin.eof ())
	{
		switch (letter)
		{
		case ';':
		case '?':
		case '{':
		case '}': array [int(letter)] ++;
		}
		fin >> letter;
	}
		for (i = 0; i < 128; ++i);
		if (array [i] > 0)
			cout << "symbol " << char(i) << 
                      " occurs " << array [i] << " times \n";
	

	system ("pause");
	return 0;
}
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
	ifstream testFile("test.txt");
	int charArray[128];
	for (int i = 0; i < 128; i++)
		charArray[i] = 0;

	if (testFile.is_open())
	{
		std::cout << "File opened!\n";
		std::cout << "Start reading file...\n";
		while (testFile >> std::skipws)
		{
			char character = ' ';
			testFile >> character;
			
			switch (character)
			{
				case ';':
				case '?':
				case '{':
				case '}':
					std::cout << "Found symbol!\n";
					charArray[int(character)]++;
				break;
				default:;
			}
		}

		testFile.close();
	}

	for (int i = 0; i < 128; ++i)
		if (charArray[i] > 0)
			cout << "symbol " << char(i) <<" occurs " << charArray[i] << " times \n";
Thanks for the help Mr Impact.
I haven't used std::cout. I know thats the preferred way for text, but I'm not quite there yet.

I found my mistakes.
Topic archived. No new replies allowed.