Thread: Calling a member function from a member function?

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    9

    Calling a member function from a member function?

    Ok, I have a function as part of my class... I want it to test a password, if it is incorrect then I want it to call itself again.. But how do I call the function from itself?

    I also want to know for future reference how to call another member function from another member function...

    I'm pretty much a newb and I thought you had to make an objeect to call class functions and use class variables etc. but there must be a way to do this...

    Thanks

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    What you could do is this :

    Make your function return a bool. (Whether the password was correct or not)

    Then, in a while loop, you can call the function until the password is correct.

    Eg.
    Code:
    #include <iostream.h>
    #include <string.h>
    
    bool function(char* name)
    {
           if(name==NULL) return false;
    
           if(strcmp(name,"Smurf")==0)  
               return true;
           else 
               return false;
    }
    
    int main()
    {
         char name[20];
         cin >> name;
         while(!function(name))
         {
               cin >> name;
         }
         return 0;
    }

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Here, look at this code:
    Code:
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    class cPassword
    {
    public:
    	cPassword();
    	~cPassword(){};
    
    	int  askPass();
    	bool compare(char* p1,char* p2,char* p3,char* p4,char* p5);
    private:
    
    	char pass1[256];
    	char pass2[256];
    	char pass3[256];
    	char pass4[256];
    	char pass5[256];
    };
    
    cPassword::cPassword()
    {
    	strcpy(pass1,"password1");
    	strcpy(pass2,"password2");
    	strcpy(pass3,"password3");
    	strcpy(pass4,"password4");
    	strcpy(pass5,"password5");
    }
    
    bool cPassword::compare(char* p1,char* p2,char* p3,char* p4,char* p5)
    {
    	if(strcmpi(p1,pass1) && strcmpi(p2,pass2) && strcmpi(p3,pass3) && strcmpi(p4,pass4) && strcmpi(p5,pass5))
    		return false;
    	else
    		return true;
    }
    
    int main()
    {
    	cPassword oMenu;
    	char pass1[256],pass2[256],pass3[256],pass4[256],pass5[256];
    
    	cout << "The menu is protected under 5 passwords. Please enter them now.\n\n";
    
    	cout << "Password #                                  Enter Password\n";
    	cout << " 様様様様様様様様様様様様様様様様様様様様様様様様様様様様様\n";
    	cout << "    1                                 ";
    	cin.getline(pass1,256,'\n');
    	cout << "    2                                 ";
    	cin.getline(pass2,256,'\n');
    	cout << "    3                                 ";
    	cin.getline(pass3,256,'\n');
    	cout << "    4                                 ";
    	cin.getline(pass4,256,'\n');
    	cout << "    5                                 ";
    	cin.getline(pass5,256,'\n');
    
    	//Im a bit stuck here also, Im not sure the best way to keep calling the function until its correct values...
    	while(!oMenu.compare(pass1,pass2,pass3,pass4,pass5))
    	{
    		cout << "Password #                                  Enter Password\n";
    		cout << " 様様様様様様様様様様様様様様様様様様様様様様様様様様様様様\n";
    		cout << "    1                                 ";
    		cin.getline(pass1,256,'\n');
    		cout << "    2                                 ";
    		cin.getline(pass2,256,'\n');
    		cout << "    3                                 ";
    		cin.getline(pass3,256,'\n');
    		cout << "    4                                 ";
    		cin.getline(pass4,256,'\n');
    		cout << "    5                                 ";
    		cin.getline(pass5,256,'\n');
    	}
    	return 0;
    }
    Whay I would suggest is to check each password once it's typed in.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Here's a simple example of calling member functions internally:



    Code:
    class Foo {
    
    private:
    
    char *data;
    
    public:
    
    
    bool Data_Is_Null(){ return !data ? true : false; }
    
    void Print() 
     { 
       if( Foo::Data_Is_Null() ) cout << "No Data! << endl; //<-preferred...
    
       //...this works too->//  if( Data_Is_Null() ) cout << "No Data! << endl;
       
       //...NOOO!!-> // if( this->Data_Is_Null() ) cout << "No Data! << endl;
       else cout << data << endl;
      }
       
    };
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    273

    Re: Calling a member function from a member function?

    Originally posted by Chroder
    I want it to test a password, if it is incorrect then I want it to call itself again..
    The others already suggested a loop and I agree of course. recursive calls in this scenario are not a good idea. Your just building up your call stack for no good reason. I suppose someone could be really dense and just keep putting in the wrong password and eventually overflow your stack. Just something to keep in mind

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    9
    Have a look at what I did last night,

    Code:
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    class cPassword
    {
    public:
    	cPassword(char p1[256], char p2[256], char p3[256], char p4[256], char p5[256]);
    	~cPassword() {}
    	void askPass();
    
    private:
    	char setPass1[256];
    	char setPass2[256];
    	char setPass3[256];
    	char setPass4[256];
    	char setPass5[256];
    
    	char pass1[256];
    	char pass2[256];
    	char pass3[256];
    	char pass4[256];
    	char pass5[256];
    };
    
    cPassword::cPassword(char p1[256], char p2[256], char p3[256], char p4[256], char p5[256])
    {
    	strcpy(setPass1, p1);
    	strcpy(setPass2, p2);
    	strcpy(setPass3, p3);
    	strcpy(setPass4, p4);
    	strcpy(setPass5, p5);
    }
    
    void cPassword::askPass()
    {
    
    	while(strcmpi(setPass1,pass1) && strcmpi(setPass2,pass2) && strcmpi(setPass3,pass3) && strcmpi(setPass4,pass4) && strcmpi(setPass5,pass5))
    	{
    		cout << "Please enter the 5 security passwords.\n\n";
    
    		cout << "Password #                                  Enter Password\n";
    		cout << "様様様様様様様様様様様様様様様様様様様様様様様様様様様様様\n";
    
    		cout << "    1                                 ";
    		cin.getline(pass1,256,'\n');
    
    		cout << "    2                                 ";
    		cin.getline(pass2,256,'\n');
    
    		cout << "    3                                 ";
    		cin.getline(pass3,256,'\n');
    
    		cout << "    4                                 ";
    		cin.getline(pass4,256,'\n');
    
    		cout << "    5                                 ";
    		cin.getline(pass5,256,'\n');
    
    		if(strcmpi(setPass1,pass1) && strcmpi(setPass2,pass2) && strcmpi(setPass3,pass3) && strcmpi(setPass4,pass4) && strcmpi(setPass5,pass5))
    			cout << "Incorrect Password(s)\n\n";
    	}
    }
    
    
    int main()
    {
        cPassword oMenu("aa","ss","dd","ff","gg");
    	oMenu.askPass();
    
    	cout << "Yay\n\n";
    
    	return 0;
    }
    That works perfectly. I wanted the askPass to have all of the testing and asking internally because I plan to have numerous password protected areas... So hows that for a newb?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a member function from a different file
    By cdonlan in forum C++ Programming
    Replies: 3
    Last Post: 02-01-2005, 12:50 AM
  4. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM