Style Program-Error bringing it in
For some reason my string isn't working in my OpenFile function. I have put the whole program in just for reference but the error will be in there.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cctype>
using namespace std;
enum Features {UPPER, LOWER, DIGIT, IGNORE, EOW, EOS};
struct Counters
{
int uppercase;
int lowercase;
int digit;
int word;
int sentence;
int ignore;
};
void OpenFile(ifstream& text);
Features Decode(char character);
void IncrementCounters(Counters& counters, char character);
void PrintTable(Counters counters);
void InitializeCounters(Counters& counters);
int main()
{
ifstream text;
char character;
Counters counters;
OpenFile(text);
if (!text)
{
cout<< "Files did not open successfully."<<endl;
return 0;
}
InitializeCounters(counters);
text.get(character);
do
{
IncrementCounters(counters, character);
text.get(character);
}while(text);
PrintTable(counters);
text.close();
return 0;
}
void OpenFile(ifstream& text)
{
string infilename;
cout<<"Enter the name of the file to be processed";
cout<<endl;
cin>>infilename;
text.open(infilename.c_str());
cout<<"Analysis of characters on input file "<<infilename<<endl<<endl;
}
Features Decode(char character)
{
if (isupper(character))
return UPPER;
if (islower(character))
return LOWER;
if (isdigit(character))
return DIGIT;
else
switch (character)
{
case '.':
case '?':
case ' ':
case ',':
case ';':
case ':':
case '\n':
return EOW;
}
return IGNORE;
}
void IncrementCounters(Counters& counters, char character)
{
static bool endOfWord=false;
switch(Decode(character))
{
case UPPER: counters.uppercase++;
endOfWord=false;
break;
case LOWER: counters.lowercase++;
endOfWord=false;
break;
case DIGIT: counters.digit++;
endOfWord=false;
break;
case EOW: if(!endOfWord)
{
counters.word++;
endOfWord=true;
}
break;
case EOS: counters.sentence++;
endOfWord=true;
break;
case IGNORE:counters.ignore++;
break;
}
}
void PrintTable(Counters counters)
{
int totalAlphaNum;
totalAlphaNum=counters.uppercase+counters.lowercase+counters.digit;
cout<<"Total number of alphanumeric: "<<totalAlphaNum<<endl;
cout<<"Number of uppercase letters: "<<counters.uppercase<<endl;
cout<<"Number of lowercase letters: "<<counters.lowercase<<endl;
cout<<"Number of digit: "<<counters.digit<<endl;
cout<<"Number of characters ignored: "<<counters.ignore<<endl;
counters.word=counters.word+counters.sentence;
cout<<"Number of words: "<<counters.word<<endl;
cout<<"Number of sentences: "<<counters.sentence<<endl;
cout<<"Average word length: "<<fixed<<setprecision(2)<<static_cast<float>(totalAlphaNum)/counters.word<<endl;
cout<<"Average sentence length: "<<fixed<<setprecision(2)<<static_cast<float>(counters.word)/counters.sentence<<endl;
}
void InitializeCounters(Counters& counters)
{
counters.uppercase=0;
counters.lowercase=0;
counters.digit=0;
counters.word=0;
counters.sentence=0;
counters.ignore=0;
}
|
Last edited on
Topic archived. No new replies allowed.