Solved

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

Posted on 2009-07-10
5
498 Views
Last Modified: 2013-11-17
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

0
Comment
Question by:PiaPia
  • 2
  • 2
5 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 24822268
>> I can't make it work
What happens?
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 250 total points
ID: 24822344
Hi PiaPia,

as far as I understand the code I think the problem is in the 'read' function - unfortunateley I cannot understand how this can compile at all since I can't see where the 'text' (line 127) is declared - at least it seems as if it is a global variable because it's neither declared in 'read' nor passed to 'read'.

The 'text' you use in 'calcAbs' is a member of the 'Text' class, so it is a different variable than the 'text' set in 'read'.

In 'main' you even use 'text', but here it's a local variable ...

To correct this IMO you just need to change the line 46 to:

> myText.setText ( out_string ) ;


Hope that helps,

ZOPPO
0
 

Author Comment

by:PiaPia
ID: 24822666
Thanks Zoppo! This put me on the right track again! Thought I had tried it all, but obviously not... ;-)
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 24822679
You're welcome - I'm glad I could help ...

have a nice day,

best regards,

ZOPPO
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24822691
Zoppo... nice :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
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…

932 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