Calling a function within a function.

This is a discussion on Calling a function within a function. within the C++ Programming forums, part of the General Programming Boards category; Hi! I am at the end of a very long program and I have one last thing to do. That ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    15

    Calling a function within a function.

    Hi!

    I am at the end of a very long program and I have one last thing to do.

    That is call an integer function into a string function with one integer parameter:

    Code:
    string pwType (int test_failed)
    	
    	{
    	
    	string pw_strength;
    
    	
    
    	if ( (pwTest ) == (0))
    		pw_strength = " strong " ; 
    
    	else if   ( int (pwTest)  == (1))
    		pw_strength = " medium ";
    
    	 else if  (int (pwTest) == 2 || int (pwTest) == 3) 
    		pw_strength =  "medium weak" ;
    	else if (int (pwTest) >= 4)
    		pw_strength = " weak " ;
    
    	
    	return pw_strength;
    
    }
    
    int pwTest(string password, bool verbose, int sec1, int sec2 )
    Those are the two functions I am using. For the sake of time and space I didn't copy the entire pwTest function because it checks about 10 different things.

    Here's what I am trying to do in the main fucntion:


    Code:
    cout <<"Your password is a " << pwType(pwTest (password, verbose,  sec1,  sec2))    << " password." << endl;
    I want the output to be: "Your password is a (one of the for types listed) password."

    However, when I call it I am always getting: "Your password is a weak password."
    Or when I change it up I often get: "Your password is a 001091 password." or some other random number. Which I am guessing is its location in RAM.

    Well, any suggestions would be greatly appreciated!

    Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Were you planning to call pwTest inside your pwType function, or pass the result of pwTest into your function? The second is what you do in your cout statement. The actual pwType function is just broken. (Hint: what is the value of "pwTest", and double hint: no function is called there.)

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    15
    I am trying to pass the result of pwTest to pwType. The result of pwTest is an integer ranging from 0-8 depending on the number of tests failed.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by CPPN0OB View Post
    I am trying to pass the result of pwTest to pwType. The result of pwTest is an integer ranging from 0-8 depending on the number of tests failed.
    So that's no problem -- but you have to use the name of the variable you declare:
    Code:
    string pwType (int test_failed)
    	
    	{
    	
    	string pw_strength;
    
    	
    
    	if ( (pwTest ) == (0))
    		pw_strength = " strong " ; 
    
    	else if   ( int (pwTest)  == (1))
    		pw_strength = " medium ";
    
    	 else if  (int (pwTest) == 2 || int (pwTest) == 3) 
    		pw_strength =  "medium weak" ;
    	else if (int (pwTest) >= 4)
    		pw_strength = " weak " ;
    
    	
    	return pw_strength;
    
    }
    If you randomly start using other names in your code, don't expect the right value to be used.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    15
    wow! Thank you for that! I feel really dumb right now! I guess I was thinking ahead when I wrote that. Thank you very much!

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    15
    I've got everything else to work. However, while testing I came across the bug.
    When I was testing to see the output when I entered only 1,2 or 3 characters in the string this error kept popping up:

    "Expression: string subscript is out of range."

    I have no idea where I went out of range. I put if statements in order to check it.

    Here's the code:

    Code:
    if(myIsUpper(password [0]) == false) //Function call to myIsUpper to check if the first character is upper case.
    	{
    		failed_counter ++;
    
    			if (verbose == true) // If verbose mode is on, returning a message to the user
    
    				if (password.length() >=1) // Check to see if there is a first character.
    				cout << "The first character is not a capital letter." << endl; 
    
    				else if (password.length() <1) 
    						cout << "There is no first character." << endl;
    	}
    
    if ( myIsLower(password [1])== false  || myIsLower (password[2] ) == false)  /* Function call to myIsLower to see if the second or third
    																			       / character is lowercase. */ 	
    	{
    		failed_counter ++;
    
    		if (verbose == true) // If verbose mode is on, returning a message to the user
    			if (password.length()>=2 ) //Check to see if the password is 2 or longer than two characters.
    				cout <<"The second and/or third character are not lower case letters." << endl;
    
    					else if(password.length()<=1) //Check to see if password is 1 or less than one character long.
    					cout <<"There is no second or third character."<<endl;
    	}
    I'm 99% sure the error is being generated in one of these statements.

    Any suggestions?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you don't have three letters, then password[2] does not exist. You should check the length first, and only if there's enough letters try to check the letters themselves.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21