Thread: Running Into Two Problems...Help Needed

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    52

    Running Into a problem here...Help Needed

    Well problem one consists of my program not writing its actions down to the file where I told it to. I checked and checked again; I seem to have the right codes maybe in the wrong order though...

    EDIT:Well I just fixed one of the problem stil can't get this thing to print to the txt though.

    #include <stdio.h>
    #include <conio.h>
    #include <math.h>

    void main(void)
    {
    char c;
    int N,x,iter = 1;

    FILE *outptr;
    outptr=fopen("myOutput.txt","wt");
    printf("\nPlease enter a value for N ");
    scanf("%i",&N);
    printf("\n ");
    for(x = N, iter = 0; x > 1; x--)
    {
    printf("%i", x);
    printf(" + ");
    iter = iter + x;
    fprintf(outptr, "%i", x);
    fprintf(outptr, " + ");
    }
    printf(" 1 = ");
    printf("%i", iter);
    getch();
    fclose(outptr);
    Last edited by Halo; 04-17-2002 at 10:08 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    int s(int a){return a+(a>0?s(a-1):0);}

    Here's the fastest way to do this. You could just throw a printf statement in there and have it work the way you expect.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    52
    I don't get how a printf statement would help me put the work done by the program on that txt file. I thought only fprintf prints stuff on output files.

    For some reason or another nothing is comming out on that txt file.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeah. Use fprintf if you want the output to a file. Mine just sums it up for you. Given a number, it generates its sum.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    52
    lol, Q this whole post was about I don't know why my fprintf doesn't work ? Nothing gets printed to output file... You got any ideas ?
    Last edited by Halo; 04-17-2002 at 10:38 PM.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    After cleaning it up heavily, it works fine for me. Try this and see if it makes any difference
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
      int N, x, iter = 1;
    
      FILE *outptr;
      if ( ( outptr=fopen("myOutput.txt","w") ) != NULL ) {
        printf("\nPlease enter a value for N ");
        scanf("%i",&N);
        printf("\n");
        for(x = N, iter = 0; x > 1; x--)
        {
          printf("%i", x), fprintf(outptr, "%i", x);
          printf(" + "), fprintf(outptr, " + ");
          iter = iter + x;
        }
        printf(" 1 = "), fprintf(outptr, " 1 = ");
        printf("%i", iter), fprintf(outptr, "%i", iter);
        getchar();
        fclose(outptr);
      }
      else
        printf ( "Error opening file\n" );
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  2. Some Socket Problems...?
    By Tal0n in forum Linux Programming
    Replies: 1
    Last Post: 04-01-2004, 05:31 AM
  3. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  4. NT Service problems - loop
    By nickname_changed in forum Windows Programming
    Replies: 0
    Last Post: 07-09-2003, 12:54 AM
  5. Help- Problems running outside of compiler
    By Derek5272 in forum C++ Programming
    Replies: 17
    Last Post: 05-07-2003, 08:19 AM