Counting Characters

Mar 13, 2012 at 2:01pm
Hello again C++ WORLD! I am once again having trouble with my program. I am trying to creat a program that will count the total number of characters, the number of letters, total upper and total lowercase, total punctiations and total numbers in the input. Also re-write the code in all caps. I have written it thus far (without the code to make it turn into all caps) where there are no errors (build errors) but It does not do anything when ran. I need any kind of help with what is wrong with how I wrote the program and what I can do to make it run.

P.S. Teacher says I need the functions cin.get(), int isAlpha(), int isDigit(), int isUpper(), and int isLower(). Thanks in advance for all the help!

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

#include <iostream>
#include <iomanip>

using namespace std;

char c = 0;
char a;

int CharCount = 0,      // Total characters count
	AlphaCount = 0,     // Total letter count
	CapCount = 0,       // Total uppercase letter count
	LowerCaseCount = 0, // Total lowercase letter count
	PunctCount = 0,     // Total punctiation count
	DigitCount = 0;     // Total number count
	

int isChar(char c)
{

	CharCount = AlphaCount + DigitCount + PunctCount;

}

int isAlpha(char c)
{
	if (c >= 'a' && c <='z' || c >= 'A' && c <= 'Z')
	return 1;

	else 
	return 0;	  
}

int isDigit(char c)
{

	if (c >= '0' && c <= '9')
	return 1;
	
	else
	return 0;
}

int isUpper(char c)
{

	if (c >= 'A' && c <= 'Z')
	return 1;
	
	else
	return 0;
}

int isLower(char c)
{
 	if (c >= 'a' && c <='z')
	return 1;

	else 
	return 0;	  	   
}

int main()
{
cout << "Type a series of lines to get character counts for each line" << endl;
cout << "Type a line consisting of a single period '.' to stop" << endl;

cout << "\n";

cin.get(c);
	while (c != '.')
		{
		        CharCount += isChar(c);
			AlphaCount += isAlpha(c);
			CapCount += isUpper(c);
			LowerCaseCount += isLower(c);
			DigitCount += isDigit(c); 
			
			if (c >= '!' && c <= ')')
				{ 
					PunctCount++;
				}	  	  	  
		}
		

cout << "Char count:         " << CharCount << endl;
cout << "Alpha count:        " << AlphaCount << endl;
cout << "Caps count:         " << CapCount << endl;
cout << "Lower case count:   " << LowerCaseCount << endl;
cout << "Punctuation count:  " << PunctCount << endl;
cout << "Digit count:        " << DigitCount << endl;

return 0;

}



Example Output:



Joe Hill
JOE HILL

Char count:    8
Alpha count:   7
Cap count:     2
Lower case:    5
Punct:         0
Digit count:   0

Mar 13, 2012 at 2:23pm
you can use the functions just definied in the header <cctype> for yout purpouse:
http://cplusplus.com/reference/clibrary/cctype/
Mar 13, 2012 at 2:29pm
You need a secon cin.get() before the end of the loop (before line 68)
Mar 13, 2012 at 3:11pm
Wow!! Thanks guys!!! The only problem I am having now is that am I getting a crazy number for my CharCount???? Im sure that's an error in computation on my part so I can figure that out. Thanks again !!!!
Last edited on Mar 13, 2012 at 3:11pm
Mar 13, 2012 at 3:22pm
What do you mean crazy? is the number super high, super low, or negative?
Mar 13, 2012 at 3:29pm

NerdTastic (32) Mar 13, 2012 at 10:22am
What do you mean crazy? is the number super high, super low, or negative?



Text:

Apples!!

Char count: 16
Alpha count: 6
Caps count: 1
Lower case count: 5
Punctuation count: 2
Digit count: 0

Everything else is ok, just the char count.
Doesnt make sense why its doing this.
Last edited on Mar 13, 2012 at 3:33pm
Mar 13, 2012 at 5:09pm
closed account (zb0S216C)
Try counting the amount of characters with this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int Char_Count( const char *String )
{
    if( !String )
        return( 0 );

    int Count( 0 );
    while( *String )
    {
        if( *String == ' ' )
        {
            ++String;
            continue;
        }

        ++Count, ++String;
    }

    return( Count );
}

I tested it and it works. Note that this function doesn't count white-space as a character.

Wazzak
Last edited on Mar 13, 2012 at 5:11pm
Mar 13, 2012 at 6:04pm

Framework (1924) Mar 13, 2012 at 12:09pm
Try counting the amount of characters with this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 int Char_Count( const char *String )
{
    if( !String )
        return( 0 );

    int Count( 0 );
    while( *String )
    {
        if( *String == ' ' )
        {
            ++String;
            continue;
        }

        ++Count, ++String;
    }

    return( Count );
} 
I tested it and it works. Note that this function doesn't count white-space as a character.

Wazzak


Thank you very much for your help but my teacher says I need to use the int isChar(char c) technique. To learn it or something.
Last edited on Mar 13, 2012 at 6:04pm
Mar 13, 2012 at 9:26pm
1
2
3
4
int isChar(char c)
{
	CharCount = AlphaCount + DigitCount + PunctCount;
}


IsChar does nothing with c. Why is that?


1
2
3
4
5
6
7
8
9
10
11
12
13
	while (c != '.')
		{
		        CharCount += isChar(c);
			AlphaCount += isAlpha(c);
			CapCount += isUpper(c);
			LowerCaseCount += isLower(c);
			DigitCount += isDigit(c); 
			
			if (c >= '!' && c <= ')')
				{ 
					PunctCount++;
				}	  	  	  
		}


On the last iteration of the loop, what happens? IsChar() depends on the values updated by the function calls following it. Is that order right? Do you really want to be adding CharCount to the value it returns?

Since IsChar doesn't do what one would think it should, you can move it out of the loop entirely and just call it after.

I suspect, however, that your instructor is expecting the definition of isChar to look more like:

1
2
3
4
5
int isChar(char c)
{
        // Yes, I realize you don't have an IsPunct() function.  You get the drift!
	return IsAlpha(c) || IsDigit(c) || IsPunct(c) ;
}


Last edited on Mar 13, 2012 at 9:34pm
Mar 13, 2012 at 9:34pm
This is certainly what you mean:
1
2
3
4
5
6
7
8
int isChar(char c)
{
	if (isAlpha(c) || isDigit(c) || isPunct(v))
	return 1;

	else 
	return 0;	  
}
Mar 13, 2012 at 10:10pm
That's another way of writing it, yes.
Mar 14, 2012 at 7:00am
@cire: Um, last time I saw your post the 'isChar()' wasn't there, I swear. This let me look kinda...

Joshua Spears wrote:
To learn it or something.
Gawd, theacher these days...
Last edited on Mar 14, 2012 at 7:02am
Mar 14, 2012 at 7:50am
Heheh. Yeah. I see now your post time was the same as my edit time.
Mar 14, 2012 at 6:38pm
wow seriously... THANKS!!!! I've been really stuck (prob because I'm a beginner lol) but all the help is GREATLY APPRECIATED!!!! and that's a thanks to EVERYONE!!
Topic archived. No new replies allowed.