simple login program problem

This is a discussion on simple login program problem within the C Programming forums, part of the General Programming Boards category; My sample code: Code: #include <stdio.h> #include <string.h> #define name1 "denny" #define pass1 "123" int main(int argc, char *argv[]) { ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    7

    simple login program problem

    My sample code:
    Code:
    #include <stdio.h>
    #include <string.h>
    #define name1 "denny"
    #define pass1 "123"
    
    int main(int argc, char *argv[])
    {   char name[5];
        char pass[3];
    
        if(argc != 2)
        {        printf("Usage: login Your_Username Your_Password\n");
                 exit(1);
        }
        scanf(argv[1],"%s", &name); // to read username argument
        scanf(argv[2],"%s", &pass; // to read password argument
    
        if(strcmp(name,name1) == 0 && strcmp(pass,pass1) == 0)
           printf("Correct username and password");
    
        else
           printf("Wrong username and password");
        return 0;
    }
    Hi I'm a new user of c programming.
    My purpose of doing this programming is letting the user to key in their username and password.

    The program must be run from console.

    It looks like

    D:\c>login
    Usage:login Your_Username Your_Password

    But the problem is, i do not know how to read the username and the password key in by the user. anyone can fix my program?
    I'll be very thankful if someone can help me.

  2. #2
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define name1 "denny"
    #define pass1 "123"
    
    int main(int argc, char *argv[])
    {  
        char * name;
        char * pass;
    
        if(argc != 3)
        {        
            printf("Usage: login Your_Username Your_Password\n");
             exit(1);
        }
    
        name = argv[1];
        pass = argv[2];    
    
        if(strcmp(name,name1) == 0 && strcmp(pass,pass1) == 0)
           printf("Correct username and password\n");
        else
           printf("Wrong username and password\n");
    
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    thanx a lot for the help.

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Login is what I would do with GUI. But it's a matter of taste.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #5
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    You might want to consider hiding the password when the user types it in. A search of these forums would yield several possible solutions, depending on your environment O/S etc.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by maxorator
    Login is what I would do with GUI. But it's a matter of taste.
    Right because they've never, ever used text based logins. Ever.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    134

    Red face

    [QUOTE=suckss]My sample code:
    Code:
        scanf(argv[1],"%s", &name); // to read username argument
        scanf(argv[2],"%s", &pass; // to read password argument
    
        }
    QUOTE]
    After getting the exact answer what u want, Did U try to know what does argc and argv means?
    S_ccess is waiting for u. Go Ahead, put u there.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    Quote Originally Posted by SKeane
    You might want to consider hiding the password when the user types it in. A search of these forums would yield several possible solutions, depending on your environment O/S etc.
    nah, i'm quite satisfied with it now. Coz i'm tryin to do bufferoverrun.
    I might have a question again later regarding buffer overrun.
    anyway thanx for you help SKeane

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    [QUOTE=maven]
    Quote Originally Posted by suckss
    My sample code:
    Code:
        scanf(argv[1],"%s", &name); // to read username argument
        scanf(argv[2],"%s", &pass; // to read password argument
    
        }
    QUOTE]
    After getting the exact answer what u want, Did U try to know what does argc and argv means?
    yeah what does argc and agrv means...coz im new to C. i prefer c++ rather than C. i never study c before and it's quite confusing for me to see the codes.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    Can i store the username entered by the user and the password to an array instead of storing it into a pointer? what's the code will looks like?

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Yes, you need to create an array, then copy the string at the pointer into the array. Look up strcpy() (and strncpy()).

    argc and argv
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  12. #12
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    argc and argv are in both C and C++. argc is a count of the command line arguments, and argv is an array of the arguments themselves. For example: foo bar bas
    Code:
    argc == 3;
    argv[0] == foo;
    argv[1] == bar;
    argv[2] == bas;
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  3. Running Program Problem
    By warfang in forum C++ Programming
    Replies: 10
    Last Post: 03-28-2007, 03:02 PM
  4. Problem with a simple program
    By Salgat in forum C Programming
    Replies: 10
    Last Post: 06-15-2006, 06:57 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM

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