Thread: problems with printing to a txt file from array

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    3

    problems with printing to a txt file from array

    just having a few issues here in printing multiple lines to a txt file.
    basically my lotto program searches and matches the same numbers between two arrays however when there is more than one match its only prints 1 line, not all of them.

    any help or suggestions would be much appreciated

    insert
    Code:
    int main(void)
    {
    
    
    FILE *fpointer;
    fpointer = fopen("bacon.txt", "w");
    
    
        int a;
        int i;
        int j;
        int x;
        int y;
        int selection[6];
        int lotto[6];
        int match[6];
        char answer;
        x=0;
        y=0;
        i=0;
        a=0;
    
    
            printf("Hello and Welcome to Donaldson's Super Duper Lottery! \n \n");
            printf("Please enter your 6 lucky numbers between 1 and 22: \n \n");
            printf("Enter selected NUMBER then hit return to enter next number below \n \n");
            printf(" \n");
    
    
            for(i=0 ; i<6; i++)
                {
                scanf("%d", &selection[i]);
                }
                printf("\n  Your Lucky Numbers for tonights game: \n");
                for (i=0;  i<6;  i++){
                printf("%5d", selection[i]);
                }
    
    
            printf("\n\n\n Ready to Draw???? PRESS 1 then RETURN TO REVEAL!  \n");
            scanf("%d", &answer);
    
    
            if (answer == 1){
                printf(" \n\n Tonights Lucky Draw Numbers are: \n");
                srand(time(NULL));
    
    
                for(j=0 ; j<6  ;j++){
                lotto[j] = rand()%6+1;
                printf("%5d", lotto[j]);
    }}
    
    
    printf("\n \n \n  Ready to see if your matching numbers???   PRESS 1 then RETURN TO REVEAL! \n");
    scanf("%d", &answer);
        if (answer == 1){
    for (x=0;x<6;x++){
        for(y=0;y<6;y++){
            if(selection[x]== lotto[y])
            {match[a] = selection[x];
                printf("\n Number %d is a WINNER!", match[a]);
                fprintf(fpointer,"Number %d is a WINNER! \n", match[a]);
    
    
    fclose(fpointer);
    }}}
    if(match [a]==0)
    printf("\n You have no winning numbers...YOU LOSE! \n \n \n");
    return(0);

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A few things to work on first:
    • Indent your code properly. This means picking a set number of spaces or a single tab per indent level, and sticking to it. You seem to have picked 4 spaces, so be consistent. Your indent style should also be consistent: if you want the opening brace to be on its own line, then always do it like that. Closing braces should generally be on their own lines.
    • Remember to include the required headers.
    • Make your variable names descriptive: avoid single letter variable names except for loop indices. So your i and j are fine, but I'm not sure why you need a, and why not reuse i and j instead of also having x and y.
    • Use named constants instead of magic numbers like 6.
    • You might as well initialise variables in the declaration instead of declaring them then assigning initial values separately.
    • Check the return value of fopen and scanf.
    • Break up your code into smaller functions that each do one thing and do it well.
    • It's probably better to move the srand call to near the top of the main function so you eliminate the risk of calling it more than once with the same seed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Your indentation is bad and you should feel bad... I'm joking, but you should pay more attention to the readability of your code.

    About your problem, of course it prints only one line, since you close the file right after you write to it. I bet this error was directly caused by your awful indentation.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    3
    Quote Originally Posted by GReaper View Post
    Your indentation is bad and you should feel bad... I'm joking, but you should pay more attention to the readability of your code.

    About your problem, of course it prints only one line, since you close the file right after you write to it. I bet this error was directly caused by your awful indentation.

    thanks for that. will tidy it up.

    any suggestions to make my text file read for example;
    1 is a WINNER
    2 is a WINNER

    etc

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    3
    awesome thanks for the time taken to open the response. Will work on your suggestions

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanning file and printing an array
    By Jeff Clay in forum C Programming
    Replies: 3
    Last Post: 11-26-2012, 01:12 AM
  2. printing from file problems
    By Conor1992 in forum C Programming
    Replies: 6
    Last Post: 04-13-2011, 10:09 AM
  3. Replies: 3
    Last Post: 11-03-2003, 08:55 PM
  4. problems reading data into an array and printing output
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-28-2003, 08:39 AM
  5. printing to an array from file
    By problematic in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:21 AM

Tags for this Thread