Thread: Check password with 3 attempts, password is automatically generated

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    1

    Check password with 3 attempts, password is automatically generated

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #include <math.h>
    
    
    void genpass(int, int);
    void check(int, int, int);
    int main ()
    {
    srand(time(NULL));
    
    
    int i, c = 0, password, EnteredPassword;
    
    
    genpass(i, password);
    check(c, EnteredPassword, password);
    
    
    return 0;
    
    
    
    
    }
    
    
    
    
    void genpass (int i, int password)
    {
    	FILE *pf;
    	pf = fopen("sifra.txt", "w");
    	fprintf(pf, "");
    	for (i=0;i<4;i++){
    		password = ( rand()%10 );
    		fprintf(pf, "%d", password);
    	}
    
    
    	
    	printf("\n New password has been generated \n");
    	fclose(pf);
    	
    }
    
    
    void check(int c, int EnteredPassword, int password)
    {
    
    
    
    
        printf("\n Enter your password: ");
          
        scanf("%d", &EnteredPassword);
          
        printf("\n You entered %d password \n", EnteredPassword);
          
        if(password == EnteredPassword)
        {
            printf("\n Successfully logged in \n");
      
      	}
          
        else
        {
            printf("\n Password incorrect \n Please try again \n");
             
            for ( c = 0; c < 3; c++)
                 
                {
                     printf("\n Enter your password: ");
          
                     scanf("%d", &EnteredPassword );
          
          
                      if(password == EnteredPassword)
                       {
                            printf("\n Successfully logged in \n");
                            break;
                       }
                     
                }
        }
    
    
    }

    Here is what I need to do:
    I need to make a program that's gonna generate 4 random digits ( code ). that part is easy. after that we are putting that code in password.txt file. easy. and then.. if you enter wrong code 3 times, that code from password.txt will delete and you need to write how many times content of password.txt has been deleted. after that you need to make UI where you can generate new code, and to check if you need to generate new code or if it's not yet deleted, how many times you can type it wrong. If you are left with 1 attempt, program will alert you that you only have 1 more attempt. if you get the code right, program will tell you that you now have access to something

    And.. I'm already stuck on part where I need to check password. If I enter password from .txt document it will just tell me that it's not correct. What's wrong?




  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    First I would suggest you simplify the code you have - by making all those parameters not actually used for passing parameters into local variables.

    Code:
    void genpass (int i, int password)
    {
        FILE *pf;
    You made pf a local, why not i and password as well?


    Next, you need to add code to read your file.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-21-2020, 11:45 PM
  2. Replies: 4
    Last Post: 03-26-2016, 07:09 AM
  3. Password check with 3 attempts (split)
    By Shedrack Ayaro in forum C++ Programming
    Replies: 1
    Last Post: 02-24-2015, 01:15 PM
  4. Replies: 1
    Last Post: 06-29-2014, 02:08 AM
  5. Password check with 3 attempts
    By samwillc in forum C++ Programming
    Replies: 6
    Last Post: 06-09-2012, 03:54 AM

Tags for this Thread