Thread: functions within functions?

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    50

    functions within functions?

    Would it be possible to call a function from within a function? I have being set a task which requires me to do a calculation in one function and without returning a value from either function, when i try calling the new function from within the current one i get the error: error C2065: 'printout' : undeclared identifier even though I've defined it before main. Any ideas?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    That should be possible. Post your code.

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    This is the whole thing:

    Code:
    #include <iostream>
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::fixed;
    
    #include <cmath>
    
    
    
    void seperate (double numbertobesplit); // function one //
    void printOut (double wholepart, double fractional, int numbersign); // function two // 
    
    int main()
    {
        double UsersNumber;
        cout << "Enter a value to analyse:";
        cin >> UsersNumber;
        seperate (UsersNumber);
    
    
        return 0;
    }
    
    void seperate (double numbertobesplit) // the first function // 
    
    {
        double wholepart, fractional;
        int numberssign;
        
        wholepart = floor (numbertobesplit);
        fractional = fabs (numbertobesplit) - wholepart;
        
        if (wholepart > 0){
            numberssign = 1;}
        else{
            numberssign = 0;}
    
        printout (wholepart, fractional, numberssign); // this is the call to the second function //
    
    }
    
    void printOut (double wholepart, double fractional, int numberssign) // second function starts here // 
    
    {
        cout << "sign: ";
            if (numberssign = 1){
                cout << "+\n";}
            else{
                cout << "-\n";}
    
        cout << "Whole number magnitude: " << wholepart << endl;
        cout << "Fractional part: " << fractional << endl;
    
    }
    If you are wondering its supposed to split a floating point number into three pieces, one part is the sign, the next is the magnitude followed by the fractional part.

  4. #4
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    You defined printOut(), then called printout(). Compilers are case sensitive.
    Last edited by Scribbler; 01-25-2005 at 05:26 PM.

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    Bugger! well that would teach me to follow our instructions won't it?

    Now it actually works, i have another quick problem! Is there something in c++ that says that double length floating point numbers are unable to be negative, i ask this because whenever i put a negative number into the code it never picks it up in the if statement (if wholepart > 0), it always comes out as positive? would it be easier to use a boolean expression?

  6. #6
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Code:
    if (numberssign == 1){
        cout << "+\n";}
    else{
        cout << "-\n";}

  7. #7
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    if (numberssign = 1); assigns numberssign to the value 1 then returns true. Then else{ numberssign = 0;} assigns numberssign to 0, and once again returns true. Finally, the printOut() function once again assigns it back to 1.

  8. #8
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    heh, I've just being playing with my code and it seems alot easier to just use booleans, i actually noticed how to fix the errors only a few minutes after i posted, guess i should spend more time coding and less time posting

    Anyway, i think I've removed all the bugs from it, it can now handle negative numbers and even remove its sign when its displayed, i don't suppose anyone can test it for me make sure I'm not missing anything daft

    Code:
    // ICA - Question 3 - Peter Dermott - c3024654 //
    
    #include <iostream>
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::fixed;
    
    #include <cmath>
    
    
    
    void seperate (double numbertobesplit); // function one //
    void printOut (double wholepart, double fractional, bool numbersign); // function two // 
    
    int main()
    {
    	double UsersNumber;
    	cout << "Enter a value to analyse:";
    	cin >> UsersNumber;
    	seperate (UsersNumber);
    
    
    	return 0;
    }
    
    void seperate (double numbertobesplit) // the first function // 
    
    {
    	double wholepart, fractional;
    	bool numberssign;
    	
    	wholepart = floor (numbertobesplit);
    	fractional = fabs (numbertobesplit) - wholepart;
    	
    	if (wholepart >  0){
    	fractional = fabs (numbertobesplit) - wholepart;
    		numberssign = true;}
    	else{
    	
    		fractional = fabs (numbertobesplit) + wholepart + 1;
    		wholepart = (wholepart * -1) - 1;
    
    		numberssign = false;}
    
    	printOut (wholepart, fractional, numberssign); // this is the call to the second function //
    
    }
    
    void printOut (double wholepart, double fractional, bool numberssign) // second function starts here // 
    
    {
    	cout << "sign: ";
    		if (numberssign == true){
    			cout << "+\n";}
    		else{
    			cout << "-\n";}
    
    	cout << "Whole number magnitude: " << wholepart << endl;
    	cout << "Fractional part: " << fractional << endl;
    
    }
    Thanx

  9. #9
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    why do you have:
    Code:
    using std::fixed;
    ?

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    because the fixed modifier is in the std namespace...

  11. #11
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    But he never uses fixed anywhere in his code.

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Maybe he is planning on using it later

  13. #13
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    That's crazy horse! lol

  14. #14
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    Nah, i thought that i would need it for some reason, thanks for spotting that

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Bah way to blow my cover petedee

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM