this function of larger program. function figures out how many characters in alphanumeric word. weird thing is, counts apostrophes when running in xcode, not after compiling , running in terminal. suggestions?
#include <iostream> #include <iomanip> #include <ctype.h> using namespace std; int wordlength(); void displaytable(int wf[]); int main() { int wordfrequency[16]={0}; int templengthstorage=0; templengthstorage=wordlength(); while (templengthstorage!=0) { if (templengthstorage==1)//skipping subscript 0 - not using ++wordfrequency[1]; else if (templengthstorage==2) ++wordfrequency[2]; else if (templengthstorage==3) ++wordfrequency[3]; else if (templengthstorage==4) ++wordfrequency[4]; else if (templengthstorage==5) ++wordfrequency[5]; else if (templengthstorage==6) ++wordfrequency[6]; else if (templengthstorage==7) ++wordfrequency[7]; else if (templengthstorage==8) ++wordfrequency[8]; else if (templengthstorage==9) ++wordfrequency[9]; else if (templengthstorage==10) ++wordfrequency[10]; else if (templengthstorage==11) ++wordfrequency[11]; else if (templengthstorage==12) ++wordfrequency[12]; else if (templengthstorage==13) ++wordfrequency[13]; else if (templengthstorage==14) ++wordfrequency[14]; else if (templengthstorage>=15) ++wordfrequency[15]; templengthstorage=wordlength(); } displaytable(wordfrequency); } int wordlength() { int lettercount=0; int endofword=0; char input; char nextchar; cin.get(input); while (endofword!=1 && !cin.eof()) { if (input==' ' || input=='\n' || input=='.' || input=='!' || input=='?' || input==',') {//do nothing if whitespace or punctuation, skips on them } else if (input=='-') { nextchar=cin.peek(); if ((isalpha(nextchar)) || (isnumber(nextchar)))//this includes hyphen in words have hyphens (refer funtion notes) ++lettercount; if (nextchar=='\n')//for cases hyphen doesn't add word length (refer function notes) { cin.get(input);//dont count , next character } } else if (input=='\'') ++lettercount; else if ((isalpha(input)) || (isnumber(input))) { ++lettercount; nextchar=cin.peek(); if (nextchar==' ' || nextchar=='\n' || nextchar=='.' || nextchar=='!' || nextchar=='?' || nextchar==',' || nextchar==')')//included ) condition end words cases such (219) { endofword=1; } } cin.get(input); } return lettercount; } void displaytable(int wf[]) { int totalwords=0; float totalletters=0;//made float float based masth later on when finding average float average=0; cout << endl; cout << "word length frequency" << endl; cout << "----------- ---------" << endl; (int i=1; i<=15; ++i) { /*this portion of loop displays table*/ cout << " "; cout << setw(2) << i; cout << " "; cout << wf[i] << endl; /*this portion of loop calculations needed find average word length*/ totalwords += wf[i]; totalletters += (i * wf[i]); } cout << "\ntotal words = " << totalwords << endl << endl; cout << "total letters = " << totalletters << endl << endl; average = totalletters/totalwords; cout << "average word length: " << average << endl; }
i used "ain't" in both situations, testing purposes.
Comments
Post a Comment