Thread: Can I use void to write a function to test for true or false?

  1. #1
    Registered User
    Join Date
    Jun 2015
    Location
    Grand Island, NY
    Posts
    7

    Can I use void to write a function to test for true or false?

    My password program checker program works fine but now I would like to add a function that I can use to check that username and password are correct. Is my general coding ok or is it just plain wrong? The pass_checker is my question. I am now getting an error: expected ')' before 'pass_checker' and else without a previous if. I don't get these errors when I don't use void pass_checker. Thanks in advance.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    string username;
    string password;
    
    
    void user_pass ()
    {
            cout << "Enter your username: " << endl;
            cin >> username;
            cout << "Enter your password: " << "\n";
            cin >> password;
    }
    
    
    void pass_checker ()
    {
        ( username == "root" && password == "1") || (username == "beer" && password == "2");
    }
    
    
    int main()
    
    
    
    
    
    
    
    
    {
      int i = 0;
        user_pass ();
          if pass_checker (); //(( username == "root" && password == "1") || (username == "beer" && password == "2"))
               { cout << "Access allowed" << "\n"; }
           else
    
    
           {
               cout << "Bad username or password. Denied access!" << "\n";
    
    
    
    
                while ( i < 2 )
                {
                    user_pass ();
                      if (( username == "root" && password == "1") || (username == "beer" && password == "2"))
                       { cout << "Access allowed" << "\n"; }
                       else
                        { cout << "Bad username or password. Denied access!" << "\n";}
    
    
                        i++;
                }
            }
    }

  2. #2
    Guest
    Guest
    For your own clarity and those who are to read you code, please format it properly. If you use tabs in your editor, it most likely offers a feature to replace those with 4 spaces.

    So instead of:
    Code:
    int main()
    
    
                                                          {
    
    
    
    
    
                        cout <<
    
    
                              "Hello World!"
    
    
    
         << endl;
    
    
    }
    ... consider:
    Code:
    int main()
    {
        cout << "Hello World!" << endl;
    }
    You get the idea.

    While user_pass and pass_checker might be able to access your variables, this is due to them being declared globally. I would advise against doing this, as it is considered back practice in most cases and makes your program harder to reason about. So declare username and password locally inside main in your example.

    Using functions involves learning about passing variables to them. So before rushing into this, I suggest you consult the web on how to do that.

    If you want to use pass_checker as a condition for an if statement, it should return a bool value.
    Code:
    bool pass_checker(const string& username, const string& password)
    {
        return (username == "root" && password == "1") || (username == "beer" && password == "2");
        // ^ returns the outcome of your expression; either true or false.
    }

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    74
    You are in your code so declare
    void pass_checker () {


    as well as the use boool !!




    But i see people have already written very well.
    Your code, in general, appears to be about normal!
    Last edited by Дмитро; 06-26-2015 at 08:02 AM.

  4. #4
    Registered User
    Join Date
    Feb 2015
    Posts
    74
    Yes, something does not add up.
    No compilation.
    And I do not quite understand this.

    Ошибка 1 error C2061: синтаксическая ошибка: идентификатор "pass_checker" d:\rozbir\ falsetrue\falsetrue\falsetrue\falsetrue1.cpp 25 1 Falsetrue

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    We neither read nor speak russian (or whatever language it is), so it makes even less sense to us.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Feb 2015
    Posts
    74
    I like your code.
    I added brackets and a pause.
    Thank you for the task.

    Code:
      
    #include<iostream>
    #include<string>
    usingnamespace std; 
    string username; 
    string password; 
    void user_pass () { 
    cout << "Enter your username: " << endl; 
    cin >> username; 
    cout << "Enter your password: " << "\n"; 
    cin >> password; } 
    bool pass_checker(const string& username, const string& password)
    { return (username == "root" && password == "1") || (username == "beer" && password == "2"); // ^ returns the outcome of your expression; either true or false. 
    } 
     
     
    int main() 
    { int i = 0; 
    user_pass (); 
    if (pass_checker (username,password)) //(( username == "root" && password == "1") || (username == "beer" && password == "2")) 
    { cout << "Access allowed" << "\n"; } 
    else 
    { cout << "Bad username or password. Denied access!" << "\n"; 
    while ( i < 2 ) 
    { user_pass (); 
    if (( username == "root" && password == "1") || (username == "beer" && password == "2")) 
    { cout << "Access allowed" << "\n"; } 
    else
    { cout << "Bad username or password. Denied access!" << "\n";
    } 
    i++;
    } 
    } 
    system("Pause"); 
     
    return 0;
    } 
    

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indent code, please. Unreadable.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. True or False
    By camel-man in forum C Programming
    Replies: 9
    Last Post: 02-14-2013, 01:27 AM
  2. write a function that returns true
    By tingting in forum C Programming
    Replies: 8
    Last Post: 08-10-2009, 09:15 PM
  3. true or false
    By johngav in forum C Programming
    Replies: 4
    Last Post: 03-19-2009, 02:25 PM
  4. need help.. true/false
    By salmansalman in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2008, 10:10 AM
  5. Help with True, False, NOT, AND, OR...
    By cgsarebeast in forum C++ Programming
    Replies: 22
    Last Post: 05-09-2006, 07:59 PM