0

What am I doing wrong? My code keeps in loop and n goes minus. It was supposed to return 0; at 0.Also whatever I do it starts with 3-2-1-0 even I type "2" it still keeps doing it 3-2-1-0

 #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
        static const char PSWRD[]="1234";
        char p[6];
        int n=3, y;
    
        printf("Hos geldiniz");
    
    
        do{
            printf("\n\nOgrenci_ID:Elif");
            fflush(stdout);
    
            printf("\nSifre:");
            scanf("%s", &p);
            fflush(stdout);
    
           
            y=strcmp(p, PSWRD);
    
            if(y==0){
                printf("\nGiris Basarili"); `//succesfull login`
                return 0;
            }else {
                printf("Yanlis Sifre, tekrar deneyiniz", 3-n); //wrong password try again
                printf("\nKalan hakkiniz ");
                printf("%d\n", n);
                getchar();
                n--;}
    
            if(n<1){
                printf("\nHesabiniz bloke oldu"); 
                    return 0;


// that means you use all your chance and now you're blocked but my code aint stop here and n goes minus
            }
    // I am not exactly sure about "3" 
    //Also what ever i do it starts with 3-2-1-0 even i type "2" it's still keep doing it 3-2-1-0
        }while (n<=3);
        return 0;
    }
Muzaffer D
  • 43
  • 7
  • Please make a responsive question, and use only english language! Look at your printfs! And you have some erros, on printfs, like on your 5 printf you dont has a %d to show the `3-n` – Gonçalo Bastos Nov 18 '20 at 22:23
  • If your while loop only runs if `n` is less than or equal to 3, and you're only ever decreasing `n` after starting it at 3, then the while loop will always run. It's not clear when you actually want it to stop running. – Random Davis Nov 18 '20 at 22:25
  • @Muzaffer D I has edited your code and posted it below! Its working for me! You has some errors, like in scanf and so, I fix that errors – Gonçalo Bastos Nov 18 '20 at 22:46

4 Answers4

2
while (n<=3);

doesn't agree with

n--;

You seem to want

while (n>0);
Joshua
  • 40,822
  • 8
  • 72
  • 132
0

In your code, there is no increment of 'n', so the loop keeps going (because n is always smaller than 3). I'm not too sure of what you're trying to do, but you need to change your 'while' condition or statements of 'n' inside the loop.

Currently the loop keeps running forever: When n=3 - > 3<=3 is true. When n=2 - > 2<=3 is true. Etc.

The only way it's going to end is when n decrements until it is equal to the absolute minimum value of an integer which is -2,147,483,648, then it will decrement one more time and change to 2,147,483,647 and the loop will end.

Ido Nir
  • 33
  • 4
0

Its working for me!

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
    {
        static const char PSWRD[]="1234";
        char p[6];
        int n=3, y;
    
        printf("Welcome");
    
    
        do{
            printf("\n\nStudent_ID:Elif");
            fflush(stdout);
    
            printf("\nPassword:");
            scanf("%s", p);
            fflush(stdout);
    
           
            y=strcmp(p, PSWRD);
    
            if(y==0){
                printf("\nSucessfull Login\n"); //succesfull login
                return 0;
            }else{
                n--;
                    printf("\nWrong password, try again: "); //wrong password try again
                    printf("\nRemaining attempts  ");
                    printf("%d\n", n);
                    getchar();
            }
    
            if(n<1){
                printf("\nYour account has been blocked\n"); 
                    return 0;
            }
        }while (n>0);
}

When user introduces wrong password, you have more 2 tries and if you enter the password wrong on thats 2 tries program ends! But if you insert it right program makes login, so works fine

Gonçalo Bastos
  • 376
  • 3
  • 16
  • Thanks, a lot, yet i am not exactly sure which parts of my codes were wrong. And how can I prevent it – Muzaffer D Nov 18 '20 at 22:54
  • @MuzafferD Prevent from what? I fixed your scanf, because you already has an array and you dont need to make `scanf("%s", &p);` with the `&`, and fixed the printf that you have on `n-3`, but you dont have any `%d`. I moved on the `n--;` because your printf of attemps was printing wrong info about attemps! And the while! – Gonçalo Bastos Nov 18 '20 at 23:00
0

Use printf to watch the value of n, and you will quickly observe that your condition for the do...while loop is incorrect.

However, note the conditional, if(n<=0) with return.

Provide a minimal reproducible example, such as below...

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

int login(char* PSWRD)
{
    int n=3, y;
    char p[100+20];

    printf("Hos geldiniz");

    do{
        printf("\nSifre:");
        scanf("%100s", p); // limit input size, use p, not &p

        if( 0 == strcmp(p, PSWRD) ) {
            printf("\nsuccess!");
            return 0;
        }

        printf("Yanlis Sifre, tekrar deneyiniz"); //wrong password try again
        n--;

        if(n<=0)
        {
            printf("\nHesabiniz bloke oldu"); 
            return -1;
        }
        printf("n=%d\n",n); // print current n
    } while (n<=3); // this should be (n>0)

    return -1;
}

int main()
{
    char* PASSWD = "password";
    int result;
    if( 0 > login(PASSWD) ) {
        printf("\nfailed!\n");
        exit(1);
    }
    printf("\nsuccess!\n");
    // continue processing here
}

Caveat: Make sure you limit input size to avoid buffer overflow Read no more than size of string with scanf()

ChuckCottrill
  • 4,360
  • 2
  • 24
  • 42