Thread: Write a new text file only with the winners - The Key is in crescent order

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

    Write a new text file only with the winners - The Key is in crescent order

    If you got at least 3 numbers match you are a winner.

    I think it is working, feel free to give advice to improve the code.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main(int argc, char** argv) {
    
    
        int nr[6];
        int nr_prize[6] = {2, 3, 5, 10, 50, 60};
    
    
        char lineA[100];
        char lineB[100];
    
    
        FILE *pFile, *pTemp;
    
    
        // If the file does not exist or got any problem, the program ends.    
        if ((pFile = fopen("totoloto.txt", "rt")) == NULL) {
            printf(" %d %s\n", errno, strerror(errno));
            exit(EXIT_FAILURE);
        }
    
    
    
    
        // The new file will get only the people that have prize.
        if ((pTemp = fopen("temp.txt", "wt")) == NULL) {
            printf(" %d %s\n", errno, strerror(errno));
            exit(EXIT_FAILURE);
        }
    
    
        
          // Reading the customer names (frist)
        while (fgets(lineA, sizeof (lineA), pFile)) {
            // Getting the numbers from the customer
            fgets(lineB, sizeof (lineB), pFile);
            sscanf(lineB, "%d %d %d %d %d %d", &nr[0], &nr[1], &nr[2], &nr[3], &nr[4], &nr[5]);
    
    
            int jackpot = 0;
            // Want to see if my numbers got prize (The numberes are in crescent order)
            for (int i = 0; i < 6; i++) {
                for (int j = i; nr[i] <= nr_prize[j] || j < 6; j++) {
                    if (nr_prize[j] == nr[i]) {
                        jackpot++;
                    }
                }
            }
    
    
            if (jackpot >= 3) {
                printf("Win the prize -> %s", lineA);
                fputs(lineA, pTemp);
                fprintf(pTemp, "%d %d %d %d %d %d\n", nr[0], nr[1], nr[2], nr[3], nr[4], nr[5]);          
            }
        }
    
    
        fclose(pFile);
        fclose(pTemp);
    
    
    
    
        return (EXIT_SUCCESS);
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    How many matches in 1,2,3,4,5 ?
    I count 3, but your program would say 0.

    You mean && j < 6, not || j < 6.

    "crescent" --> "increasing"

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    61
    Quote Originally Posted by algorism View Post
    How many matches in 1,2,3,4,5 ?
    I count 3, but your program would say 0.

    You mean && j < 6, not || j < 6.

    "crescent" --> "increasing"

    Sorry i am not understanding your questions .

    I will leave here a example of the file. (It should be already on the 1 post)

    Joao C. Silva
    2 3 6 8 10 34
    Maria Amelia
    2 3 6 8 10 50
    Ana Lima Nunes
    4 10 23 30 50 60
    Antunes Silvestre
    3 12 40 41 42 43

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    If the winning number is 2, 3, 5, 10, 50, 60 (as in your program), and the ticket number is 1, 2, 3, 4, 5, 6, then how many matches would there be? It looks like 3 to me, since my ticket has a 2, 3, and 5 in it. But your program says 0 matches.

    Try adding that number to your input file to see what happens.

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    61
    Quote Originally Posted by algorism View Post
    If the winning number is 2, 3, 5, 10, 50, 60 (as in your program), and the ticket number is 1, 2, 3, 4, 5, 6, then how many matches would there be? It looks like 3 to me, since my ticket has a 2, 3, and 5 in it. But your program says 0 matches.

    Try adding that number to your input file to see what happens.
    I see

    Code:
            for (int i = 0; i < 6; i++) {
                for (int j = 0; nr[i] >= nr_prize[j] && j < 6; j++) {                
                    if (nr_prize[j] == nr[i]) {
                        jackpot++;
                    }
                }
            }
    Last edited by thinkabout; 06-25-2017 at 01:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Write the low-order 8 bits of the time to the disk file
    By kfuller002 in forum C Programming
    Replies: 2
    Last Post: 11-10-2010, 02:32 PM
  2. Replies: 2
    Last Post: 07-18-2009, 01:17 PM
  3. Write to a Text File
    By slowcoder in forum C Programming
    Replies: 3
    Last Post: 08-14-2007, 07:52 AM
  4. Replies: 9
    Last Post: 11-20-2003, 07:51 PM
  5. is there a way to write the file names in a folder in a text file?
    By Commander in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 08-15-2002, 05:11 PM

Tags for this Thread