CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2009
    Posts
    5

    calling a function within a function

    Hello all i am currently working on an assignment and I am almost done with it. I'm having a bit of a hard time understanding the calling of a function within a function.

    Here is some background to the program. It has generated randomly the phone number, age, credit card number, and has giving each employee a 1 (for secure) or 0 (for unsecured). I bascally have two functions print_secure and print_all. If the information is not secure it will display everything and if it is, it will display certain items.

    Here is what I have so far.

    Code:
    void CustomerData::print_all()
    {
      int empNo;
    	cout << "Please enter the Employee Number: ";
    	cin >> empNo;
    	
    	if (0 > empNo || empNo > 49) // checks to make sure that user entered value is within the limit [0 .. 49] 
    		{
    		cout << "The Employee Number entered is out of bound. Please try again.\n";
    		return;
    		}
    	
    	//Employee details are printed below
    	cout << "\nEmployee details\n";
    	cout << "----------------\n";
    	cout << "Employee Number: " << employees[empNo].empNo << "\n";
    	cout << "Employee Security Preference: " << employees[empNo].security_preference << "\n";
    	cout << "Age: " << employees[empNo].empAge << "\n";
    	cout << "Phone Number: " << employees[empNo].empphone << "\n";
    	cout << "Credit Card Number: " << employees[empNo].empCCNo << "\n";
    	cout << "\n\n";
    }
    
    
    /*void CustomerData::print_secure()
    {
     	//Employee details are printed below
    	cout << "\nEmployee details\n";
    	cout << "----------------\n";
    	cout << "Employee Number: " << employees[empNo].empNo << "\n";
    	cout << "Employee Security Preference: " << employees[empNo].security_preference << "\n";
    	cout << "Employee Age: " << employees[empNo].empAge << "\n";
    	cout << "\n\n";
    }*/

    Since I don't want a classmate taking my code I only posted what I think is the main part I need help with.

    I was thing of putting an if statement in void CustomerData:: print_all() to reference void CustomerData:: print_secure() but I don't have a clue as how to reference it. Could someone offer some advise?
    Last edited by Fezzy; December 10th, 2009 at 04:03 PM.

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: calling a function within a function

    I don't understand what the question is...

    Viggy

  3. #3
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: calling a function within a function

    Quote Originally Posted by MrViggy View Post
    I don't understand what the question is...

    Viggy
    He is asking us how he should remove a few cout-lines and call print_secure instead because they actually print the same.

  4. #4
    Join Date
    Nov 2009
    Posts
    5

    Re: calling a function within a function

    Sorry mate.

    I want to create an if statement in the print_all function that says if "security_preference" has a 1 then us the "void CustomerData:: print_secure()" function as the output. How can I go about this?
    Last edited by Fezzy; December 10th, 2009 at 04:57 PM.

  5. #5
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: calling a function within a function

    Quote Originally Posted by Fezzy View Post
    Sorry mate.

    I want to create an if statement in the print_all function that says if "security_preference" has a 1 then us the "void CustomerData:rint_secure()" function as the output. How can I go about this?
    See my last post, and after that you need to create a if to check if you need the print the extra info.

  6. #6
    Join Date
    Apr 2009
    Location
    Cochin
    Posts
    83

    Re: calling a function within a function

    Quote Originally Posted by Fezzy View Post
    Sorry mate.

    I want to create an if statement in the print_all function that says if "security_preference" has a 1 then us the "void CustomerData:: print_secure()" function as the output. How can I go about this?
    Why cant you think of doing that condition check in the place where you intend to call print_secure() or print_all()?

    i mean like,

    Code:
    CustomerData cd;
    if( security_preference == 1 )
             cd.print_secure();
    else
             cd.print_all();
    "I studied everything but never topped. Today, toppers of the world's best universities are my employees"

    -William Henry Gates (Bill Gates)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured