very noobie simple login

This is a discussion on very noobie simple login within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> int main(void) { char name1, name2, pass1, pass2; printf("input username: "); scanf("%s", &name1); printf("input password: ...

  1. #1
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49

    very noobie simple login

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main(void)
    {
    char name1, name2, pass1, pass2;
    
    printf("input username: ");
    scanf("%s", &name1);
    printf("input password: ");
    scanf("%s", &pass1);
    
    puts("registration successful");
    printf("USERNAME: ");
    scanf("%s", &name2);
    if(strcmp(name1, name2)==0)
    {
    puts("good now input your password");
    }
    else
    printf("badbye\n");
    
    printf("PASSWORD: ");
    scanf("%s", &pass2);
    if(strcmp(pass1, pass2)==0){
    puts("logging in successful");
    }
    else
    {
    puts("wrong password");
    }
    
    getchar();
    return 0;
    }
    anyone there?? i need help here can't get it right....

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    Use char arrays rather than chars for your strings.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    4
    Use something like:
    Code:
    char name1[20], name2[20], pass1[20], pass2[20];
    instead of
    Code:
    char name1, name2, pass1, pass2;

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    . . . and since your variables are arrays, you can drop the & on your scanf calls. Or better yet, use fgets(). FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    And use this to pause at the end:
    Code:
    int c;
    while((c = getchar()) ~= '\n' && c != EOF);
    See the FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    You might want to exit the program when the user enters an invalid username.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    1
    I'm learning c as well, and thought I'd have a go at this. Took your code and came up with this.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	char name1[15], name2[15], pass1[15], pass2[15]; //create char arrays
    	int validUser=0;
    
    	// Register a username and password
    	printf("input username: ");
    	scanf("%s", name1);
    	printf("input password: ");
    	scanf("%s", pass1);
    	puts("registration successful");
    
    	// Check for registered username
    	printf("USERNAME: ");
    	scanf("%s", name2);
    	if(strcmp(name1, name2)==0)
    	{
    	validUser=1; // flag as good username
    	puts("good now input your password");
    	printf("PASSWORD: ");
    	scanf("%s", pass2);
    	}
    	else
    	{
    	printf("Invalid user byebye !!\n");
    	}
    	
    	// If we have a valid username, check the password
    	if(validUser==1)
    	{
    		if(strcmp(pass1, pass2)==0)
    		{
    		puts("logging in successful");
    		}
    		else
    		{
    		puts("wrong password");
    		}
    	}
    	getchar();
    	return 0;
    }

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    The OP never said it wasn't homework.

    You didn't change the getchar() at the end nor any scanf() calls like I suggested.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    
    
    int main(void)
    {
    char name1[15], name2[15], pass1[15], pass2[15];
    
    printf("input username: ");
    scanf("%s", name1);
    printf("input password: ");
    scanf("%s", pass1);
    
    puts("registration successful");
    printf("USERNAME: ");
    scanf("%s", name2);
    if(strcmp(name1, name2)==0)
    {
    puts("good now input your password");
    }
    else
    {
    printf("badbye\n");
    return 0;
    }
    printf("PASSWORD: ");
    scanf("%s", pass2);
    if(strcmp(pass1, pass2)==0){
    puts("logging in successful");
    }
    else
    {
    puts("wrong password");
    return 0;
    }
    
    
    return 0;
    }
    i have questions why do i have to remove the & sign......and sorry i dont know how to use the fgets() function i don't understand it i want to learn the use of fgets() but im having a hard time understanding it and i dont even know how to pause at the end the... pause that i know is getch() but i want to learn the code that you gave me dwks... i already read it in the faq but can't get it right.....help me in understanding...need help....if its okey can you modify my code, change scanf() to fgets() and using the cool pause at the end....thanks

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    1. scanf takes pointers as arguments. http://c-faq.com/stdio/scanf1a.html

    2. fgets works by grabbing characters until it encounters and includes a newline character or until the buffer you supplied is filled (which is why you must pass its size), from the FILE *

    Code:
    /* For example: */
    char str[40];
    puts("Enter your name, stranger");
    fgets(str, sizeof(str), stdin);
    3. Programs only really pause for input. Use getchar or something like that to simlate a pause.

  9. #9
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    weeeeee thanks for that very well explained i really appreciate your help now i know how to use fgets. So fgets() function takes three arguments if im not mistaken first is the char * then the second is an int[number of characters] then the standard input or stdin please correct me if im wrong so i will know.....about scanf when using arrays i will not include & anymore ....thanks for that great help citizen...btw can i put anything else instead of stdin as the third argument??


    Code:
    #include <stdio.h>
    
    
    
    
    int main(void)
    {
    char name1[15], name2[15], pass1[15], pass2[15];
    
    printf("input username: ");
    fgets(name1, 15, stdin);
    printf("input password: ");
    fgets(pass1, 15, stdin);
    
    puts("registration successful");
    printf("USERNAME: ");
    fgets(name2, 15, stdin);
    if(strcmp(name1, name2)==0)
    {
    puts("good now input your password");
    }
    else
    {
    printf("badbye\n");
    return 1;
    }
    printf("PASSWORD: ");
    fgets(pass2, 15, stdin);
    if(strcmp(pass1, pass2)==0){
    puts("logging in successful");
    }
    else
    {
    puts("wrong password");
    
    }
    
    
    return 0;
    }
    another problem i really dont know how to use getchar() to pause at the end any idea or example????
    Last edited by limitmaster; 06-11-2006 at 08:18 PM.

  10. #10
    Registered User
    Join Date
    May 2006
    Location
    Da World
    Posts
    6
    Just use a getchar (); before the return statement in main()
    I think orange already showed that in code !!

    Code:
    int main()
    {
    
    /* Your code Here*/
    
    getchar();   // The program waits for a user key press before it exits
    
    return 0;
    }

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    > btw can i put anything else instead of stdin as the third argument??
    Sure you can. stdin is just a #defined FILE pointer after all; use any valid FILE pointer as the third argument. I don't think it matters for this program though.

  12. #12
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    okey understood thanks i really appreciate it......till next time...

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You'll probably want to remove the '\n' from the ends of strings read in by fgets(). You can use strchr() to do this. The FAQ probably has something.
    Just use a getchar (); before the return statement in main()
    That was what the OP had earlier, and it won't work if something is sitting in the input stream (eg, scanf() was just called). Try it. That's why I recommended
    Code:
    int c;
    while((c = getchar()) != '\n' && c != EOF);
    I even posted an FAQ link, which explains this more fully.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unable to login to facebook
    By zykostar in forum C# Programming
    Replies: 4
    Last Post: 03-30-2010, 02:04 PM
  2. Simple login program - Crashes :(
    By spadez in forum C Programming
    Replies: 1
    Last Post: 03-23-2009, 04:16 PM
  3. File Input and Output, simple.
    By Vber in forum C Programming
    Replies: 5
    Last Post: 11-17-2002, 01:57 PM
  4. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM
  5. ...deceptively simple...
    By Sebastiani in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 07-29-2002, 12:51 PM

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