Home > Blockchain >  if/else statements in for loop not working
if/else statements in for loop not working

Time:10-27

I'm trying to make a program that asks for user input and the for loop should check if the input of both user id and pin matches any of the ten pre-made account's user id and pin, like an authorization system

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

struct account{
    int uid;
    int pin;
    int user_bal;
};


int main()
{
    int scan_uid, scan_pin;
    int att = 3;
    bool loop = true;

    struct account user[10];

    user[0].uid = 1234;
    user[0].pin = 123456;

    user[1].uid = 4181;
    user[1].pin = 308592;

    user[2].uid =1111;
    user[2].pin =111111;

    user[3].uid =2222;
    user[3].pin =222222;

    user[4].uid =4444;
    user[4].pin =444444;

    user[5].uid =5555;
    user[5].pin =555555;

    user[6].uid =6666;
    user[6].pin =666666;

    user[7].uid =7777;
    user[7].pin =777777;

    user[8].uid =8888;
    user[8].pin =888888;

    user[9].uid =9999;
    user[9].pin =999999;

    for (int i; i <= 9; i  ){
        user[i].user_bal = 1000;
    }

    do{
        printf("\nEnter your user ID: ");
        scanf("%d", &scan_uid);
        printf("Enter your pin: ");
        scanf("%d", &scan_pin);
        printf("\n--------------------------------------------\n");
        att--;
        for (int i; i <= 9;   i){
            //printf("\n%d", i);
            //printf("\n%d", user[i].uid);
            //printf("\n%d", user[i].pin);
            //printf("\n%d", scan_uid);
            //printf("\n%d", scan_pin);
            if (user[i].uid == scan_uid && user[i].pin == scan_pin){
                loop = false;
            }
            else{
                printf("\nThe username or password is incorrect!");
                printf("\nYou have %d attempt(s) left.", att);
                if (att > 0)
                {
                    printf("\nPlease try again.\n");
                }
                else if (att == 0)
                {
                    printf("\nUnauthorized Access.");
                    printf("\nReport for stolen credit card uploaded.");
                }
            }
        }
    }while (att > 0 || loop == false);
    return 0;
}

I tried the relatively same code in python and it works perfectly there. I also checked if the "i" is correct and incremented and if it scanned the user input correctly. all ok. But i've hit a brick wall trying to solve why it just skips the 'if/else' and just scans input again.

I also tried an 'else if ' that does the opposite(!=) of the initial 'if' statement, with no luck.

Thanks.

CodePudding user response:

Others have already pointed out that you need to initialize int i with int i = 0 in your for loop.

However, I would additionally like to point out several other mistakes here. First, your loop condition appears to be incorrect:

while (att > 0 || loop == false)

You want to continue the loop in the case where they haven't entered the correct value yet and where they still have more attempts left. However, as written, this will continue to loop even if the user enters the correct password. I think that this should actually be

while (att > 0 && loop == true)

Also, most languages don't require you to explicitly compare to true and false, so the following is stylistically better:

while (att > 0 && loop)

Also, as written, it prompts the user to try again; however, it doesn't prompt the user for input again after the first time, so it's impossible for the user to try again. You need to prompt the user for input again inside the loop if their input was incorrect.

CodePudding user response:

A few issues ...

  1. As Dan said, initialize i to 0. This will fix the segfault.

  2. When you do loop = false; you need break; immediately afterwards.

  3. And, your do/while condition is wrong. Change while (att > 0 || loop == false); into while ((att > 0) && loop);

  4. The placement of the "incorrect passwords" related printf is incorrect. It should come after the for.

  5. Otherwise, the same error message will be repeated 9 times for a single incorrect answer.

  6. And, we want to exit the do/while loop [immediately after the for loop] if we see loop become false.


Here is the refactored code. It is annotated:

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

struct account {
    int uid;
    int pin;
    int user_bal;
};

