Can I call a function inside condition of if statement?

This is a discussion on Can I call a function inside condition of if statement? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <string> using namespace std; string username; string password; void passwordcheck(); void passwordwrong(); void passwordcorrect(); void passwordcorrect() ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77

    Can I call a function inside condition of if statement?

    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    string username;
    string password;
    
    
    void passwordcheck();
    void passwordwrong();
    void passwordcorrect();
    
    
    void passwordcorrect()
    {
        cout << "You may enter. \n";
        
    }
    
    
    void passwordwrong()
    {
        cout << "Access denied. \n";
        
    }  
    
    
    void passwordcheck()
    {
    
    
        
        
            if   ( (username == "a" && password == "a")
                || (username == "b" && password == "b")
                || (username == "c" && password == "c"))
        
             {
                 passwordcorrect();
                
            
    
    
             }
                 
                
    
    
            else 
             {
                   passwordwrong();
                 
             }
        
    
    
    
    
        
    }
    
    
    
    
    
    
    
    
    
    
    
    
    int main()
    { 
        for ( int attempt = 0 ; attempt < 3;  attempt++ )
         {
    
    
         cout << "Username: ";
         getline (cin, username );
         cout<< endl;
    
    
         cout << "Password: ";
         getline (cin, password);
         cout << endl;
        
    
    
         passwordcheck();
    
    
     // can you do this?
    
    
             if (passwordcorrect())
             {     
                            cout << "You may enter. \n";
                break;
             }
         
          
         }
        
        
    
    
        
    
    
            
        system("pause");
        return 0;
         
    }
    I tried like this but I get error. Can you actually call function in an if statement's condition?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,699
    Can I call a function inside condition of if statement?
    Yes, but the function will probably need to return a value that can be evaluated as true or false.

    Also in future please ask your question in the body of the post, not the title and not inside the code tags.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    Thanks Jim.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. if-else statement condition
    By YouMe in forum C Programming
    Replies: 16
    Last Post: 07-04-2012, 09:48 AM
  2. How i can call refrence function inside web serviese
    By king naif in forum C# Programming
    Replies: 5
    Last Post: 02-25-2010, 09:44 PM
  3. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 02:18 PM
  4. call to realloc() inside a function: memory problem
    By simone.marras in forum C Programming
    Replies: 15
    Last Post: 11-30-2008, 09:01 AM
  5. cin.get() inside switch statement
    By timberwolf5480 in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 12:26 AM

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