Question regarding array for printing income and tax on to a file

This is a discussion on Question regarding array for printing income and tax on to a file within the C Programming forums, part of the General Programming Boards category; Hey guys, I'm stuck on a tax program. First i need to use a loop to generate 10 incomes and ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    6

    Question Question regarding array for printing income and tax on to a file

    Hey guys,
    I'm stuck on a tax program. First i need to use a loop to generate 10 incomes and save them in an array. Then use a loop to calculate the tax and save the taxes in another array. After calculating all the taxes, i have to write the output to a file.
    This is what i have so far... (I got it to run but i dont know how to loop the scanf)

    Code:
    #include <stdio.h>
    #include <math.h>
    void main (void)
    
    {
    
    	int i, j, k, num_elem;
    	double   tax, x[9], y[9], z[9];
    	FILE *outfile;
    	
    	printf ("Type in your ten incomes\n");
    	
    	outfile= fopen("a6q1.out","w");
    	
    	scanf ("%lf", &x[0]);
    	
    	fprintf (outfile, "Value of EOF = %d\n", EOF);
    	
    	
    	while (x[i] != EOF) i++;
    
    	
    	num_elem = i;
    	
    	fprintf(outfile,"x[i]\n");
    
    	for (j=0; j<num_elem; j++)
    		{
    			if(x[j]<1000)
    			{
    			tax=0;
    			printf("%.2lf %.2f", x[j], tax);
    			}
    			else if(x[j]>=2000)
    			{
    			tax=x[j]*0.30;
    			printf("%.2lf %.2f", x[j], tax);
    			}
    			else 
    			{
    			tax=x[j]*0.20;
    			printf("%.2lf %.2f", x[j], tax);
    			}
    		}
    		
    	fclose(outfile);
    }
    Any help would be greatly appreciated. Thanks!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >void main (void)
    You really don't want to see me have a conniption, do you? void main is the root of all evil, use
    Code:
    int main ( void )
    instead. No argument, do it.

    Concerning your code...have you tried following the logic on paper? Did you become as confused as I did? Try something more like this:
    Code:
    #include <stdio.h>
    
    #define N    10
    #define HTAX 0.30
    #define LTAX 0.20
    #define LOW  1000
    #define HIGH 2000
    
    int main ( void )
    {
      double income[N];
      double taxes[N];
      double taxed_income[N];
      int i;
      int j;
    
      printf ( "Enter %d incomes: ", N );
      fflush ( stdout );
      for ( i = 0; i < N && scanf ( "%lf", income[i] ) == 1; i++ ) {
        if ( income[i] < LOW )
          taxes[i] = 0;
        else if ( income[i] >= HIGH )
          taxes[i] = income[i] * HTAX;
        else
          taxes[i] = income[i] * LTAX;
        taxed_income[i] = income[i] + taxes[i];
      }
      for ( j = 0; j < i; j++ )
        printf ( "%d: %f\n", j, taxed_income[j] );
    
      return 0;
    }
    Of course, this doesn't print to file.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    6
    Thanks so much, but i still need to print to file... I will keep trying.





  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but i still need to print to file...
    I know, I did that on purpose because I'm evil.

    >I will keep trying.
    The trick to working successfully with files is to remember that once they're open, you can use them exactly the same way as stdin/stdout, just use functions that take a stream pointer instead of assuming stdin or stdout like scanf and printf do. And don't forget to close your file when you're done. I wrote a FAQ entry concerning this topic if you're interested in more detail. Of course, I wrote it a while ago and haven't read it in some time, so read at your own risk...I've been known to make mistakes.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Big help for big program
    By Mahesh in forum C Programming
    Replies: 1
    Last Post: 05-04-2005, 10:02 AM
  2. question to anybody who knows tax laws of ohio
    By Scourfish in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-14-2002, 11:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21