I need some help with my C progam

This is a discussion on I need some help with my C progam within the C Programming forums, part of the General Programming Boards category; I'm doing this program but I'm having a lot of problems on it. I need some help please. My program ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    1

    Unhappy I need some help with my C progam

    I'm doing this program but I'm having a lot of problems on it. I need some help please.

    My program should open an exiting file, called cal_result.txt, for appending data to it.

    2) It define the following four macros to perform addition, subtraction, multiplication, and division
    * ADD(n1, n2)
    * SUB(n1, n2)
    * MUL(n1, n2)
    * DIV(n1, n2)



    2) You must modify the point structure as shown in the Example 16-1:

    struct point struct cal3field

    { {

    int x; ?????

    int y; ?????

    ????

    } a; } CalResult;

    to create a new structure which contains three fields for holding n1, n2, and the result, all of the float type. A structure variable called CalResult should be the created and should hold the actual values of n1, n2, and result, when the calculation of switch-case is finished.



    4) My program now should open the pre-created file cal_result.txt for appending data to it. I have to use the FORMAT that is a macro that contains something similar to the following statement as shown below for writing data to the disk, each piece of data is separated by a blank space, and n1, n2, and result should be replaced by structure members.

    fprint(fprt, %f %f %f , n1, n2, result);


    Now invoking the the DOS command prompt, I use the some testing data like for example:
    C:\ mycal 10 + 20

    C:\mycal 10 20

    C:\mycal 10 x 20

    C:\mycal 10 / 20

    and now I should get this results

    10.000000 20.000000 30.000000 10.000000 20.0000000 -10.0000000 10.000000 20.000000 200.000000 0.500000.



    Here is what I have so far:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    // MACROS
    #define ADD(n7,n8) ((n7)+(n8))
    #define SUB(n1,n2) ((n1)-(n2))
    #define MUL(n1,n2) ((n1)*(n2))
    #define DIV(n1,n2) ((n1)/(n2))
    //MACRO FORMAT for saving
    #define FORMAT fprint(fPtr"%f%f%f",CalResult.n1,CalResult.n2,CalResult.result)  
    
    
    
    // declare struct CalResult
     struct cal3field 
    {
    	  double n1;
    	  double n2;
    	  double result;
    }	  CalResult;
    
    
    
    
    void main(int argc, char *argv[])
    
    {
    	FILE *fPtr;     /* fPtr = cal_result.txt file pointer */
    
        char op[2] = " "; 
    	double result  = 0, n1 = 0, n2 = 0;
    
    	
     strcpy(op,argv[2]);  
    
    		
    
    //open the file
       /* opens the file. Exit program if was unable to create file  */
       if ( ( fPtr = fopen( "cal_result.txt", "a" ) ) == NULL ) {
    		printf( "File could not be opened\n" );
       } 
    		else { 
          
          //printf( "Enter EOF to end input.\n" );
    
    	  /* get operator */
          n1 = atof(argv[1]); 
    
    	  /* get first argument */
          n2 = atof(argv[3]); 
    	
    	  /* get second argument */
    	  switch(op[0])
    		{
    			case '+': 
    				result = ADD(n1,n2);
    				//PRINT;
    			break;
    
    			case '-': 
    				result = SUB(n1,n2);
    				//PRINT; 
    			break;
    
    			case '*':
    
    			case 'x':
    
    			case 'X': 
    				result = MUL(n1,n2);
    				//PRINT;
    			break;
    
    			case '/':
    			//case '\\': 
    				if(n2 != 0.0) result = DIV(n1,n2);
    					//PRINT; 
    			break;
    			
    			default: puts("Wrong operator entered");
    		}// end case
    
    	// Structure variable CalResult holds values of n1, n2, and result
    	CalResult.n1 = n1; 
    	CalResult.n2 = n2; 
    	CalResult.result = result;
         
    	fprintf( fPtr, "%f %f %f\n", (CalResult.n1),(CalResult.n2),(CalResult.result)
    );
    	 /* write n1, n2 and result into file with fprintf */
          
             //fprintf( fPtr, "%f %f %f\n", n1, n2, result ); // write data
    	//fprintf(fPtr, FORMAT); // write data
    		 
          //} /* end while */
    
          fclose( fPtr ); /* fclose closes file */
    } /* end else */
       
    
    
    //Read the file 
    
    	  /* fopen opens file; exits program if file cannot be opened  */
       if ( ( fPtr = fopen( "cal_result.txt", "r" ) ) == NULL ) {
    		printf( "File could not be opened\n" );
       } // end if 
       else { // read n1, n2 and result from file *
    			printf("Reading file\n");
    			printf( "%-10s%-13s%s\n", "n1", "n2", "result" );
    			//fscanf( fPtr, "%lf%lf%lf", &CalResult.n1, &CalResult.n2 &CalResult.result);
    			//printf( "%f  %f  %f\n   ", CalResult.n1  , CalResult.n2,  CalResult.result);
    			// while not end of file 
    			//while ( !feof( fPtr ) ) { 
    				printf( "%-10f%-13f%7.2f\n", n1, n2, result );
    				fscanf( fPtr, " %lf %lf %lf", &CalResult.n1, &CalResult.n2
    ,&CalResult.result );
    			//} // end while 
    
    		fclose( fPtr ); // fclose closes the file 
       } // end else
    
       
    
    } /* end main */

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    The output file is being appended as expected (as you would see if you opened the file in an editor or printed its contents), so it would seem that your issue is with reading back the file's data and displaying it. After opening the file for reading, do something like this.
    Code:
          printf("Reading file\n");
          printf( "%-10s%-13s%s\n", "n1", "n2", "result" );
          while ( fscanf(fPtr, "%lf %lf %lf", &CalResult.n1,
                         &CalResult.n2, &CalResult.result) == 3 )
          {
             printf( "%-10f%-13f%7.2f\n", CalResult.n1, CalResult.n2, CalResult.result );
          }
          fclose( fPtr ); // fclose closes the file
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by ces4u
    void main(int argc, char *argv[])
    main fucntion to return an int

    Code:
    int main()
    {
      .  
      . 
      return 0;
    }
    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM
  2. about decimal to hex progam
    By Abdi in forum C Programming
    Replies: 2
    Last Post: 06-01-2002, 07:08 PM
  3. Need help with text reversing progam..
    By ThunderCow in forum C++ Programming
    Replies: 3
    Last Post: 09-09-2001, 10:11 AM

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