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;
#include <iostream>
#include <fstream>
usingnamespace 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' )
returntrue;
elsereturnfalse;
}
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.