Counters

I am trying to make a program that counts the number of characters in a txt file. I have no problem getting the file open but when it gets to the first letter (R), it freezes and I don't know what I am doing wrong.

In total, I need to make a function for each of these criterias

1. Total Characters
2. if (isVowel(ch))
3. if (isConsonant(ch))
4. if (isalpha(ch))
5. if (isdigit(ch))
6. if left parantheses
7. if right parantheses
8. if single quote
9. if double quote
10. if other

so far I am working on the first one, total character count. I have it in the int main function because when I had it seperate it didn't work and for some reason, it still doesn't. I think if I can get the total character count, parantheses, and other counters working I should be fine for the other ones.


CAN ANYONE HELP!!! THANKS IN ADVANCE!



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

using namespace std;

char ch;
int j=0;
void ChCount();


int main ()

{

ifstream inFile;
ofstream outFile;	   


inFile.open ("hopper.txt");


inFile.get(ch); // Gets first Character of the text file

while (inFile)
{
	cout << ch; // Displays that character

	int j=0;
	
	while(ch !='.')
	  {
	  	 if (isalpha(ch))
		 {
		  j++;
		  
		  inFile.get(ch);
	  	 }
	  }

	inFile.get(ch); // Get next character
}	 	 
		
cout << j;

		
return 0;

}


1
2
3
4
5
6
7
8
9
while(ch !='.')
	  {
	  	 if (isalpha(ch))
		 {
		  j++;
		  
		  inFile.get(ch);
	  	 }
	  }


When ch is not a alphabetic number it does nothing... so it will loop forever..
when there are no alphabethic numbers.. it will only stop when it reaches a '.' .. so i guess it will skip a lot of characters...
Last edited on
You say you are counting characters but isalpha() only denotes actual "letters." Also istream.get() returns a reference to the istream so you can use something like:

1
2
3
4
5
char ch;
while(inFile.get(ch))
{
  // count
}


If the error or eof bit is set during the get() call it will equate to false. I don't know why you would need more than one call to inFile.get() per iteration if you are inspecting characters one-bye-one.

Jikax (192) Jul 17, 2012 at 9:51am
123456789 while(ch !='.')
{
if (isalpha(ch))
{
j++;

inFile.get(ch);
}
}



When ch is not a alphabetic number it does nothing... so it will loop forever..
when there are no alphabethic numbers.. it will only stop when it reaches a '.' .. so i guess it will skip a lot of characters...
Last edited on Jul 17, 2012 at 9:56am



So does this mean that my loop is ok for counting letters and all I have to do is convert the characters to their respective numbers? Is that why it doesn't go past the first letter?


Texan40 (364) Jul 17, 2012 at 10:23am
You say you are counting characters but isalpha() only denotes actual "letters." Also istream.get() returns a reference to the istream so you can use something like:


12345 char ch;
while(inFile.get(ch))
{
// count
}



If the error or eof bit is set during the get() call it will equate to false. I don't know why you would need more than one call to inFile.get() per iteration if you are inspecting characters one-bye-one.



Ok, does it make sense for it to look like this?

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
double chCount()
{
double Char;

while(ch !='.')
	  {
	  	 if (isalpha(ch))
		 {
		  j++;
	  	 }
                 if (isdigit(ch))
		 {
		  i++;
	  	 }

Char = i+j;
cout << char;
	  }
return 0;
}

char ch;
int j=0;
int i=0;

int main ()

{

ifstream inFile;
ofstream outFile;	   


inFile.open ("hopper.txt");


while(inFile.get(ch)) // Gets first Character of the text file

{
  	chCount;
} 



THANKS TO EVERYONE WHO IS HELPING ME OUT!!!
Last edited on
Hello,

I'm pretty new to C++ programming, but I liked you're post so I made it an excersice for myself.

Maybe it can help you to cast each character to an int, and using the (ASCII/int) value to determine the function to invoke using a switch statement.

In that way you can also get rid of a huge list of if-statements.


jwijnker (1) Jul 17, 2012 at 3:53pm
Hello,

I'm pretty new to C++ programming, but I liked you're post so I made it an excersice for myself.

Maybe it can help you to cast each character to an int, and using the (ASCII/int) value to determine the function to invoke using a switch statement.

In that way you can also get rid of a huge list of if-statements.
Report




Yeah I agree but my Prof. doesn't want switch statements, only If-statements. As for casting the Characters to int values, I am not sure how to do this or how to do this with the If statements. Also I cannot get the parantheses counter or "other" counter to work properly nor do I know how to cast those into int values.
This is something I was messing with.
I hope it can help you in some way.

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

using namespace std;

int main()
{
    /**
     * Loop through 'most-frequent-used' characters
     */
    char c;
    long ascii;

    for(long i = 32; i < 127; i++)
    {
        c = i;//someString[i];
        ascii = c;
        cout << "Char '" << c <<"' in int: " << ascii << endl;
            
        ///
        /// Here you should determine the function to invoke of each character
        /// 
        /// if (ascii < 48) // Some special char
        /// {
        ///     // Invoke some function..
        /// }
        
    }
    
    return 0;
}
Just to throw this out there, 1 character = 1 byte. Therfore just use ftell() to get the size of the file and the number of bytes should be close to the number of characters. Then you shouldn't have to worry about the diffrent cases such as numbers, lowercase, uppercase etc..
Last edited on
Topic archived. No new replies allowed.