Thread: While Loop in C

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

    While Loop in C

    Hi,

    I am trying to create a log table to use with a uC.
    My program is below. It works for tables up to about 300 lines.
    If I increase it, I lose the top lines in the table in the CMD window
    How can I make it so it will do 4096 lines.
    Also what would be the best way to output it in a .txt file?

    Thanks

    Code:
    #include <math.h>
    #include <stdio.h>
    
    int main( void )
    {
    
    int i=1;
    double x = 1.0, y = 1.0067, z;
    
    z= (x*y);
    printf("%d  %1f  \n",i,x);
    i++;
    while (i<=100) {
    printf("%d  %1f  \n",i,z);
    
    z= (z*y);
     i++;}
    
    
      return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you want 4096 lines in your command prompt window, you'll have to fiddle with your command prompt. To get a file, you need to look at fopen and fprintf.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    3

    Output to .txt

    Thanks for the tip on the CMD window. That is much better now.

    I have looked at the fopen and tried a few things but I am having trouble incorporating
    into the loop. All I can get it to do is print the last line to a file.

    Any code examples would be helpful. I am a real beginner and am not
    getting it.

    Thanks

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You shouldn't keep opening/closing the file inside the loop -- open it before the loop starts, then each pass through the loop should just add one line.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    3

    I got it now thanks

    Here is how I did it.



    Code:
    #include <math.h>
    #include <stdio.h>
    
    int main( void )
    {
    
      int i=1;
      double x = 1.0, y = 1.0020, z;
    
      FILE *fp;
    fp=fopen("test.txt", "w");
    
    
    z= (x*y);
        printf("%d  %1f \n",i,x);
    	fprintf(fp,"%d  %1f \n",i,x);
    	i++;
      while (i<=4096) {
    	  	    printf("%d  %1f\n",i,z);
    				  	    fprintf(fp,"%d  %1f\n",i,z);
    
    z= (z*y);
     i++;}
     
      return 0;
    }

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    2
    Make sure you call:
    Code:
    fclose(fp);
    before your program ends. You should put that before the return.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM