Solved

Trouble with an array method in a class

Posted on 2009-07-07
23
419 Views
Last Modified: 2013-11-17
Hi, I have trouble with one of my methods in a class. The error I get is "E2193 ... Too few parameters in call to 'Text::abs (int *, double *)' in function main()". Where have I gone wrong? I've tried all day to figure it out, but what I ever I try I can't figure it out. Can someone please help a complete beginner?
The program using a class shall give the most probable language of a text file. I have the whole program in one cpp-file.
The program is not yet completed, but this error is the only one so far. :-)
/PiaPia

#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") ; 

  } 

}

Open in new window

0
Comment
Question by:PiaPia
  • 9
  • 7
  • 2
  • +2
23 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 125 total points
Comment Utility
>> "E2193 ... Too few parameters in call to 'Text::abs (int *, double *)' in function main()"

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.
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 75 total points
Comment Utility
>>  myText.absToRel () ; // Here is error 2193
>>   myText.plotRel () ;  // Here is error 2193

Look at the function definitions of these 2 functions, they both take parameters that you are not passing.


void Text::absToRel ( int numb[], double histRel[] )
void Text::plotRel (double histRel[])
0
 
LVL 7

Expert Comment

by:Valleriani
Comment Utility
Well looking at:

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 ;    
    }    
  }
}

They both require values:

While you specified

  myText.absToRel () ; // Here is error 2193
  myText.plotRel () ;  // Here is error 2193

But for myText.absToRel you need two values, and plotRel you need one, for example:

  myText.absToRel (3, 3.1) ; // Here is error 2193
  myText.plotRel (3.151) ;  // Here is error 2193


Basicly, you need to give it values, they can be a integer/double value that you already have in your main class, for example, using 'numb' for one of the values. (Integer)

I believe thats the issue thuogh.
0
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
Well, I guess that's 3 of us that agree on the problem :)
0
 

Author Comment

by:PiaPia
Comment Utility
If I put it like this in 'main' (at least that what I think you mean):
 myText.absToRel ( numb[], histRel[] ) ;
I get another error: E2188 Expression syntax in function main ().
Or perhaps I don't fully understand you....??

/PiaPia
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
To add to above comments:

The  absToRel member function takes an array of integers (size is 26 for any letter) and an array of doubles which is output.

The array of integers probably could be spared as argument as you have a member array n in class Text which look as it should be used for that. But the output histogram array must be provided by your main function, or you would need to add the array as a member to class Text as well (what could make sense as you now need the array to pass to the print member function).

So you either should have

int main()
{
     int numbers[NUMBER_OF_LETTERS] = { 0 };
     double histrels[NUMBER_OF_LETTERS] = { 0 };

     ...
     numb = myText.calcAbs () ;
     myText.writeAbs () ;
     myText.getNumbers(numbers);  // would copy the Text::n array to numbers
     myText.absToRel (numbers, histrels) ;   // should compile now
     myText.plotRel (histrels) ;                     // should compile now

}

or you change class Text so that it has a double array hist additional to array n and change the absToRel and plotRel so that both don't have arguments but take the member arrays instead.

0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 125 total points
Comment Utility
>> If I put it like this in 'main' (at least that what I think you mean):
>>  myText.absToRel ( numb[], histRel[] ) ;

You need to pass two instances of the given type as the parameter.
The function expects an array of int and an array of double as parameter, so that's what you'll have to pass as parameter. See the below code for example.

Obviously, you'll have to pass the correct arrays with the correct data in them, so that it fits whatever you want the method to do ... (you've created the method, so you know what it expects). The values used below are just for illustration purposes, and are likely not to be the actual values that you want/need to use.
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);

Open in new window

