File Read and count number of times specified characters appear

Hi I need help, I have a program project due in 4 days and managed to compose a source file for counting indivdual characters from a file but whenever I run my program the indivual character counts totals 0.

Here are the specified instructions for this particular problem:

2. Read text from a file called neural.txt character at a time until you encounter end-of-file. If you wish, you may read the file line by line instead of character by character.
Then print out the number of occurrences of each of the vowels a, e, i, o and u in the text, the total number of letters, and each of the vowels as an integer percentage of the letter total. Also output the total number of punctuation characters in this file.

Suggested (this is not the correct answer) output format is (the numbers in the output below are incorrect and are just being used as an example):

Numbers of characters:

a 3 ; e 2 ; i 0 ; o 1 ; u 0 ; rest 17 ; punct 15

Percentages of total:

a 13% ; e 8% ; i 0% ; o 4% ; u 0% ; rest 73% punct 12%

Help: here is how you approach this problem:

Open the text file for reading.
Read one character at a time until the end-of-file is reached using a while loop
In this loop, check to see if character read is an alphabet (using the isalpha() function from the cctype library)
If it is a character, increase the counter for characters by one and then check to see if it is one of the vowels (use a switch/case for the 5 vowels). Increase the counter for the respective vowel in each of the case statements.
End your while loop for reading the file
Now calculate the percentages using the final counter values and print them out in the specified fashion.
To download neural.txt, right-click on the file name and save it in the same directory as the executable.


And also here is the source code for this problem I have right now:

#include <iostream>
#include <cctype>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
ifstream inputFile;
char input;

double vowel = 0;
double rest = 0;
double punct = 0;
double total = vowel + rest + punct;

double acount = 0;
double ecount = 0;
double icount = 0;
double ocount = 0;
double ucount = 0;

double prest = rest / total;
double ppunct = punct / total;

double pa = acount / total;
double pe = ecount / total;
double pi = icount / total;
double po = ocount / total;
double pu = ucount / total;

cout << showpoint << fixed;

inputFile.open("neural.txt");

while ((input = inputFile.get()) != EOF)
{


if(toupper(input) == 'A')
{
vowel++;
acount++;
}

if(toupper(input) == 'E')
{
vowel++;
ecount++;
}

if(toupper(input) == 'I')
{
vowel++;
icount++;
}

if(toupper(input) == 'O')
{
vowel++;
ocount++;
}

if(toupper(input) == 'U')
{
vowel++;
ucount++;
}


if (ispunct(input))
{
punct++;
}
}


cout << "Number of characters:\n" << endl;
cout << "a " << acount << ";e " << ecount << ";i ";
cout << icount << ";o " << ocount << ";u " << ucount;
cout << ";rest " << rest << ";punct " << punct << endl;

cout << "Percentage of total:\n" << endl;
cout << "a " << pa << "%;e " << pe << "%;i " << pi;
cout << "%;o " << po << "%;u " << pu << "%;rest ";
cout << prest << "%;punct " << ppunct << endl;

system("pause");
return 0;
}


Any help would be appreciated, I honestly can't tell if I have a logical error in my program since it compiles perfectly or if the problem lies in "neural.txt" itself. . Thanks!
The variables in the calculations don't have valid values when you make the variable declaration for total, prest, etc.. The program is executed top to bottom - it isn't going to go back to these assignments later on to figure out the calculation for the output statements.

Wait until you have finished processing all the characters in the file and have these totals, then do the calculations.

1
2
3
4
5
6
7
8
double total = vowel + rest + punct;
double prest = rest / total;
double ppunct = punct / total;
double pa = acount / total;
double pe = ecount / total;
double pi = icount / total;
double po = ocount / total;
double pu = ucount / total;
Last edited on
It's still not working properly, I think there's something wrong with either the logic in my loop or read file statements or the problem lies in the .txt file itself. all of the individual counts total 0 each time I run the program.
Is the file opening successfully?

http://www.cplusplus.com/reference/fstream/fstream/is_open/


Edit:

Someone linked this page on this forum on working with file streams - maybe it will be helpful? Goes into why you don't want to use EOF to control the while loop.

http://www.umich.edu/~eecs381/handouts/filestreams.pdf
Last edited on
ok the problem seems to lie in the file itself thanks for helping me identify this. So what can i do to fix the problem? I'm confused at the moment on what seems to be wrong with the file.
Edit: Ok I can open the file now but for some reason my count still equals 0 and I can't find any explanation for this. Please help, this is all too confusing for me.

Here is my modified source code:

#include <iostream>
#include <cctype>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
ifstream inputFile;
char input;

double vowel = 0;
double rest = 0;
double punct = 0;

double acount = 0;
double ecount = 0;
double icount = 0;
double ocount = 0;
double ucount = 0;

cout << showpoint << fixed << setprecision(2);

inputFile.open("C:\\Users\\Z\\Desktop\\a.txt");

if (inputFile)
{
cout << "Operation successfully performed\n";
inputFile.close();
}
else
{
cout << "Error opening file";
}

while (inputFile >> input)
{


if(toupper(input) == 'A')
{
vowel++;
acount++;
}

if(toupper(input) == 'E')
{
vowel++;
ecount++;
}

if(toupper(input) == 'I')
{
vowel++;
icount++;
}

if(toupper(input) == 'O')
{
vowel++;
ocount++;
}

if(toupper(input) == 'U')
{
vowel++;
ucount++;
}


if (ispunct(input))
{
punct++;
}
}

