// Gizmo.cpp : Defines the entry point for the console application.
//
#include "StdAfx.h"
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
struct statistics{
int words;
double averagewordlength;
int wordlengthcount[13];
};
void outputstats (statistics textstat){
cout << "Total words are " << textstat.words << "\n";
int i = 0;
cout << "Number of words with 'X' letters \n";
while (i <13)
{
double percent = double(textstat.wordlengthcount[i])/double(textstat.words) *100 ;
if (i == 12)
{
cout << "=>";
}
cout << i+1 << " has " << textstat.wordlengthcount[i] << " words and makes up " << percent << "%\n" ;
i++;
}
cout << "Average word length is: " << textstat.averagewordlength/textstat.words << "\n";
}
string createpxtfile(string fname)
{
//default
string rtnstr="Successful creation of PXT file\n";
string fName2 = fname.substr(0,fname.length() -4) + "converted.pxt" ;
ofstream outFile(fName2.c_str());
if (!outFile)
rtnstr = "Failure\n";
return rtnstr;
}
string readLine(istream& fin)
{
char buff[256];
bool ignored = true;
// Clear any remaining end of line characters
while (ignored == true)
{
if(fin.peek()=='\n')
{
ignored = true;
fin.ignore();
}
else
ignored = false;
}
// Read the line into a buffer
fin.getline(buff, 256, '\n');
// Convert the buffer to a string and return it
return string(buff);
}
string getfile(){
cout << "Please type in the filename for the program to read from, e.g C:\\example.txt\n";
string fileloc;
cin >> fileloc;
return string (fileloc);
}
string editpxtfile(string fname, statistics* text)
{
string word;
ifstream ptxFile(fname.c_str());
//warning, some wacked out error occurs in here to screw up wordcount we must make adjustments.
int lastwordlength = 0;
while(!ptxFile.eof())
{
text->words++;
ptxFile>>word;
lastwordlength = word.length();
text->averagewordlength = text->averagewordlength + word.length();
if(word.length()>12)
{
text->wordlengthcount[12]++;
}
else
{
text->wordlengthcount[word.length()-1]++;
}
}
if(lastwordlength>12)
{
text->wordlengthcount[12]--;
}
else
{
text->wordlengthcount[lastwordlength-1]--;
}
text->averagewordlength = text->averagewordlength - lastwordlength;
text->words = text->words -1;
//ran through without error. thus successful
return "successful editing of structure through pointer\n";
}
//struct text
statistics text;
void main()
{
text.words= 0;
text.averagewordlength=0;
int i = 0;
while (i < 13)
{
text.wordlengthcount[i] = 0;
i++;
}
string word;
bool eofReached = false;
string fName = getfile();
ifstream inFile(fName.c_str());
string fName2 = fName.substr(0,fName.length() -4) + "converted.pxt" ;
//now we create the file and check for errors
cout << createpxtfile(fName);
//now we write to the file
ofstream outFile(fName2.c_str());
if(!inFile)
{
// If file is not read: Show error message
cout << "Error, can't open file" << endl;
}
else
{
// If file is read: Continue
while(!inFile.eof())
{
inFile >> word;
for(int i = 0; i < (int)word.length(); i++)
{
word[i] = tolower(word[i]);
if(ispunct (word[i]))
{
word[i] = ' ';
}
}
outFile << word << endl;
eofReached = inFile.eof();
}
inFile.close();
outFile.close();
}
//we now edit the struct text through a function we pass the pointer of an array to it and the pxt filename.
statistics* textpointer = &text;
cout << editpxtfile(fName2, textpointer);
//now function to output.
outputstats(text);
system("pause");
}