0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 50 total points
Comment Utility
>>>> myText.absToRel ( numb[], histRel[] ) ;
The brackets [] can only be used in the declaration and definition of a function but not when calling it:

   class Text
   {
        ...
        void absToRel(int numb[], double hist[]);   // here the brackets are allowed and necessary
                                                                           // they tell the compiler that the function takes
                                                                           // an array and not only a single int resprectively double



    int main()
    {
          // the = { 0 } makes the elements all 0
          int numbers[26] = { 0 };  // here we need the brackets with a constant size
          double hist[26] = { 0 };   // same applies here
          ...
          text.absToRel(numbers, hist);   // here we have no brackets as the compiler already knows
                                                            // that the arguments are arrays

          ...  
    }
0
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
>> // they tell the compiler that the function takes
>> // an array and not only a single int resprectively dou

Actually, that's not completely true... in a function declaration that is just syntactic sugar for a pointer since you can't pass an array to a function like that (it just decays to a pointer). Try sizeof(numb) and it'll be the same size as sizeof(int *)
#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 *

}

Open in new window

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>>>> // they tell the compiler that the function takes
>>>>>> // an array and not only a single int resprectively dou

>>>> Actually, that's not completely true...

Hmmm. I think it is completely true.

If the compiler desides - for IMO bad reasons - to not using the information that the given variable is an array, it doesn't prevent from the fact that we did tell it and that the brackets indeed is (one of) correct syntax how to tell it ;-)

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 40

Expert Comment

by:evilrix
Comment Utility
>> Hmmm. I think it is completely true.
It will never be true -- never ever! When you pass an array to a function it will always decay to a pointer.

http://www.transcendentaxis.com/dthompson/blog/archives/9

Pointer Decay

When we pass an array through a pointer, sizeof loses the ability to figure out this size information:

void value(const T some_array[])
void pointer(const U* const some_array)

Given just these two function declarations and no other code, could YOU figure out how big the passed in array was? In fact, in the case of the pointer function, could you even figure out that for sure that an array was being passed? What happens, then, is that the array decays into a pointer. Calling sizeof on some_array will simply return the sizeof that pointer. sizeof(some_array) turns into sizeof(T*).

Also, passing using the some_array[] syntax is essentially the same as the pointer syntax. It was included mostly for completeness.

When we pass by reference, though, were required to send along the array size as well. This makes our function declaration:

void reference(const T (&some_array)[U])

Using this information, sizeof is able to figure out the actual size of the array (U * sizeof(T)).
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> It will never be true
You should notice that I only said I *tell* it to the compiler that the variable is an array what surely is undeniable ;-)  A second thing is that the compiler turns it to a pointer what doesn't make the first statement a wrong statement.

We shouldn't proceed the off-topic, ok.
0
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
>> notice that I only said I *tell* it to the compiler that the variable is an array what surely is undeniable ;-)
Notice I said, "that is just syntactic sugar" :)

But, I guess we're now talking semantics and it seems we are agreeing -- in an odd sort of way :)

>> We shouldn't proceed the off-topic, ok.
Actually, I think it's an important thing to make clear for PiaPia

Unfortunately, I don't believe the answer accepted is actually the asnwer to the original question, "Hi, I have trouble with one of my methods in a class. The error I get is "E2193 ... Too few parameters in call to 'Text::abs (int *, double *)' in function main()". Where have I gone wrong?". This was answered by the first 2 posts (I say 2 because they were both posted at the same time). That being the case I'm afraid I'm going to RA this one. Sorry Alex.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> I'm going to RA this one. Sorry Alex.
No probs, though my add-on comment http:#24792909 also answers the initial question (and maybe was more helpful than explaining the error message outputed by the compiler).
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
@PiaPia

I hope, you were not discouraged by some of the discussions here.

For the knowledgebase it is important that the answer accepted by the asker fits to the initial question. Otherwise people searching in the knowledgebase would need to reread a whole thread what is impossible.

So, as the answer you accepted wasn't an answer to the initial question, it only could be used as an assisted solution but not as the accepted solution. So, after evilrix reopened the thread you should accept any of the earlier questions which answered you initial question and could give assist points to any other answer which helped somehow. You are free how to split points.
0
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
>> No probs
Thanks Alex. Since I've participated in this Q as an expert I've RA'd it to let a mod handle things rather than reopening it myself.

>> I hope, you were not discouraged by some of the discussions here.
Me too :) As Alex has already explained (but just to confirm) the only reason I am requesting this be re-opened is to ensure the answers selected match the Q for the PAQ (Previously Answered Question) database, which is used by others when looking for help.