double total = vowel + rest + punct;

double prest = rest / total;
double ppunct = punct / total;

double pa = acount / total;
double pe = ecount / total;
double pi = icount / total;
double po = ocount / total;
double pu = ucount / total;


cout << "Number of characters:\n" << endl;
cout << "a " << acount << ";e " << ecount << ";i ";
cout << icount << ";o " << ocount << ";u " << ucount;
cout << ";rest " << rest << ";punct " << punct << endl;

cout << "Percentage of total:\n" << endl;
cout << "a " << pa << "%;e " << pe << "%;i " << pi;
cout << "%;o " << po << "%;u " << pu << "%;rest ";
cout << prest << "%;punct " << ppunct << endl;

system("pause");
return 0;
}
Last edited on
Did you use the is_open() function to check that the file was open?

It looks like you're closing out the file before you start to read in characters.

1
2
3
4
5
if (inputFile)
{
cout << "Operation successfully performed\n";
inputFile.close();
}



Also - it looks like you're just counting vowels and punctuation right now. Not the rest of the letters. Wasn't sure if you just hadn't gotten to that part yet.
Okay so I've modified my program and it doesn't experience compile error but the number of totals are zero while the other counts are displayed correctly. For anyone who is experience similar problems with a similar program, the problem was in the loop, for each case or else if statement you have to use toupper(input) = "A' or use the or statement to check if the character is either the lower or upper version of the vowel. I will probably be able to identify why my total count is zero but anyways thanks for the help, all that is left is to identify why total is zero at the end.






#include <iostream>
#include <cctype>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
ifstream inputFile;
char input;

int rest = 0;
int punct = 0;
int total = 0;

int acount = 0;
int ecount = 0;
int icount = 0;
int ocount = 0;
int ucount = 0;

inputFile.open("C:\\Users\\Z\\Desktop\\a.txt");

if (inputFile)
{
cout << "Operation successfully performed\n";

}
else
{
cout << "Error opening file";
}

while (inputFile >> input)
{
toupper(input);

if (ispunct(input))
{
punct++;
total++;
}

else if (isalpha(input))
{
if (toupper(input) == 'A')
{
acount++;
total++;
}

else if (toupper(input) == 'E')
{
ecount++;
total++;
}

else if (toupper(input) == 'I')
{
icount++;
total++;
}

else if (toupper(input) == 'O')
{
ocount++;
total++;
}

else if (toupper(input) == 'U')
{
ucount++;
total++;
}

else
{
rest++;
total++;
}
total++;
}
}

cout << showpoint << fixed << setprecision(2);

double prest = rest / total;
double ppunct = punct / total;

double pa = acount / total;
double pe = ecount / total;
double pi = icount / total;
double po = ocount / total;
double pu = ucount / total;


cout << "Number of characters:\n" << endl;
cout << "a " << acount << ";e " << ecount << ";i ";
cout << icount << ";o " << ocount << ";u " << ucount;
cout << ";rest " << rest << ";punct " << punct << endl;

cout << "Percentage of total:\n" << endl;
cout << "a " << pa << "%;e " << pe << "%;i " << pi;
cout << "%;o " << po << "%;u " << pu << "%;rest ";
cout << prest << "%;punct " << ppunct << endl;

system("pause");
return 0;
}
Last edited on
I've fixed my problem by simply changing the type of total to a double from an int and adding total counter to the end of the loop. Anyways thanks again for the help here is my solved program, it works as intended and I'm glad to have signed up to this forum, you guys are awesome.

Also the reason why a only appeared 16 times was because that was the number of times uppercase appears in the txt file, apparently using the toupper(input) at the beginning of the loop and expecting the relationship expressions to check the vowels will not work because the cctype conversion toupper function is only temporary and must be used in all intended logical and relationship expressions.



Final source code:


#include <iostream>
#include <cctype>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
ifstream inputFile;
char input;

int rest = 0;
int punct = 0;
double total = 0;

int acount = 0;
int ecount = 0;
int icount = 0;
int ocount = 0;
int ucount = 0;

inputFile.open("C:\\Users\\Z\\Desktop\\a.txt");

if (inputFile)
{
cout << "Operation successfully performed\n";

}
else
{
cout << "Error opening file";
}

while (inputFile >> input)
{
toupper(input);

if (ispunct(input))
{
punct++;
}

else if (isalpha(input))
{
if (toupper(input) == 'A')
{
acount++;
}

else if (toupper(input) == 'E')
{
ecount++;
}

else if (toupper(input) == 'I')
{
icount++;
}

else if (toupper(input) == 'O')
{
ocount++;
}

else if (toupper(input) == 'U')
{
ucount++;
}

else
{
rest++;
}
}
total++;
}

cout << showpoint << fixed << setprecision(2);

double prest = rest / total;
double ppunct = punct / total;

double pa = acount / total;
double pe = ecount / total;
double pi = icount / total;
double po = ocount / total;
double pu = ucount / total;


cout << "Number of characters:\n" << endl;
cout << "a " << acount << ";e " << ecount << ";i ";
cout << icount << ";o " << ocount << ";u " << ucount;
cout << ";rest " << rest << ";punct " << punct << endl;

cout << "Percentage of total:\n" << endl;
cout << "a " << pa << "%;e " << pe << "%;i " << pi;
cout << "%;o " << po << "%;u " << pu << "%;rest ";
cout << prest << "%;punct " << ppunct << endl;

system("pause");
return 0;
}
Last edited on
Topic archived. No new replies allowed.