Thread: Simple log in program

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    13

    Simple log in program

    Hello guys i need to make a simple log in program that will ask for the user to enter a username and password, and just like a log in program, the entered username and password must be the same with the default username and password in the program. Here is my code,but it looks like it isn't working.

    Code:
    #include <iostream.h>
    #include <conio.h>
    char defusername[50]="test",user[50],pass[50];
    char defpassword[50]="try";
    int main(){
    cout<<"Username:";
    cin>>user;
    if(defusername == user)
    cout<<"\nEnter your password:";
    cin>>pass;
    if(defpassword == pass)
    cout<<"\nWelcome GUILDNAME to the Infinity Tower!";
    
    getch();
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    1. This is C++ code, but you posted it to the C forum.

    2. Make sure your code is neatly formatted and indented:

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    char defusername[50]="test",user[50],pass[50];
    char defpassword[50]="try";
    
    int main()
    {
        cout<<"Username:";
        cin>>user;
    
        if(defusername == user)
            cout<<"\nEnter your password:";
    
        cin>>pass;
    
        if(defpassword == pass)
            cout<<"\nWelcome GUILDNAME to the Infinity Tower!";
    
        getch();
    
        return 0;
    }
    2b. Notice how much clearer the logic is when indented properly? You may want to put everything after the first "if" into a block, since currently the program will expect the password input regardless if the username is correct or not.

    3. You should be using "iostream" instead of "iostream.h", and you most certainly should not be using "conio.h".

    4. You should not be using global variables.

    5. If you're learning C++, why are you using C-strings?

    6. Don't just tell us "it isn't working", tell us how it isn't working. Is it compiling? Are there errors? Are you not getting the expected output?

    If you're using C-strings (null-terminated character arrays), you can't compare strings with ==.

    Out of curiosity, what language are you supposed to be learning (C or C++) and what learning material are you using?
    Last edited by Matticus; 12-15-2016 at 06:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  5. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM

Tags for this Thread