Many thanks to you all.

-Rx.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
PiaPia accepted my comment #24792942 as only solution what must be corrected as this question answers a follow-up question and not the initial question. Though that was accepted by me we should see that it is a rare exception here in EE that moderators were overruling an asker's valuation for sake of the integrity of the knowledgebase.

Having that in mind, it doesn't seem fair to me that the answer #24792909 which helped most - as the comment from PiaPia given when firstly accepting the answer showed - should get equal points to questions which may have answered the initial question but have not been chosen as solution or assist by PiaPa. Though now an assist the answer #24792909 should get more points than answers which didn't help so much. Moreover, to fulfil the requirement that the accepted answer should answer the initial question, it is enough that one of #24792663 (Infinity08),  #24792668 (evilrix),  #24792679 (Valleriani), or #24792909 (itsmeandnobodyelse) got points cause all four meet the requirement for an accepted answer.

When using my comment #24792909 as accepted answer and my comment #24792942 as assist, both the will of the asker and the requirements of a PAQ'ed solution could be met, what seems a fair solution to me.
0
 

Author Closing Comment

by:PiaPia
Comment Utility
Thank you for explaining in a really simple way, which made it so much easier for me to understand where I had gone wrong!
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> the only reason I am requesting this be re-opened is to ensure the answers selected match the Q for the PAQ (Previously Answered Question) database

That is the recommendation of the ZA as documented in the thread.

>>>> and is being closed based on the recommendations of the Zone Advisors.

and that statement either is false or there are further recommendations of the Zone Advisor which were not documented here.

>>>> Thank you for explaining in a really simple way, which made it so much easier for me to understand where I had gone wrong!

The comment PiaPia made when accepting my comment  #24792942 undoubtedly makes clear that PiaPia wanted to give the points to me. You have had the chance to select my comment #24792909 which refers to the above 3 solutions and gave useful tips how to procede.

Instead you accept the first solution, give an assist to the second solution and ignore the third solution of Valleriani though all three sucessfully translated the error message posted "E2193 ... Too few parameters in call to 'Text::abs (int *, double *)' in function main()" from English to English. Then, you give equal points to #24792927 which also answers the follow-up and not the initial q. but explicitly was not selected from PiaPia. All that 'based on the recommendations of the Zone Advisor' which nowhere could be found and who explicitly had declared that he let some moderator handle it.

I don't know what was happening with that forum and me, but I don't like it.
0
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
>> or there are further recommendations of the Zone Advisor which were not documented here.
Did you check the CS-G threads related to this question?

http:/Q_24550470.html
http:/Q_24563548.html

I both cases my recommendations, with reasons, are clearly documented (so I won't repeat them again).

>> give an assist to the second solution and ignore the third solution of Valleriani
I will respond to this point for you though... the first two answers were posted at the same time and it is generally accepted that when answers are posted at the same time they are both awarded points. The 3rd answer was 4 minutes after the first was posted and for this reason I did not consider it as posted "at the same time".

I hope that now clarifies things for you.

Best regards,

-Rx.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> I hope that now clarifies things for you.
Yes it does. I didn't know of the CS-G threads.

Two little remarks (only as I had no possibility to answer to your - slightly different - new recommendations).

>>>> By this point the problem has been resolved. How could this help the most when it comes in AFTER the actual problem has been identified?

The problem was identified but no solution was given. Both the first answers only were translating what the error message told anyhow. PiaPia obviously is a beginner but all your answers were directed to a programmer. In both my comments I added code what PiaPia actually should do to overcome her problems. As the first of my comments refers to the initial comments it would have been suitable for an accepted answer same as it was in http:Q_24546546.html.

>>>> This answer provides clarification to a related follow-up question http:#a24792927 (50 pts)

>>>> And the following expands on it http:#a24792942 (50 pts)
 
My answer was longer and posted three minutes later. It was the one the asker had accepted and has thanked for. To tell that the one expands the other maybe is one of the strangest statements in that thread which indeed has a lot of oddities.

But I thank you for making things clear and apologize by those who have performed the recommendations of the Zone Advisor as they have stated and where I had doubts whether thatr was true.





0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now