Thread: For loop not looping

  1. #1
    Registered User
    Join Date
    Sep 2020
    Posts
    21

    For loop not looping

    Trying to write to file via function. Loop writes to screen properly but same syntax in the write_report function 'i' does not increment. ??? Thanks for help.


    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    
    
    void write_report(int *num_set);
    
    
    int main()
    {
    int num_set[101] = {0};
    
    
    for (int i = 0; i < 101; i++)
    
    
    printf("\nnumber = %d", i);
    
    
    write_report(num_set);
    
    
    system("PAUSE");
    
    
    return (0);
    }
    
    
    /*************************************************************/
    void write_report(int num_set[])
    {
     FILE *pWrite;
     pWrite = fopen("vle.dat", "w");
        if (pWrite == NULL)
        printf("\nFile not opened\n");
        else
        fprintf(pWrite, "\nnumber\n");
    
    
        for (int i = 0; i < 101; i++)
          {
             fprintf(pWrite,"%d\n", num_set[i]);
          }
          fclose(pWrite);
    }

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    21
    Trying to write to file via function. Loop writes to screen properly but same syntax in the write_report function 'i' does not increment. ??? Thanks for help.

    Before anybody answers this... I see it. Still a noob. Duh


    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    
    
    void write_report(int *num_set);
    
    
    int main()
    {
    int num_set[101] = {0};
    
    
    for (int i = 0; i < 101; i++)
    
    
    printf("\nnumber = %d", i);
    
    
    write_report(num_set);
    
    
    system("PAUSE");
    
    
    return (0);
    }
    
    
    /*************************************************************/
    void write_report(int num_set[])
    {
     FILE *pWrite;
     pWrite = fopen("vle.dat", "w");
        if (pWrite == NULL)
        printf("\nFile not opened\n");
        else
        fprintf(pWrite, "\nnumber\n");
    
    
        for (int i = 0; i < 101; i++)
          {
             fprintf(pWrite,"%d\n", num_set[i]);
          }
          fclose(pWrite);
    }

  3. #3
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    339

    Post

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void write_report(int *num_set);
    
    int main() {
        int num_set[101] = {0};
    
        for (int i = 0; i < 101; i++) {
            num_set[i] = i;
            printf( "\nnumber = %d", num_set[i] );
        }
    
        write_report( num_set );    
    
        return 0;
    }
    
    void write_report(int num_set[]) {
        
        FILE *pWrite;
        pWrite = fopen("vle.dat", "w");
        
        if (pWrite == NULL) {
            printf("\nFile not opened\n");
        } else {
            fprintf(pWrite, "\nnumber output file\n");
        }
    
        for (int i = 0; i < 101; i++) {
            fprintf(pWrite,"number: %d\n", num_set[i]);
        }
    
        fclose(pWrite);
    
    
    }
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While loop not looping
    By Arcand in forum C Programming
    Replies: 19
    Last Post: 07-21-2012, 05:33 AM
  2. Do/While loop not looping
    By pdenman in forum C++ Programming
    Replies: 6
    Last Post: 03-18-2011, 01:10 AM
  3. looping a variable within a loop.
    By vespine in forum C Programming
    Replies: 3
    Last Post: 06-27-2010, 10:30 PM
  4. do while loop keeps looping
    By wankel in forum C Programming
    Replies: 23
    Last Post: 06-15-2009, 04:53 PM
  5. 'For loop' looping one too many times.
    By thealmightyone in forum C Programming
    Replies: 7
    Last Post: 02-20-2009, 06:46 AM

Tags for this Thread