Thread: need help

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    4

    need help

    ok I have a project for my C programming class due pretty soon but I can't seem to get the darn output file to contain any of the variables values.

    Code:
    #include <stdio.h>
    
    
    int main (void)
    {
    	double  V,I, R1, RL, vR1, vR2, vRL, PR1, PR2, PRL,RS;
    	int count;
    	int R2;
    	
    FILE *f= NULL;		
       
    		f = fopen("G:\\Projects\\ET156-C Programming\\DC-Final Project\\Final Project Data\\data.txt","a");
    		
    			fprintf(f,"%s","\nR1+R2\t\tRL Power\tPower R2\tPower R1\tRL\n");
    			fprintf(f,"%s","---------\t------------\t------------\t--------\t---\n"); 
    			
    			printf("this program will repeat 10 times\n");
    			while(count<10){
    			//Asks for voltage
    			printf("Please input a value between 1 & 15 for the Voltage: ");
    				scanf("%lf", &V);
    		//if voltage is not within parameters asks for a different voltage
    		if (V<15.4 && V>0.4);
    			
    		else{
    				
    				printf("Please use a voltage between 1 & 15: ");
    					scanf("%lf", &V);
    				}
    		//asks for a value for RL
    			printf("Please enter a value between 0 & 5000 for RL: ");
    				scanf("%lf", &RL);
    		if (RL<5000.4 && RL>-0.6);
    		
    		else{
    			printf("Please use a load resistance between 0 & 5k ohms: ");
    				scanf("%lf", &RL);
    			}
    		//RS must equal RL for Max Power Transfer
    		R1 = 100;
    		R2 = RL - 100;
    		RS = R1 + R2;
    		//Formulas used to calculate Maximum Power Transfer
    		I = V /(RS + RL);
    		vR1 = I * R1;
    		vR2 = I * R2;
    		vRL = I * RL;
    		PR1 = I * vR1;
    		PR2 = I * vR2;
    		PRL = I * vRL;
    
     
    			
    			
    			fprintf(f,"%d","\n\n %f\n\t\t %g \t\t %g \t\t %g \t\t %g \f\n" , RS, PRL, PR2, PR1, RL);
    
    		
    	
    	
    		printf("Maximum power transferred to RL is %.3lf Watts\n\n\n\n", PRL);
    
    		count++;
    		}
    
    
    	fclose (f);
    				
    	
    
    return (0); 
    }
    that is the code that I have written so far. any help with the last fprintf would be much appreciated.

    thanks

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First, you have to check if the fopen() call succeeded or not... If it fails, you should exit your program.

    This will only work if all the sub-folders in your path already exist.
    Using fopen() in append mode will create a file... but it will not create folders.

    Why not shorten that down to just fopen("data.txt","a") for testing...
    Last edited by CommonTater; 08-21-2011 at 08:07 PM.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    fprintf(f,"%f\n\t\t %g \t\t %g \t\t %g \t\t %g \f\n" , RS, PRL, PR2, PR1, RL);
    I suggest learning how to enable your compiler warnings; then learn to understand the warnings.

    Indent you code, so it is readable, or you are not going to get much help.

    Edit: Also, Tater's suggestion is very good; but, your code did not compile for me(after I fixed the first warning); so, I ignored any run-time issues.

    Tim S.
    Last edited by stahta01; 08-21-2011 at 08:15 PM.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    4
    I apologize... this is only my third time writing code... so sorry if my format is wrong... I have gotten it working so thank you all for your criticism and what not I will bear in mind your guys' advice.

Popular pages Recent additions subscribe to a feed

Tags for this Thread