Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.
#include <string>
#include <cctype>
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
const int NUMBER_OF_LETTERS = 26; //A-Z
const int NUMBER_OF_LANGUAGES = 4;
const double INTERPRET[NUMBER_OF_LANGUAGES][NUMBER_OF_LETTERS]=
{{8.27,1.48,2.94,4.03,11.78,2.22,1.72,6.77, //engelska
7.39,0.12,0.81,3.76,2.85,6.71,7.79,1.54,
0.05,5.95,6.69,9.07,2.66,1.13,2.14,0.19,
1.89,0.03},
{7.97,1.40,3.55,3.79,16.89,1.02,1.00,0.75, //franska
7.08,0.38,0.04,5.51,2.82,8.11,5.19,2.78,
1.01,6.69,8.35,7.22,6.09,1.35,0.02,0.54,
0.30,0.15},
{9.50,1.11,1.53,5.30,8.94,1.74,3.57,3.94, //svenska
3.98,0.89,3.26,4.93,3.41,8.46,5.01,1.77,
0.00,6.73,5.56,9.20,1.94,2.42,0.00,0.05,
0.45,0.00},
{5.12,1.95,3.57,5.07,16.87,1.35,3.00,5.79, //tyska
8.63,0.19,1.14,3.68,3.12,10.64,1.74,0.42,
0.01,6.30,6.99,5.19,3.92,0.77,1.79,0.01,
0.69,1.24}};
class Text
{
private:
string text ;
int letters ;
int other ;
int n[NUMBER_OF_LETTERS] ;
public:
Text() ;
void setText ( string nyText ) ;
int calcAbs () ;
void writeAbs () ;
void absToRel ( int numb[], double histRel[] ) ;
void plotRel ( double histRel[] ) ;
string interpret ( double histRel[] ) ;
} ;
// -------------------------------------------------------
// Functions
// -------------------------------------------------------
void file_name (string &in_file) ;
void read (string &in_file, string &out_string) ;
// -------------------------------------------------------
// Main program
// -------------------------------------------------------
int main()
{
string text ;
int numb ;
Text myText ;
string in_file ;
string out_string = "" ;
int prob_lang = 99 ;
string lang[4] = {"Eng", "Fre", "Swe", "Ger"} ;
cout << "Type the name of the file:" << endl ;
file_name (in_file) ;
read (in_file, out_string) ;
myText.setText ( text ) ;
numb = myText.calcAbs () ;
myText.writeAbs () ;
myText.absToRel () ; // Here is error 2193
myText.plotRel () ; // Here is error 2193
cout << "The most probebly language is: " <<
lang[prob_lang] << endl ;
return 0;
}
// -------------------------------------------------------
// Class implementation
// -------------------------------------------------------
Text::Text ()
{
}
// -------------------------------------------------------
void Text::setText ( string newText )
{
text = newText ;
}
// -------------------------------------------------------
int Text::calcAbs ()
// This method calculates the number of letters in the text
{
letters = other = 0 ;
for (int i=0; i<(int) text.length(); i++)
{
if (isalpha(text.at(i))) letters++ ;
else other++ ;
}
cout << "Number of letters in the text is: " << letters << endl ;
for (int i = 0; i < NUMBER_OF_LETTERS; i++ )
n[i] = 0 ;
for (int i = 0; i < (int) text.length(); i++)
{
int index ;
if (text.at(i) >= 'a' && text.at(i) <= 'z')
{
index = text.at(i) - 'a'; n[index]++ ;
}
if (text.at(i) >= 'A' && text.at(i) <= 'Z')
{
index = text.at(i) - 'A'; n[index]++ ;
}
}
return letters ;
}
// -------------------------------------------------------
void Text::writeAbs ()
// This method prints the number of letters and the letter in to columns
{
cout << "\nLetter\tFrekvence\n" ;
for (int i = 0; i<NUMBER_OF_LETTERS; i++)
{
char b = char (i+'A') ;
if (n[i]>0) cout << b << "\t" << n[i] << endl ;
}
}
// --------------------------------------------------------
void Text::absToRel ( int numb[], double histRel[] )
// This method should calculate the histogram
{
double percent = 100 ;
for (int i = 0; i < NUMBER_OF_LETTERS; i++)
{
histRel[i] = ((double)numb[i]/(double)letters) * percent ;
}
}
// --------------------------------------------------------
void Text::plotRel (double histRel[])
// This method plots the histogram
{
cout << "The frekvence of each letter is:" << endl ;
for (int i = 0; i < NUMBER_OF_LETTERS; i++)
{
if (histRel[i]>=0)
{
char b = char (i+'A') ;
string s = "" ;
for (int c = 0; c < (int)histRel[i]; c++)
s += "**" ;
if ((histRel[i] - (int)histRel[i]) >= 0.5)
s += "*";
cout << b << "\t" << s << endl ;
}
}
}
// --------------------------------------------------------
string Text::interpret ( double histRel[] )
// This method should compare the histogram with the four langugages and give the most probeble language.
{
string lang[4] = {"Eng", "Fra", "Swe", "Ger"};
double diff ;
double square_sum_save = 999999999.0 ;
int prob_lang = 99 ;
//Comparing the different langugages
cout << "\nLanguage\tSquare sum\n" << endl ;
for (int s=0; s<NUMBER_OF_LANGUAGES; s++)
{
double square_sum_all = 0.0 ;
for (int i=0; i<NUMBER_OF_LETTERS; i++)
{
diff = INTERPRET[s][i] - histRel[i];
square_sum_all += diff*diff; //kvadratskillnad
}
cout << lang[s] << "\t" << square_sum_all << endl ;
}
//To find the most probable language
for (int s=0; s<NUMBER_OF_LANGUAGES; s++)
{
double square_sum = 0.0 ;
for (int i=0; i<NUMBER_OF_LETTERS; i++)
{
diff = INTERPRET[s][i] - histRel[i];
square_sum += diff*diff;
}
if (square_sum < square_sum_save)
{
square_sum_save = square_sum ;
prob_lang = s ;
}
}
//cout << square_sum_save << endl ;
//cout << "The most probable language is: " << lang[prob_lang]
//<< endl ;
return lang[prob_lang] ;
}
// --------------------------------------------------------
// The functions
// --------------------------------------------------------
// Checking the in file
void file_name (string &in_file)
{
string file_ending = ".txt" ;
cout << "Type the name of the file: " << endl ;
getline ( cin, in_file ) ;
if (in_file.rfind(file_ending) != in_file.length() - 4)
in_file.append (file_ending) ;
}
// --------------------------------------------------------
// Reading the file
void read (string &in_file, string &out_string)
{
string in_row ;
ifstream fin ( in_file.c_str() ) ;
if ( !fin ) //Felkontroll
{
cout << "The file doesn't exist" << endl ;
exit ( EXIT_FAILURE ) ;
}
//Saving the file row by row
while ( getline (fin, in_row) )
{
out_string.append(in_row) ;
out_string.append("\n") ;
}
}
int array_of_int[5] = { 1, 2, 3, 4, 5 };
double array_of_double[5] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
myText.absToRel(array_of_int, array_of_double);
#include <iostream>
void foo(int a[])
{
std::cout << "sizeof(a) == " << sizeof(a) << std::endl; // 4
std::cout << typeid(a).name() << std::endl; // int *
}
int main()
{
int a[4];
foo(a); // decays to a int *
}
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
From novice to tech pro — start learning today.
There's no abs method in the Text class, nor is there a call to it in main. So, I'll assume that the error actually talked about the absToRel method.
Now, you have defined that method to take two parameters :
>> void absToRel ( int numb[], double histRel[] ) ;
But you're passing no parameters when you call it :
>> myText.absToRel () ; // Here is error 2193
That's why you get the error. You need to pass the appropriate parameters when calling the method.