0

So the code runs and for the most part, does what I want it to do. The only issue is that when the password equals 1000 The print statement "Trying password: 1000" does not print before the statement that the computer has cracked the password which I know results from the fact that because they are equal to each other the code skips to the else if statement. Is there a way to get it so that the "Trying password:1000" prints before it prints that it cracked the password?

#include <stdio.h>
#include <stdbool.h>

int password;
int attack = 1000;
int num = 1;
bool repeat = true;

int main()
{
    printf("Please enter a 4 digit password:\n");
    scanf("%4d", &password);
    while (repeat)
    {
        if (password < 1000 || password > 9999)
        {
            printf("Please enter a 4 digit password.\n");
            scanf("%4d", &password);
        }
        if (attack != password)
        {
            printf("Trying password: %4d\n", attack++);
            num=num+1;
        }
        else if (attack == password)
        {
            printf("Computer cracked the password in %4d tries\n", num);
            repeat = false;
        }
   }    
   return 0;
}
Jesse Hix
  • 83
  • 5
  • Move `printf("Computer cracked the password in %4d tries\n", num);` to after the `while` loop ends - right before `return 0;` – 001 Feb 08 '21 at 17:22
  • I just tried that and it still did not print out "Trying password: 1000." Would moving the entire else if statement outside work and if so where would I put the repeat = false to end the loop? – Jesse Hix Feb 08 '21 at 17:26
  • Also remove `if (attack != password) {}` (but not what is inside the brackets) so it executes that code every time. – 001 Feb 08 '21 at 17:29
  • Do you consider `0123` a four-digit password? Your code will treat it as a three-digit password. – Andrew Henle Feb 08 '21 at 17:29
  • Please [edit] your question and show some example input, the corresponding output you get and the expected output. If you enter the correct passowrd `1000`, do you want both a message `Trying password: XXXX` and a message `Computer cracked the password in NNNN tries`? Please clarify this in the question if it is not already clear from example input and output, – Bodo Feb 08 '21 at 17:30

1 Answers1

0

The problem is attack already starts with the value 1000, so the check else if (attack == password) is immediately true and your statement printing out the attempted attack value is never even accessed.

You should move the statement

printf("Trying password: %4d\n", attack);

To the start of your while loop, and put attack++ at the end.

Also: see this question to understand why you should always check the return value of scanf. I mean, if you have your heart set on using scanf. fgets is almost always a better choice.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
  • Thank you I ended up using your suggested statement and putting a nested if statement before the code prints computer cracked the password and that solved the issue. else if (attack == password) { if (password == 1000) printf("Trying password: %4d\n", attack); printf("Computer cracked the password in %d tries\n", num); repeat = false; } – Jesse Hix Feb 08 '21 at 17:33
  • @JesseHix If it worked, feel free to mark my answer as 'accepted' by clicking the checkmark icon near the voting arrows. – Govind Parmar Feb 08 '21 at 17:34