I'm currently stuck on this project...I can't seem to find the right number of vowels, number of As, and blanks from the file. I also, don't understand why my output won't align right. Also, confusion on how to determine the total number of lines processed. Help would be needed...
//Constant for the class and exercise heading
const string CLASS_EXERCISE = "CST 133 - Exercise 4";
//Constant for programmer's name heading
const string PROGRAMMERS_NAME = "Dorthy Petrick";
//Constant for the divider width
const int WIDTH = 94;
void OutputHeading (ofstream& fout, string);
void OutputDivider (ofstream& fout, int WIDTH,
char symbol);
int main (void)
{
int stringLength;
ifstream fin;
ofstream fout;
string fileName;
string sentence;
int empty;
int lineOfData;
int numberOfAs;
int countOfVowels;
int countOfBlanks;
int lengthOfSentence;
int totalNumberOfLines;
int totalLength;
char ch;
//Open data file
fout.open("Ex4Output.txt");
OutputDivider ( fout, WIDTH, '-');
//Open data file
fin.open("Lines.txt");
//Set the file empty flag
empty = true;
//Determine if the input file is empty or doesn't exist
if (fin)
{
//Set the message
fout <<"Processing continues, the input file" << right << setw(7) <<
"Lines.txt was successfully opened." << endl;
}
else
{
fout << "The input file" << right << setw(2) <<
"is empty or does not exist." << endl;
}
OutputDivider ( fout, WIDTH, '-');
//Insert blank line
fout << endl;
//Output a divider to the file
OutputDivider ( fout, WIDTH, '-');
//Output Heading to the file
void OutputHeading (ofstream& fout, string);
//Output a divider to the file
OutputDivider ( fout, WIDTH, '-');
//Output column headings to the file
fout << left << "Line" << right << setw(48) << "Length" << right <<
setw(15) << "# of Vowels" << right << setw(11) << "# of As" <<
right << setw(15) << "# of Blanks" << endl;
//Output a divider to the file
OutputDivider ( fout, WIDTH, '-');
//Read the sentence from input file
getline (fin,sentence);
//Determine the counters for all three
while (fin)
{
//Determine the length of the sentence
lengthOfSentence = sentence.length();
//Determine which are vowels, blanks, or As
for (lineOfData = 0; lineOfData < lengthOfSentence; lineOfData++)
{
ch = sentence.at(lineOfData);
switch(ch)
{
//Determine which counter
case 'a':
numberOfAs++;
countOfVowels++;
break;
case 'e':
countOfVowels++;
break;
case 'I':
countOfVowels++;
break;
case 'i':
countOfVowels++;
break;
case 'o':
countOfVowels++;
break;
case 'u':
countOfVowels++;
break;
default:
countOfBlanks++;
}
}
//Output the line of data
fout << sentence << setw(28) << lengthOfSentence << setw(13) <<
countOfVowels << setw(12) << numberOfAs << setw(12) <<
countOfBlanks << endl;
//Read line of sentence from input file
getline(fin,sentence);
//Accumulate the total length of sentences
totalLength += lengthOfSentence;
//Accumulate total number of lines
totalNumberOfLines += ch;
}
//Output a divider to the file
OutputDivider ( fout, WIDTH, '-');
//Output the totals
fout << left << "Totals" << right << setw(45) << totalLength << right <<
setw(13) << numberOfAs << endl;
//Output a divider to the file
OutputDivider ( fout, WIDTH, '-');
//Output the number of lines processed
fout << left << "The number of lines is ----------------------->" <<
right << setw(2) << totalNumberOfLines << endl;
//Close output file
fout.close();
//Close the input file
fin.close();
return 0;
}
//-----------------------------------------------------------------------------
//OutputHeading - print the heading to the output file
//-----------------------------------------------------------------------------
void OutputHeading (ofstream& fout, string CLASS_EXERCISE, string PROGRAMMERS_NAME)
{
int stringLength;
//Calculate the centering print for the heading
stringLength = (WIDTH + CLASS_EXERCISE.length())/2;
//Output centered print for the heading
fout << setw(stringLength) << CLASS_EXERCISE << endl;
//Calculate the centering print for the heading
stringLength = (WIDTH + PROGRAMMERS_NAME.length())/2;
//Output centered print for the heading
fout << setw(stringLength) << PROGRAMMERS_NAME << endl;
}
//----------------------------------------------------------------------------
//OutputDivider - Outputs on the screen the symbol
// the number of times specified by numberOfSymbols
//----------------------------------------------------------------------------
void OutputDivider (ofstream& fout, int WIDTH,
char symbol)
{
fout<< setfill(symbol) << setw(WIDTH + 1)
<< ' ' << setfill(' ') << endl;
}
Use the fallthrough behavior of cases in switch statements to take the same action for upper and lowercase chars. Also, take note that the default case does not imply that you've reached a blank character. Your current implementation would take all consonants (well, all non-lowercase vowels except 'I') and count them as a blank.
Another thing, your code is WAAAAY over-commented. Most of the comments do not add any understanding to your program. Use good variable names and use comments to document conceptually what you're trying to accomplish, not what a line of code is doing verbatim.
EDIT: Here's an example of how a good variable name can remove the need for a comment:
1 2
//Constant for the divider width
constint WIDTH = 94;