Reading files and counting the input

The goal of this program is to read "input.doc" and then print the number of words, capital letters, lowercase letters, digits, and non-alphanumerics.
My .doc file is a 97-2003 compatible type word document with the sentence

"Testing 123. Two roads diverged in a yellow wood."

I'm getting a debug assertion failed and it says at line 56 but I haven't been able to find the problem
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
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;
int main()
{
	ifstream fin;

	fin.open("input.doc");
	if (fin.fail())
	{
		cout<<"Input file opening failed.\n"<<endl;
		exit(1);
	}
	
	int countWord = 1;
	int countNum = 0;
	int countCap = 0;
	int countLow = 0;
	int countOther = 0;
	char next;

	fin.get(next);
	while(!fin.eof())
	{

		if (isupper(next))
		{
			countCap++;
		}
		
		else if(islower(next))
		{
			countLow++;
		}

		else if(isdigit(next))
		{
			countNum++;
		}

		else if(isspace(next))
		{
			countWord++;
		}
	
		else if(!isalpha(next))
		{
			countOther++;
		}

		fin.get(next);
	}

	cout<<"Number of words: "<<countWord<<endl;
	cout<<"Number of uppercase letters: "<<countCap<<endl;
	cout<<"Number of lowercase letters: "<<countLow<<endl;
	cout<<"Number of digits: "<<countNum<<endl;
	cout<<"Number of other characters: "<<countOther<<endl;
	fin.close();

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

// using namespace std;

int main()
{
	std::ifstream fin( "input.doc" ) ; // constructor opens the file

	// fin.open("input.doc");
	if (fin.fail())
	{
		std::cout << "Input file opening failed.\n\n" ; // << endl;
		// exit(1);
		return 1 ; // prefer return from main over std::exit()
	}

	int countWord = 1;
	int countNum = 0;
	int countCap = 0;
	int countLow = 0;
	int countOther = 0;
	char next;

	// fin.get(next);
	// while(!fin.eof())
	while( fin.get(next) ) // check for failure, (not eof)
	{

		if (std::isupper(next))
		{
			countCap++;
		}

		else if(std::islower(next))
		{
			countLow++;
		}

		else if(std::isdigit(next))
		{
			countNum++;
		}

		else if(std::isspace(next))
		{
			countWord++;
		}

		else if(!std::isalpha(next))
		{
			countOther++;
			// TODO: take care of two consecutive white spaces
		}

		// fin.get(next);
	}

	std::cout << "Number of words: " << countWord << /* endl; */ '\n'
	          << "Number of uppercase letters: " << countCap << /* endl; */ '\n'
	          << "Number of lowercase letters: " << countLow << /* endl; */ '\n'
	          << "Number of digits: " << countNum << /* endl; */ '\n'
	          << "Number of other characters: " << countOther << /* endl; */ '\n' ;

	// fin.close(); // destructor closes the file

	// return 0; // implicit
}
Topic archived. No new replies allowed.