Read In Whitespace from a file.

Hey guys, I have been searching the forums to no avail. I'm pretty sure that I am close to having it I just for some reason cannot find what's wrong and why this program won't print anything. I think it has something to do with inputCharacter, but if it is I am not entirely sure as to why. Any input would be appreciated!

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
/* Program: Read in a file and count the number of whitespaces in the file and print them on screen.*/

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <ctype.h>
using namespace std;
int main()
{
	char inputCharacter; 
	int count = 0, space = 0, htabs = 0, nlines = 0, vtabs = 0, ffeeds = 0, creturn = 0; 

	ifstream myInputStream;
	myInputStream.open("Alice.txt");//input file stream

	while (!myInputStream.eof())//checks for end of file
	{
		myInputStream.get(inputCharacter);
		if (inputCharacter == ' ')
			count++;
			space++;
		if (inputCharacter == '\t')
			count++;
			htabs++;
		if (inputCharacter == '\n')
			count++;
			nlines++;
		if (inputCharacter == '\v')
			count++;
			vtabs++;
		if (inputCharacter == '\f')
			count++;
			ffeeds++;
		if (inputCharacter == '\r')
			count++;
			creturn++;	
	}

	myInputStream.close();

	cout << "Spaces" << space << endl;
	cout << "Horizontal Tabs" << htabs << endl;
	cout << "Newlines" << nlines << endl;
	cout << "Vertical Tabs" << vtabs << endl;
	cout << "Form Feeds" << ffeeds << endl;
	cout << "Carriage Returns" << creturn << endl <<"--------------------"<< endl;
	cout << "Total Whitespace" << count << endl;

        return 0;
} // end main function
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
	while (myInputStream.get(inputCharacter))
	{
		if (inputCharacter == ' '){
			space++;
			count++;
		}
		if (inputCharacter == '\t'){
			htabs++;
			count++;
		}
		if (inputCharacter == '\n'){
			nlines++;
			count++;
		}
		if (inputCharacter == '\v'){
			vtabs++;
			count++;
		}
		if (inputCharacter == '\f'){
			ffeeds++;
			count++;
		}
		if (inputCharacter == '\r'){
			creturn++;
			count++;
		}
	}
Making that into a while loop still doesn't print anything on my screen. When the program is run all I get is a blank exe screen.
Last edited on
If I take out this while loop:

while (!myInputStream.eof())

It will print, however, it does not count any whitespaces and just prints zeroes.
Based on the filename I'm probably in the same class. My code is counting fine.

I believe you need braces anytime there is more than one operation being completed, either way it is good habit to use them.

Try this

1
2
3
4
5
6
7
8
9
10
11
12
13

myInputStream.get(inputCharacter);
		if (inputCharacter == ' ')
                        {
			count++;
			space++;
                        }
		else if (inputCharacter == '\t')
			{
                        count++;
			htabs++;
                        }
                else if (........................)


and so on.
Last edited on
I figured it out, I had a null statement in my function declaration. Thanks though. Ah really what class?
Last edited on
Topic archived. No new replies allowed.