Thread: Break from a for loop with a flag. - Game get at least 3 numbers from 6

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    61

    Break from a for loop with a flag. - Game get at least 3 numbers from 6

    I donīt understand why my program fail, when i try to jump out of a loop using a flag.

    Code:
            int flag = 0;
    
    
            for (int i = 0; i < KEY_SIZE; i++) {
                for (int j = 0; j < KEY_SIZE || flag == 1; j++) {
                    if (key[i] == keyinput[j]) {
                        jackpot++;
                        flag = 1;
                    }
                }
                flag = 0;
            }
    If i do with a break everything works ok


    Code:
            for (int i = 0; i < KEY_SIZE; i++) {
                for (int j = 0; j < KEY_SIZE; j++) {
                    if (key[i] == keyinput[j]) {
                        jackpot++;
                        break;
                    }
                }
            }




    The full code is here.

    If you got more then 2 numbers on a key you are a winner.

    Text file(keys.txt)
    6 7 8 9 40 45
    8 9 33 34 36 41
    1 2 3 4 5 8



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 100
    #define KEY_SIZE 6
    
    
    int main(int argc, char** argv) {
    
    
        FILE *pFile;
    
    
        int keyinput[KEY_SIZE] = {2, 3, 4, 5, 6, 7};
        int key[KEY_SIZE];
        char buffer[SIZE];
        int jackpot = 0;
    
    
        if ((pFile = fopen(argv[1], "r")) == NULL) {
            return -1;
        }
    
    
        while (fgets(buffer, sizeof (buffer), pFile)) {
            sscanf(buffer, "%d %d %d %d %d %d", &key[0], &key[1], &key[2], &key[3], &key[4], &key[5]);
    
    
            int flag = 0;
    
    
            for (int i = 0; i < KEY_SIZE; i++) {
                for (int j = 0; j < KEY_SIZE; j++) {
                    if (key[i] == keyinput[j]) {
                        jackpot++;
                        break;
                    }
                }
                flag = 0;
            }
    
    
            if (jackpot >= 3) {
                puts("That key is a winner");
            } else {
                jackpot = 0;
            }
        }
    
    
        return (EXIT_SUCCESS);
    }

  2. #2
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    I think you can use the for loop to break as you want if the middle part is like this:

    j < KEY_SIZE && flag == 0

    I could be wrong but that seems right to me.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    61
    Quote Originally Posted by BpB View Post
    I think you can use the for loop to break as you want if the middle part is like this:

    j < KEY_SIZE && flag == 0

    I could be wrong but that seems right to me.
    Yes i was messing with || and &&
    Code:
     for (int j = 0; j < KEY_SIZE || flag == 0; j++)
    it works

    Code:
       int flag = 0;
    
        for (int i = 0; i < KEY_SIZE; i++) {
                for (int j = 0; j < KEY_SIZE && flag == 0; j++) {
                     if (key[i] == keyinput[j]) {
                        jackpot++;
                        flag = 1;
                    }
                }
                flag = 0;
            }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to implement a flag control in my loop?
    By tmac619619 in forum C Programming
    Replies: 2
    Last Post: 11-04-2012, 02:37 PM
  2. Can anyone help me to break numbers into an array numbers
    By s2xcracker in forum C++ Programming
    Replies: 4
    Last Post: 08-25-2009, 05:17 PM
  3. how could I break this loop?
    By Trafalgar Law in forum C Programming
    Replies: 4
    Last Post: 09-27-2008, 05:11 AM
  4. flag-controlled while loop
    By azsquall in forum C++ Programming
    Replies: 10
    Last Post: 07-02-2008, 02:48 PM
  5. String break to words and numbers
    By westm2000 in forum C Programming
    Replies: 3
    Last Post: 03-16-2002, 03:53 PM

Tags for this Thread