Link to home
Start Free TrialLog in
Avatar of PiaPia
PiaPiaFlag for Sweden

asked on

Use variable from function outside a class in a method inside a class

Hi, I have trouble making my program to use a string from a function outside of the class in a method inside the class. How do I solve this?
When I type a text in the main and then call the method 'int calcAbs ()' I can make the method work. But when I try to give the file name using the function 'void file_name (string in_file) I can't make it work. The two functions are also working as they should. So what have I forgotten to do?
#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;
 
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[] ) ;
} ;
 
void file_name (string &in_file) ;
void read (string &in_file, string &out_string) ;
 
int main()
{
  string text ;
  int numb ;
  Text myText ; 
  
  int number[NUMBER_OF_LETTERS] = { 0 };
  double histRel[NUMBER_OF_LETTERS] = { 0 };   
  
  string in_file ;
  string out_string = "" ; 
  
  file_name (in_file) ;
  read (in_file, out_string) ;
 
  myText.setText ( text ) ;
 
  numb = myText.calcAbs () ;
  myText.writeAbs () ;
  myText.absToRel ( number, histRel ) ;
  myText.plotRel ( histRel ) ;
 
  return 0;
}
// -----------------------------------------------------
Text::Text ()
{
}
// -------------------------------------------------------
void Text::setText ( string newText ) 
{
  text = newText ;
}
// -------------------------------------------------------
int Text::calcAbs ()
{  
  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 ;
} 
//-----------------------------------------------------------
// The rest of the methods are here...
// Then comes the functions:
//------------------------------------------------------------
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) ;
}
// --------------------------------------------------------
void read (string &in_file, string &out_string)
{
  string in_row ;
 
  ifstream fin ( in_file.c_str() ) ;
   
  if ( !fin ) 
  {  
    cout << "The file doesn't exist" << endl ;
    exit ( EXIT_FAILURE ) ;
  } 
 
  while ( getline (fin, in_row) )
  {
    out_string.append(in_row) ;
    out_string.append("\n") ; 
  } 
  text = out_string ;
}

Open in new window

Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

>> I can't make it work
What happens?
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PiaPia

ASKER

Thanks Zoppo! This put me on the right track again! Thought I had tried it all, but obviously not... ;-)
You're welcome - I'm glad I could help ...

have a nice day,

best regards,

ZOPPO
Zoppo... nice :)