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
|
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
/**********************************************/
//Name: checkFile
//Description: Checks to see if file is valid
//Parameters: countWords, name
//Return: none
/**********************************************/
void checkFile(string &name, ifstream &countWords);
/**********************************************/
//Name: addWords
//Description: Opens file and counts the words
//Parameters: words, size, twords, countWords
//Return: none
/**********************************************/
void addWords(int words[], int &size, int &twords, ifstream &countWords);
/**********************************************/
//Name: getTotal
//Description: Tallys total
//Parameters: words, size
//Return: sum
/**********************************************/
int getTotal(int words[], int size);
/**********************************************/
//Name: displayOutput
//Description: displays output
//Parameters: sum, words, size, name
// countWords
//Return: none
/**********************************************/
void displayOutput(int words[], int &twords, int &sum, string &name, ifstream &countWords);
int main()
{
int words[10];
int size, sum, twords;
string name;
ifstream countWords;
checkFile(name, countWords);
while(!countWords.eof())
{
addWords(words, size, twords, countWords);
sum = getTotal(words, size);
}
displayOutput(words, twords, sum, name, countWords);
return 0;
}
void checkFile(string &name, ifstream &countWords)
{
cout << "Enter File Name: ";
cin >> name;
countWords.open(name.c_str());
if(countWords.fail())
{
cout << "File Failed to Open"<<endl;
exit(0);
}
}
void addWords(int words[], int &size, int &twords, ifstream &countWords)
{
string letters;
twords = 0;
while(countWords >> letters)
if(letters.size() >= 10)
{
words[9]++;
twords++;
}
else
{
words[letters.size()-1]++;
twords++;
}
}
int getTotal(int words[], int size)
{
int sum = 0;
for(int r = 0; r < 10; r++)
{
sum += words[r];
}
return sum;
}
void displayOutput(int words[], int &twords, int &sum, string &name, ifstream &countWords)
{
int size = 10;
cout << "File: "<< name << "\t \t" << "Words: " << twords <<endl;
cout << "\t \t" << "Analysis of Words" <<endl;
cout << "Size " << "\t " << "1 " << "2 " << "3 " << "4 " << "5 " << "6 " << "7 " << "8 " << "9 " << "1\
0+ " <<endl;
cout << "#Words";
for (int j = 0; j < size; j++)
{
cout << words[j] << "\t ";
}
cout <<endl;
cout <<"Sum : " << sum <<endl;
}
thats the whole code. once again I think its something to do with the .szie() but I don't know how to fix it
|