Hi! Still being an absolute beginner, I can't find a reason for why this program doesn't run properly (note that the program is not complete). It is possible to compile it but it encounters some kind of error when executing. The string saves the file properly I have checked this by using cout << so I guess the error is in the class definition, no? I would be truly grateful if you could help me somehow so I can grasp the concept of files becoming strings going through object methods.
#include <string>
#include <cctype>
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
const int NUMBER_OF_LETTERS = 26; // The alphabet
class Text
{
private:
string textstring;
int histogram_abs[NUMBER_OF_LETTERS];
double histogram_rel[NUMBER_OF_LETTERS];
int letters_total;
public:
Text();
~Text();
void setText(string text);
bool calculateHistogram();
};
string file_name(string &text);
void read_file(string &rad, string &text);
int main()
{
string text, rad;
bool histOK;
Text myText;
file_name(text);
read_file(rad, text);
myText.setText(text);
histOK = myText.calculateHistogram();
system("pause");
return 0;
}
Text::Text()
{
textstring="";
for(int i=0;i<NUMBER_OF_LETTERS;i++)
{
histogram_abs[i]=0;
}
for(int i=0;i<NUMBER_OF_LETTERS;i++)
{
histogram_rel[i]=0.0;
}
letters_total=0;
}
Text::~Text()
{
}
void Text::setText(string text)
{
textstring=text;
}
bool Text::calculateHistogram()
{
for(unsigned int i = 0; i<textstring.length(); i++)
{
if(isalpha(textstring[i]))
letters_total++;
if(isupper(textstring[i]))
textstring[i]=tolower(textstring[i]);
histogram_abs[(textstring[i] - 'a')]++;
}
if(letters_total==0)
return false;
else
return true;
}
string file_name(string &text)
{
cout << "File name:\n";
cin >> text;
int john=text.rfind(".txt");
if (john == string::npos)
text.append(".txt");
return text;
}
void read_file(string &rad, string &text)
{
ifstream fin(text.c_str() );
if(!fin)
{
cout << "No file with that name in current folder. " << text << endl;
}
while(!fin.eof())
{
string radtemp;
getline(fin,radtemp);
rad += radtemp;
}
text=rad;
}