Home > Software design >  How can i make this loop go on for 3 tries and function correctly
How can i make this loop go on for 3 tries and function correctly

Time:08-03

I have been learning to code for a few days and today I decided to use my limited capabilities to try to code this. I am trying to make a loop for a password input where if the password entered is correct, the loop will end. And if password is incorrect it will give you two more tries to input it correctly. I have tried writing a while loop like shown below. However, this not only did not fix the problem it also ruined the previous somewhat functional loop. Now it just accepts any input forever without giving anything back.

If you have any fixes and tips please share them! Many thanks!

#include <stdio.h>
#include <cs50.h>

int main(void)
{
    long password;
    int ask;
    int tries = 0;
    int keepgoing = true;
    ask = get_int("Enter Password: ");
    password = 123456789;
    while (tries < 3 && keepgoing == true);
    if (ask == password)
    {
        printf("Correct!!!\n");
        keepgoing = false;
    }
    if (ask != password)
    {
        printf("Incorrect, Try Again!!!\n");
    }
    if (tries == 3)
    {
        printf("Too many wrong attempts\n");
        keepgoing = false;
    }
}

CodePudding user response:

I don't happen to have the CS50 library and its functions installed on my system; however, the following code example should illustrate one way to approach this issue. Following is an example of how to repeatedly prompt for a password until either the correct password is entered or the number of tries is exceeded.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    long password = 123456789;
    int ask;
    int tries = 0;

    while (1)    /* This is equivalent to "while(true)" */
    {
        // ask = get_int("Enter Password: ");   /* This statement would be used with the CS50 include and library */

        printf("Enter Password: ");             /* Equivalent to ask = get_int("Enter Password: ");*/
        scanf("%d", &ask);                      /* Equivalent to ask = get_int("Enter Password: ");*/

        if (ask == password)
        {
            printf("Correct!!!\n");
            break;                              /* Just using break to exit the "while" loop */
        }

        tries  = 1;

        if (tries == 3)
        {
            printf("Too many wrong attempts\n");
            break;
        }

        printf("Incorrect, Try Again!!!\n");
    }

    return 0;
}

The bits to note are:

  • The prompting for a password was moved inside of the while loop, so if need be, the prompt for a password is repeated.
  • If either the correct password is used or the number of tries has been exceeded, a notification is printed and the "break" statement exits the "while" loop. This simplifies using the "while" loop.

Anyway, give that a try. Since you do have the CS50 library, you can swap out the "ask = get_int("Enter Password:" );" statement for the generic "printf" and "scanf" statements I used.

CodePudding user response:

There's no need for so flags and extra code.

This for() loop will terminate when 3 tries are exhausted, or the user has entered the correct value.

long pswd = 123456;

for( int tries = 3; tries && get_int("Enter password") != pswd; tries-- )
    printf( "Incorrect\n" );

if( tries == 0 )
    printf( "Failed\n" );
else
    printf( "Welcome\n" );

CodePudding user response:

A little thing you forgot is "else if". By adding this, you code will loop through each of the condition as if they were a whole. Therefore, your code will stop running when a condition is met. Also, your final if statement is useless as you while loop will expire once tries = 3. Another little thing you forgot is "tries " to increment your tries each time. Finally, you need to put your if statements inside the while loop by using curly brackets. Here is the correct code:

while (tries < 3 && keepgoing == true) 
{
    if (ask == password)
    {
        printf("Correct!!!\n");
        keepgoing = false;
    }
    else
    {
        printf("Incorrect, Try Again!!!\n");
        ask = get_int("Enter Password: ");
        tries  ;
    }
}

if (tries == 3)
    {
        printf("Too many wrong attempts\n");
    }
  • Related