Thread: How to give three attempt's to entered password

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    81

    How to give three attempt's to entered password

    I wrote program to match password it works. I want to improve it. When user enter wrong password just give him three attempt's. I am stuck I don't have idea how to do it.

    Code:
    #include<stdio.h>
    
    int main (void)
    
    
    {
    	int password = 123, EnteredPassowrd;
    	
    	printf("Please Enter Password : ");
    	
    	scanf("%d", &EnteredPassowrd );
    	
    	printf("Number enterd by you : %d \n", EnteredPassowrd);
    	
    	if(password == EnteredPassowrd)
    	{
    		printf("log in successful \n");
    
    
    	}
    	
    	else
    	{
    		printf("log in faild \n");
    	}
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Learn about loops For, While and Do While Loops in C - Cprogramming.com

    And, give it a try.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by stahta01 View Post
    Learn about loops For, While and Do While Loops in C - Cprogramming.com

    And, give it a try.

    Tim S.
    I have given my best try. I understand while and for loop. I am having problem to apply in program. It will appreciate if you can give some clues

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps post your best try.
    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.

  5. #5
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    Perhaps post your best try.
    I am thinking in this way but it's not what I want to do

    Code:
    #include<stdio.h> 
    int main (void)
     
     
    {
        int password = 123, EnteredPassowrd;
    	
    	int i = 0;
         
        printf("Please Enter Password : ");
         
        scanf("%d", &EnteredPassowrd );
         
        printf("Number enterd by you : %d \n", EnteredPassowrd);
         
        if(password == EnteredPassowrd)
        {
            printf("log in successful \n");
     
     
        }
         
        else
        {
            printf("log in faild \n Please try again \n");
    		
    		for ( i = 0; i < 3; i++)
    			
    			{
    				 printf("Please Enter Password : ");
         
                     scanf("%d", &EnteredPassowrd );
         
         
                      if(password == EnteredPassowrd)
                       {
                            printf("log in successful \n");
    				   }
    				
    			}
        }
        return 0;
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Change this
    Code:
                      if(password == EnteredPassowrd)
                       {
                            printf("log in successful \n");
                       }
    To this
    Code:
                      if(password == EnteredPassowrd)
                       {
                            printf("log in successful \n");
                            break;
                       }
    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.

  7. #7
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    Change this

    Code:
                      if(password == EnteredPassowrd)
                       {
                            printf("log in successful \n");
                            break;
                       }
    Thanks Salem. It works but Is it possible without using break statement.

    I think it can be using while loop. I understand how while loop work But I don't understand where to use it program

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Sure, you can make your conditional something like
    Code:
    int passwordCorrect = 0;
    for ( i = 0; i < 3 && !passwordCorrect ; i++)
    Now just set the boolean at the appropriate moment in the code.
    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.

  9. #9
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    Sure, you can make your conditional something like
    Code:
    int passwordCorrect = 0;
    for ( i = 0; i < 3 && !passwordCorrect ; i++)
    Thanks a lot for suggestion

    Now just set the boolean at the appropriate moment in the code.
    Please look at my flow chart

    I made flow chart for program. If you see there, when false condition happen program start form beginning. how to repeat this process in three time if condition is false
    Attached Images Attached Images How to give three attempt's to entered password-program-jpg 

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    do while loop
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  11. #11
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by stahta01 View Post
    do while loop
    Basically I was trying to implement flow chart into program

    this isn't my program suppose to do

    Code:
    #include<stdio.h> int main (void)
      
      
    {
        int password = 123, EnteredPassowrd;
         
        int i = 0;
          
        printf("Please Enter Password : ");
          
        scanf("%d", &EnteredPassowrd );
          
        printf("Number enterd by you : %d \n", EnteredPassowrd);
          
        do 
        {
            if(password == EnteredPassowrd)
    		{
    		
    		  printf("log in successful \n");
    		 
    		  printf("log in faild \n Please try again \n");
    		}         
            else 
    			
    			{
    				
    				
    				for ( i = 0; i < 3; i++)
                 
                    {
                        printf("Please Enter Password : ");
          
                        scanf("%d", &EnteredPassowrd );
          
          
                        if(password == EnteredPassowrd)
                           {
                              printf("log in successful \n");
                           }
    				}
                }
        }
      
          
       while (password != EnteredPassowrd);
        
        
        return 0;
    }

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    OK, so where do you see a for loop in your flow chart?

    Also, where on your flowchart is 'number of attempts'?

    I suggest you start with an empty shell of a program just containing comments taken directly from your flowchart.
    Code:
    do {
    // prompt user to enter password
    // get password
    // check password
    
    // true: success
    // false: print error message and try again
    } while ( 0 );
    This compiles, but it will only run the code once.
    Your job is to change the condition in the while to be what you want.


    You basically had the first three comments sorted in your first post.
    That is a good starting point, so just forget about all that for loop stuff and concentrate on putting a while loop around your original code.
    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.

  13. #13
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    OK, so where do you see a for loop in your flow chart?

    Also, where on your flowchart is 'number of attempts'?

    I suggest you start with an empty shell of a program just containing comments taken directly from your flowchart.
    Code:
    do {
    // prompt user to enter password
    // get password
    // check password
    
    // true: success
    // false: print error message and try again
    } while ( 0 );
    I don't understand how to convert this statement into program

    Code:
    // false: print error message and try again
    } while ( 0 );

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Change the "0" into an Boolean expression.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  15. #15
    Registered User
    Join Date
    Jul 2018
    Posts
    16
    just in case you still need help here is the c++ solution using a while loop. C++ and C are similar so it may help you find a solution. I would have done it in C but i thought this would be a more helpful way to learn. Or at least see the structure of the program.

    Code:
    #include <iostream>#include <string>
    
    
    int main()
    {
        //keep track of while loop
        bool correct = true;
        //Password user must enter to unlock
        int password = 123;
        //password user is entering
        int enteredPassword;
        //number of times user can enter password
        int numTries = 0;
        //continue asking until correct or 3 limit reached. 
        while(correct == true)
        {
            std::cout << "Password Checker" << std::endl;
            std::cout << "Please enter your password." << std::endl;
            std::cin >> enteredPassword;
            if(enteredPassword == password)
            {
                std::cout << "Correct password!" << std::endl;
                correct = false;
            }
            else
            {
                std::cout << "Wrong password!" << std::endl;
                numTries++;
                std::cout << "Attempted ";
                std::cout << numTries;
                std::cout << " out of 3." << std::endl;
                if(numTries == 3)
                {
                    std::cout << "Too many password attempted!" << std::endl;
                    std::cout << "Exiting program" << std:endl;
                    correct = false;
                }
            }
            
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-29-2014, 02:08 AM
  2. Replies: 2
    Last Post: 01-07-2009, 10:35 AM
  3. Replies: 1
    Last Post: 06-29-2004, 05:23 PM
  4. Replies: 3
    Last Post: 12-15-2001, 01:46 PM
  5. Replies: 1
    Last Post: 09-01-2001, 10:33 AM

Tags for this Thread