Yes, people need to fix their own problems...
ViperVenom
But as it goes, your program needs to reset
v to zero after every count.
Also, the vowel counting for each line needs to be in the loop.
Finally, don't test against EOF. Test against the 'goodness' of the file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
fstream file_op("c:\\proj3cpp.txt",ios::in);
// while there are lines to be read
while (file_op.getline( str, 3000 ).good())
{
// for each character in the string
for (v = 0; str[ v ] != '\0'; v++)
{
// update your counts here
...
}
}
// Only close the file when done using it
file_op.close();
| |
Line 4 needs some expanation:
The
ifstream::
getline() method only reads to the end of each line, not the entire file.
The method also returns the stream... so you can use it in a test condition (like an
if statement or loop of some kind).
So we test it against the
good() attribute. It is shorthand for this:
1 2 3 4 5 6 7
|
file_op.getline( ... );
while (file_op.good())
{
...
file_op.getline( ... );
}
| |
The nice thing about the 'shorthand' is that it combines lines 1 and 6 here into a single line -- meaning less chance for errors (do one thing
once).
Finally, the
good() part can be omitted to get the same result:
1 2
|
while (file_op.getline( ... ))
{
| |
In this case,
good() is presumed, and the result is the same.
As
Kiana indicated, you can use
toupper() to help a bit... but you must
#include <cctype>
to use it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <cctype>
...
switch (toupper( str[ v ] ))
{
case 'A':
a += 1;
break;
case 'E':
...
...
}
| |
Finally, be careful with your indentation. I find that it helps to always write the
{
and
}
braces immediately after writing the
while,
if, function header, etc. This helps things to be properly lined-up and properly terminated.
if (fooey)_ if (fooey) if (fooey) if (fooey) if (fooey)
{_ { {_ {
}_ } cout << "fooey!\n";_
}
Hope this helps.