Count characters, vowels, consonants, etc.

closed account (2bMo216C)
Please help! I am extremely lost as I had to miss several classes for an emergency and am turning to the cplusplus world for help. In this assignment, you must enter a file and get out of it this:


Summary

Total characters: 8282
Vowels: 2418
Consonants: 3970
Letters: 6388
Digits: 174
Left parentheses: 17
Right parentheses: 17
Single quotes: 17
Double quotes: 14
Other: 1655

Here is my code. Any help at all would be appreciated. I am extremely new at this and I need some guidance to help me get through this program. Please and thank you.

#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;

char ch;
bool isVowel (char);
bool isConsonant (char);



int main ()
{

int vowel = 0;

ifstream inFile;
inFile.open( "hopper.txt" );

if ( inFile.fail() )
{
cout << "The file for hopper.txt failed to open.";
exit(1);
}

inFile.get(ch);
while (inFile)
{
cout << ch;

inFile.get(ch);
}


//***************************************************************
bool isVowel (char ch);
{
ch = toupper(ch);
if (ch =='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
return true;
else
return false;
}

//****************************************************************
bool isConsonant (char ch);
{
ch = toupper(ch);
if (ch == 'B'||ch=='C'||ch=='D'||ch=='F'||ch=='G'||ch=='H'||ch=='J'||ch=='K'||ch=='L'||
ch=='M'||ch=='N'||ch=='P'||ch=='Q'||ch=='R'||ch=='S'||ch=='T'||ch=='V'||ch=='W'||ch=='X'||
ch=='Y'||ch=='Z')
return true;
else
return false;
}
//******************************************************************


return 0;
}


You can format your code with code blocks to make it more readable: http://www.cplusplus.com/articles/jEywvCM9/

By single quotes do you mean an apostrophe? -> '

I've put your code in code tags and added a bunch of comments.

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

using namespace std;

char ch; // This should be inside main() with your other variables.
bool isVowel ( char ch );

// You don't need "isConsonant();" so I removed it. I explain below.

int main ( )
{
    int vowel = 0; // This is a good start, but you'll need more variables for each
    // of the things you want to keep track of.

    ifstream inFile; // These two lines can be combined like this:
    inFile.open ( "hopper.txt" ); // ifstream inFile ( "hopper.txt" );


    if ( inFile.fail ( ) )
    {
        cout << "The file hopper.txt failed to open.";

        exit ( 1 ); // You should use "return 0;" here.
        // exit is only neede if you are quiting from a function other than
        // main(), and return values other than 0 are only used for errors.
        // A file failing to open isn't necessarily an error.
    }


    inFile.get ( ch ); // This line is not needed, as you can do this instead:

    while ( inFile ) // "while ( inFile.get ( ch ) )"
    {
        cout << ch; // Do you need to do this?

        inFile.get ( ch ); // This line is not needed, as I handled it above.

        // Firstly, you can increment your total characters counter.
        // Now you need to check what type of char "ch" is.
        // First, I'd use the function "isalpha()".
        // If true, use "isVowel()" and increment your letter counter.
            // If "isVowel()" is true, increment your vowel counter.
            // If false, you know it must be a consonant, so increment that counter.
        // Now I would use "isdigit()", and then check for the other characters.
        // Remember to increment the appropriate counters.
        // If none of them match, increment the other counter.
    }


    return 0;
}

bool isVowel ( char ch )
{
    ch = toupper ( ch );

    if ( ch =='A' || ch == 'E' || ch == 'I' || ch== 'O' || ch=='U' )
        return true;

    else return false;
}
Last edited on
This isVowel function has less code and does the same thing:

1
2
3
4
5
bool isVowel (char ch)
{
	ch = toupper(ch);
	return (ch =='A'|| ch=='E' || ch=='I' || ch=='O' || ch=='U');
}


When you get problems where you are counting number of times some characters appear in a file, it is usually easier to read the file one character at a time. You seem to have done that, so all that is left is to now determine what character is read and then increment a counter for that.


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
void counter(std::istream &iss) { 
        // istream can refer to any object that is a child of the istream class
        // so if you have any ifstream, istringstream, etc object, you can pass that
        // as a parameter to this function
	int vowel(0), consonants(0), digits(0), lparen(0), rparen(0), 
	squote(0), dquote(0), other(0), ch;
	
	while (iss) { 
                // while there are more characters to read
                -> http://www.cplusplus.com/reference/ios/ios/operator_bool/

		switch(ch = iss.get()) {
			case '(':
				// increment respective counter
				break;
			case ')':
				// increment respective counter
				break;
			case '\'':
				// increment respective counter
				break;
			case '\"':
				// increment respective counter
				break;
		}
		
		if (isalnum(ch)) { // if true, then this character is either a digit or a letter
			isdigit(ch) ? /* increment respective counter */:
                        isVowel(static_cast<char>(ch)) ? /* increment respective counter */ :
                       /* increment respective counter */;
		}
                // Tenary operator (?:) -> http://www.cplusplus.com/articles/1AUq5Di1/
		else other++;
	}
	
	std::cout << "\tSummary:\n"
	<< "Total characters: " << vowel + consonants + digits + 
                                   lparen + rparen + sqoute + 
                                   dquote + other << '\n'
	<< "Vowels: " << vowel << '\n'
	<< "Consonants: " << consonants << '\n'
	<< "Letters: " << vowel + consonants << '\n'
	<< "Digits: " << digits << '\n'
	<< "Left parentheses: " << lparen << '\n'
	<< "Right parentheses: " << rparen << '\n'
	<< "Single quotes: " << squote << '\n'
	<< "Double quotes: " << dquote << '\n'
	<< "Other: " << other << endl << endl;
}


Usage:
http://coliru.stacked-crooked.com/a/2477a713e3a10d3e
Last edited on
Topic archived. No new replies allowed.