int
main(void)
{
    int scan_uid,
     scan_pin;
    int att = 3;
    bool loop = true;

    struct account user[10];

    user[0].uid = 1234;
    user[0].pin = 123456;

    user[1].uid = 4181;
    user[1].pin = 308592;

    user[2].uid = 1111;
    user[2].pin = 111111;

    user[3].uid = 2222;
    user[3].pin = 222222;

    user[4].uid = 4444;
    user[4].pin = 444444;

    user[5].uid = 5555;
    user[5].pin = 555555;

    user[6].uid = 6666;
    user[6].pin = 666666;

    user[7].uid = 7777;
    user[7].pin = 777777;

    user[8].uid = 8888;
    user[8].pin = 888888;

    user[9].uid = 9999;
    user[9].pin = 999999;

    for (int i; i <= 9; i  ) {
        user[i].user_bal = 1000;
    }

    do {
        printf("\nEnter your user ID: ");
        scanf("%d", &scan_uid);
        printf("Enter your pin: ");
        scanf("%d", &scan_pin);
        printf("\n--------------------------------------------\n");
        att--;
// NOTE/BUG: i is uninitialized
#if 0
        for (int i; i <= 9;   i) {
#else
        for (int i = 0; i <= 9;   i) {
#endif
            // printf("\n%d", i);
            // printf("\n%d", user[i].uid);
            // printf("\n%d", user[i].pin);
            // printf("\n%d", scan_uid);
            // printf("\n%d", scan_pin);
            if (user[i].uid == scan_uid && user[i].pin == scan_pin) {
                loop = false;
// NOTE/FIX: no need to continue loop if we get a match
#if 1
                break;
#endif
            }
        }

#if 1
        // stop if everything matched
        if (! loop)
            break;
#endif

        printf("\nThe username or password is incorrect!");
        printf("\nYou have %d attempt(s) left.", att);
        if (att > 0) {
            printf("\nPlease try again.\n");
        }
        else if (att == 0) {
            printf("\nUnauthorized Access.");
            printf("\nReport for stolen credit card uploaded.");
        }
    } while (att > 0);

    return 0;
}

In the above code, I've used cpp conditionals to denote old vs. new code:

#if 0
// old code
#else
// new code
#endif

#if 1
// new code
#endif

Note: this can be cleaned up by running the file through unifdef -k

CodePudding user response:

  1. First of all, you should initialize i to 0
  2. To browse all array contents, you should increment i after the instruction NOT before (i ) instead of ( i)
  3. Condition while loop is WRONG

Look at this version of your code, It works properly:

#include <stdio.h> 
#include <string.h>
#include <stdbool.h>
struct account{
    int uid;
    int pin;
    int user_bal;
};


int main()
{
    int scan_uid, scan_pin;
    int att = 3;
    bool loop = true;

    struct account user[10] = {0};
    user[0].uid = 1234;
    user[0].pin = 123456;

    user[1].uid = 4181;
    user[1].pin = 308592;

    user[2].uid =1111;
    user[2].pin =111111;

    user[3].uid =2222;
    user[3].pin =222222;

    user[4].uid =4444;
    user[4].pin =444444;

   user[5].uid =5555;
   user[5].pin =555555;

   user[6].uid =6666;
   user[6].pin =666666;

   user[7].uid =7777;
   user[7].pin =777777;

   user[8].uid =8888;
   user[8].pin =888888;

   user[9].uid =9999;
   user[9].pin =999999;

   for (int i = 0; i <= 9; i  ){
       user[i].user_bal = 1000;
   }

   do{
       printf("\n--------------------------------------------\n");
       printf("\nEnter your user ID: ");
       scanf("%d", &scan_uid);
       printf("Enter your pin: ");
       scanf("%d", &scan_pin);
       printf("\n--------------------------------------------\n");
       att--;
       for (int i = 0; i <= 9; i  ){
           //printf("\n%d", i);
           //printf("\n%d", user[i].uid);
           //printf("\n%d", user[i].pin);
           //printf("\n%d", scan_uid);
           //printf("\n%d", scan_pin);
           if (user[i].uid == scan_uid && user[i].pin == scan_pin)
           {
               loop = false;
               break;
           }
    }
    if (loop && att)
    {
        printf("\nThe username or password is incorrect!");
        printf("\nYou have %d attempt(s) left.", att);
    }
    else if (! att && loop)
    {
        printf("\nUnauthorized Access.");
        printf("\nReport for stolen credit card uploaded.\n");
    }
    else if (!loop)
    {
        printf("\nMatches !\n");
    }
} while ((att > 0) & (loop == true));

return 0;
}
  •  Tags:  
  • c
  • Related