I have to write a program that reads in data from a text file and
tells how many of each punctuation mark is in the file and then tells the average length of each word in the file. so my question is what would be the best to do this.
I have to write a program that reads in data from a text file and
tells how many of each punctuation mark is in the file and then tells the average length of each word in the file. so my question is what would be the best to do this.
open file.
read into string.
count # of words
count # of spaces
count # of chars
subtract: chars - spaces
divide
count # of punctuation marks
close file.
I am having trouble reading it into a string.
that aint all but that will do for now
Code:ifstream infile("filename.txt"); if(!infile) exit(1); string mystring; getline(infile, mystring);
or you could read the whole file into an array...and then step through it picking up punctuations (hint: use ASCII values in a for loop) and at the same time counting all the printable letters (hint: function isalpha() ) as well as number of words. Than divide all the alphabetical characters by the number of words to get the average word length...
post some of your code and we'll help you further, this isn't that hard
axon
some entropy with that sink? entropysink.com
there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka
I suggest to open the file and get chars like this:
You can add more signs in the IF operator. Hope this will help YouCode:char letter; int count=0; while(ptr_file.get(letter)) { if(letter=='.' || letter==',' || letter=='!' || letter=='?') {count++;} } cout << "There are " << count << "punctuations in the file.";
Lull!
http://friedegg.hit.bg
or in case to cont how many of each mark there are, just in the while add IFs. Also create more integers for each mark:
Code:int punkt=0; int comma=0; ... while... { if(letter==',') comma++; etc. then cout the results
Lull!
http://friedegg.hit.bg