Thread: Need Help Adding something to my code

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    24

    Need Help Adding something to my code

    I have the following code (shown below) compiling correctly and I'm able to run it. The only thing I need to add is logic so that if there is no input data file in the directory, to display a message "Error Opening File". How can I do this?

    This code looks for an input_data.txt file, then it calculates something and writes to another file called output_data.txt file.

    Code:
    int main ()
    {
    	FILE *fptr1;
    	FILE *fptr2;
    	char fName[20];
    	char lName[20];
    	char buf[100];
    	char letterGrade;
    	int final_score;
    	int count;
    	int quiz1, quiz2, quiz3, quiz4, mid, final;
    
    
    	fptr1 = fopen("input_data.txt","r");
    //display the results
    	fptr2 = fopen("output_data.txt","w");
    //reading using fscanf
    
    
    	while(fscanf(fptr1,"%s %[^ \t\n\r\v\f,]%*c %d, %d, %d, %d, %d, %d", fName,lName,&quiz1,&quiz2,&quiz3,&quiz4,&mid,&final) != EOF)
    	{
    //determine final_score calculation 
    		final_score = quiz1*.10 + quiz2*.10 + quiz3*.10 + quiz4*.10 + mid*.25 + final*.35; 
    		printf("%d", final_score); 
    
    
    //determine letter grade based on final_score
    		if (final_score >= 90) 
    			letterGrade = 'A'; 
    		else if (final_score >= 80 && final_score < 90)
    			letterGrade = 'B'; 
    		else if (final_score >= 70 && final_score < 80) 
    			letterGrade = 'C'; 
    		else if (final_score >= 60 && final_score < 70) 
    			letterGrade = 'D'; 
    else
    			letterGrade = 'F'; 
    
    
    //following code will output the file 
    //and move on to the second student
    		if(fptr2) 
    		{
    			fprintf(fptr2, "%s %s     :%c\n", fName, lName, letterGrade); 
    			fflush(fptr2); 
    		}
    	}
    	fclose(fptr1); 
    	fclose(fptr2); 
    }
    
    
    
    
    
    
    
    
    
    

  2. #2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C code adding a delay
    By Carlenegriffith in forum C Programming
    Replies: 3
    Last Post: 11-06-2011, 04:31 PM
  2. Adding extra lines of code doesn't work
    By Yoshi1981 in forum C++ Programming
    Replies: 6
    Last Post: 03-17-2010, 09:12 PM
  3. Adding C++ content into C code
    By lautarox in forum C Programming
    Replies: 1
    Last Post: 05-20-2009, 12:55 PM
  4. adding to a set
    By Suchy in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2006, 12:53 AM
  5. adding to C++
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 01-06-2003, 01:02 PM