Problem I can't seem to solve

This is a discussion on Problem I can't seem to solve within the C Programming forums, part of the General Programming Boards category; Hello all I've got an assignment to make a quiz. First of all one must be able to create users ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    3

    Problem I can't seem to solve

    Hello all

    I've got an assignment to make a quiz.
    First of all one must be able to create users with a passwords
    when the program starts, it ask if you want to create a user/pass or login


    creating I've done... it writes all the users to a file
    both username and password are max 15 long...
    they are written to a binary file :

    ----15 for username----- 15 for pasword----- (without the trails.. it's just white spaces)


    now when I want to test my login. i've got a problem:

    I first ask the user to enter pass en login
    I save this but the watch of my compiler tells me it just saved what I write and not the 15 long

    so during the stringcompare it's logical it doesn't compare:

    more info on the script:

    let's imagine i enter user1 and pass1
    and that inside the file it says user1 and pass1:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>
    
    
    int login(void){
    
    char user[15];
    char pass[15];
    char temp_user[17];
    char temp_pass[17];
    int sw_ok=1;
    FILE *login;
    
    clrscr();
    printf("\t**********************************************\n");
    printf("\t*                  Login                     *\n");
    printf("\t**********************************************\n");
    
    while (sw_ok)
    {
    login=fopen("users.dat", "rb");  
    printf("username: ");
    scanf("%15s", user); //according to watch it store "user1"
    printf("Paswoord: ");
    scanf("%15s", pass);  //according to watch it stores "pass1"
    
    
    while(!feof(login))
    {
    	 fgets(temp_user,sizeof(user)+1,login); //gets "          user1"
    	 fgets(temp_pass,sizeof(pass)+1,login); //gets "         pass1"
    	
    
    		if (strcmp(temp_user, user) == 0 && strcmp(temp_pass, pass) ==0) {
    			printf("\nAccess OK\n");
    			sw_ok=0;
    			}
    		else printf("\incorrect\n");
    
    }
    
    
    
    }
    printf("Tataaaa");
    return 0;
    }


    thanks for some feedback...
    Last edited by psykik; 05-15-2005 at 04:19 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    FAQ > Explanations of... > Why it's bad to use feof() to control a loop

    Why binary mode for strings? If you really need binary mode, shouldn't you use fread instead of fgets?

    Do you really need clrscr and conio.h?

    [edit]
    Code:
    fgets(temp_user,sizeof(user)+1,login);
    Read one less than the string can hold (even when accounting for the null character), but disguise it to look like it's reading one more than it should?

    Kinda the same thing here:
    Code:
    scanf("%15s", user);
    What's \i?
    Code:
    else printf("\incorrect\n");
    Is login a FILE* or a function?
    Code:
    int login(void){
    
    char user[15];
    char pass[15];
    char temp_user[17];
    char temp_pass[17];
    int sw_ok=1;
    FILE *login;
    Last edited by Dave_Sinkula; 05-15-2005 at 05:09 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Hey Dave,
    He's probably using clrscr() so that when he calls this program from cmd.exe or any other command line app all of the garbage above will be removed so that the program will start out with a fresh screen, it all comes down to aesthetics.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Kleid-0
    Hey Dave,
    He's probably using clrscr() so that when he calls this program from cmd.exe or any other command line app all of the garbage above will be removed so that the program will start out with a fresh screen, it all comes down to aesthetics.
    I'm aware of what it does. I'm also aware of senior programmers who could detail why doing so is "unnecessary and anti-social".
    Last edited by Dave_Sinkula; 05-15-2005 at 08:23 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    What!? C'mon Dave, that's just silly.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    3
    Quote Originally Posted by Dave_Sinkula
    Why binary mode for strings? If you really need binary mode, shouldn't you use fread instead of fgets?
    I use binary because a txt file would enable users to go look into the txt file. Ok one could say they just have to open the file in notepad to see it too.... but not everyone knows that. + I still need to find a way to "encrypt" the writing to a file.


    thanks for the feedback, I'll change the loop with feof like the tutorial explains...

    here are the answers to your questions:

    Quote Originally Posted by Dave_Sinkula
    Do you really need clrscr and conio.h?
    yes I do... before entering this function, the screen needs to be cleared because they already got an intro function with a header, menu ...
    anyway this is just esthetics...

    Quote Originally Posted by Dave_Sinkula
    [edit]
    Code:
    fgets(temp_user,sizeof(user)+1,login);
    Read one less than the string can hold (even when accounting for the null character), but disguise it to look like it's reading one more than it should?
    well this is odd I must admit... but without the +1 the watch tells me it reads in "[blank spaces]... user" (it omits the last character.. here a 1 )
    by doing the +1 it reads the info correctly...: "[blankspaces]....user1"




    Quote Originally Posted by Dave_Sinkula
    Kinda the same thing here:
    Code:
    scanf("%15s", user);
    I put this because I made a limit that max. 15 characters are to be used for login and password...

    + I was hoping by putting this like that... when a user enters for instance user1

    it would store it like this " user1"

    thus the strcmp between this and the sequence read out of the file would be equal
    and thus result in "login ok"



    Quote Originally Posted by Dave_Sinkula
    What's \i?
    Code:
    else printf("\incorrect\n");
    sorry typo after having paste it into this box


    Quote Originally Posted by Dave_Sinkula
    Is login a FILE* or a function?
    Code:
    int login(void){
    
    char user[15];
    char pass[15];
    char temp_user[17];
    char temp_pass[17];
    int sw_ok=1;
    FILE *login;


    good point...
    I renamed the function to "inloggen" (dutch)

    this function is called out from the main function..



    thanks for the feedback...

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by psykik
    I put this because I made a limit that max. 15 characters are to be used for login and password...
    And this doesn't account the null character.

    Food for thought...
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *foo(char *line, int size, const char *prompt)
    {
       fputs(prompt, stdout);
       fflush(stdout);
       if ( fgets(line, size, stdin) )
       {
          char *newline = strchr(line, '\n');
          if ( newline != NULL )
          {
             *newline = '\0';
          }
       }
       return line;
    }
    
    int main(void)
    {
       char file_user[15] = "         user1";
       char file_pass[15] = "         pass1";
       char user [ sizeof file_user + 1/* expect trailing newline with fgets*/ ];
       char pass [ sizeof file_pass + 1/* expect trailing newline with fgets*/ ];
       static const char filename[] = "users.dat";
       FILE *file = fopen(filename, "wb");
       if ( file )
       {
          fwrite(file_user, sizeof file_user, 1, file);
          fwrite(file_pass, sizeof file_pass, 1, file);
          fclose(file);
       }
       foo(user, sizeof user, "username: ");
       foo(pass, sizeof pass, "password: ");
       file = fopen(filename, "rb");
       if ( file )
       {
          while ( fread(file_user, sizeof file_user, 1, file) == 1 &&
                  fread(file_pass, sizeof file_pass, 1, file) == 1 )
          {
             if ( strcmp(file_user, user) == 0 && strcmp(file_pass, pass) == 0 )
             {
                puts("Access OK");
                goto done;
             }
          }
          puts("Invalid Access");
          done:
          fclose(file);
       }
       return 0;
    }
    
    /* my output
    username: user1
    password: pass1
    Invalid Access
    
    username:          user1
    password:          pass1
    Access OK
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    May 2005
    Posts
    3
    thanks for the code and alternative way...

    I've been looking into how to do it also
    and came up with this:


    (sorry if the code is lame... I'm still quite new at C)
    also the printf's are in dutch...



    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int inloggen(void){
    
    char user[15];
    char pass[15];
    char **temp_user,**temp_pass,lijn[31],c='o'; //15+15+1
    //int's voor de lussen...
    int sw_ok=1, sw_user=1, sw_pass=1,sw_gevonden=0,lengte=0,i=0;
    FILE *login;
    
    
    printf("\t**********************************************\n");
    printf("\t*                  Login                     *\n");
    printf("\t**********************************************\n");
    
    login=fopen("users.dat", "rb");
    fscanf(login,"%*s");
    	 while(!feof(login))
    	 {	  lengte++;
    		  fscanf(login,"%*s");
    	 }
    	 rewind(login);
    	 temp_user=calloc(sizeof(char*),lengte);
    	 temp_pass=calloc(sizeof(char*),lengte);
    	 for(i=0;i<lengte;i++)
    	 {   fscanf(login,"%s",lijn);
    		  temp_user[i]=malloc(16);
    		  strncpy(temp_user[i],lijn,15);
    		  fscanf(login,"%s",lijn);
    		  temp_pass[i]=malloc(16);
    		  strncpy(temp_pass[i],lijn,15);
    	 }
    	 fclose(login);
    
    while (sw_ok)
    {
    	while(sw_user)
    	{
    		printf("Gebruikersnaam: ");
    		scanf("%15s%c", user,&c);
    		if(c!='\n')
    		{
    			printf("Deze gebruikersnaam is te lang. U kan enkel een gebruikersnaam gebruiken van 15 karakters.\n");
    			scanf("%*[^\n]%*c");
    		}
    		else sw_user=0;
    	}
    
    	while(sw_pass)
    	{
    		printf("Geef uw paswoord: ");
    		scanf("%15s%c",pass,&c);
    		if(c!='\n')
    		{
    			printf("Foutief paswoord...(te lang, max 15 karakters)\n");
    			scanf("%*[^\n]%*c");
    		}
    		else sw_pass=0;
    	}
    
    
    for(i=0;i<lengte;i++){
    	if (strcmp(temp_user[i], user) == 0 && strcmp(temp_pass[i], pass) ==0){
    							sw_ok=0;
    							sw_gevonden=1;
    				}
    				else  {
    					sw_user=1;
    					sw_pass=1;
    					}
    	 }//einde for lus
    if (sw_gevonden)
    {
    printf("***********************\n");
    printf("*     Toegang OK      *\n");
    printf("***********************\n");
    menu();
    }
    else printf("Foute gebruikersnaam of paswoord\nProbeer opnieuw AUB\n");
    }
    	return 0;
    }

  10. #10
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Dave!? First you say clrscr() is antisocial, and now you use goto. Are you going through tough times?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's a difference: goto is standard.

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

  12. #12
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Well I think goto is antisocial.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Goto is anything but antisocial. It's always going places.

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

  14. #14
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    omg I'm going insane, AAHH!!!

  15. #15
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    *bump*

    I believe this works well without a goto:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <assert.h>
    
    char *getLine( char *l, int size, const char *prompt )
    {
    	int i, c;
    
    	printf( "%s ", prompt );
    	for( i=0; i<size-1 && ( c=getc( stdin ) )!=EOF && c!='\n'; ++i )
    		l[i]=c;
    	l[i]='\0';
    	
    	return l;
    }
    
    
    int main( void )
    {
    	char file_user[] = "birds";
    	char file_pass[] = "sing";
    	char user [ sizeof( file_user ) + 1 ];
    	char pass [ sizeof( file_pass ) + 1 ];
    	static const char filename[] = "users.dat";
    	FILE *file = fopen( filename, "w" );
    	
    	/* Write the correct username/password to the file */
    	assert( file );
    	if( file )
    	{
    		fwrite( file_user, sizeof( file_user ), 1, file );
    		fwrite( file_pass, sizeof( file_pass ), 1, file );
    		fclose( file );
    		file = NULL;
    	}
    	
    	/* Get a username/password from the user */
    	getLine( user, sizeof( user ), "username: " );
    	getLine( pass, sizeof( pass ), "password: " );
    	
    	/* Prepare to read the file */
    	file = fopen( filename, "r" );
    	assert( file );
    	
    	/* Get the correct username/pass from file */
    	if( !feof( file ) && !ferror( file ) )
    		fread( file_user, sizeof( file_user ), 1, file );
    	if( !feof( file ) && !ferror( file ) )
    		fread( file_pass, sizeof( file_pass ), 1, file );
    		
    	/* Make sure the username/pass are correct */
    	if( !strcmp( file_user, user ) && !strcmp( file_pass, pass ) )
    		puts( "Access OK" );
    	else
    		puts( "Invalid Access" );
    
    	/* Close the stream */
    	fclose( file );
    	
    	/* Return successful */
    	return 0;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Solve This Problem Within 3 Hr Urgent Need
    By annum in forum C Programming
    Replies: 12
    Last Post: 10-04-2009, 10:56 AM
  2. problem solve
    By coolnarugodas in forum C Programming
    Replies: 7
    Last Post: 04-26-2005, 01:31 PM
  3. Replies: 2
    Last Post: 04-25-2005, 12:59 PM
  4. Bubble Sort / selection problem
    By Drew in forum C++ Programming
    Replies: 7
    Last Post: 08-26-2003, 04:23 PM
  5. problem cant solve please help.
    By sarah in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 02:32 PM

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