Thread: Comparison between pointer and integer

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    16

    Comparison between pointer and integer

    Hi,

    I am writing a program to read in grades from a text file and calculate the grade point average from a list of grades. I have the basic code done, i have not started to calculate the average or write to the file because there's no point until i figure this problem out although I'm sure its something simple. Any help would be great

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    
    
    
    
    #define Agrade 4.0
    #define Bgrade 3.4
    #define Cgrade 2.8
    #define Dgrade 2.2
    #define Egrade 1.6
    #define Fgrade 1.0
    
    
    
    
    int main ()
    {
    FILE *fopen(), *fp1, *fp2;
    
    
    	float GPA=0.0;
    
    
    	int c,line;
    
    
    	char filename1[80],filename2[80],name[80],grade[20];
    	
    	
    	printf("Please enter the name of the file you wish to open:");
    	
    	gets(filename1);
    	
    	printf("Please enter the name of the file you would like to store the information in:");
    	
    	gets(filename2);
    	
    	fp1= fopen(filename1,"r");
    	
    	fp2= fopen(filename2,"w");
    	
    	if(fp1==NULL)
    	{
    	
    		printf("The File %s could not be opened for reading", filename1);
    		
    		exit(0);
    		
    	}
    	
    	if(fp2==NULL)
    	{
    	
    		printf("The File %s could not be opened for writing", filename2);
    		
    		exit(0);
    	}
    	
    	else 
    	
    	c=getc(fp1);
    	
    	while(c!= EOF && line!='\n')
    	{
    		
    		fscanf(fp1, "%s %c",&name,&grade);
    		
    		if (grade=='A'){
    		
    		GPA+Agrade;
    		}
    		
    		if (grade=='B'){
    		
    		GPA+Bgrade;
    		}
    		
    		if (grade=='C'){
    		
    		GPA+Cgrade;
    		}
    		
    		if (grade=='D'){
    		
    		GPA+Dgrade;
    		}
    		
    		if (grade=='E'){
    		
    		GPA+Egrade;
    		}
    		
    		if (grade=='F'){
    		
    		GPA+Fgrade;
    		}
    		
    		exit(0);
    	}
    
    
    	
    	
    return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I suppose the big question is why are you going to the keyboard before reading your file?

    The error is most likely coming from line 65 where you use the variable "line" in an uninitialized state.

    You can probably remove lines 61 to 66 and line 101 and have it actually work better.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    Thanks for your reply

    I have removed everything that you said but i still get the same error. I also initialized "line" but still no joy. In a previous question i used that same while loop and it worked fine. Also forgive me for my lack of knowledge but where am i going to the keyboard before the file?Thanks again

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The getc() function reads the keyboard...

    I'm assuming you wanted fgetc()

    A much better approach would be this...
    Code:
    c=fgetc(fp1);  // why is this here?
    	
    while(fscanf(fp1, " %s %c", name, &grade) > 1 )
      {
         //...
       }
    Look up fscanf() in your C Library Documentation (if you don't have it... get it) paying special attention to the return value from the call.
    Last edited by CommonTater; 11-12-2011 at 08:50 AM.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    Quote Originally Posted by CommonTater View Post
    The getc() function reads the keyboard...

    I'm assuming you wanted fgetc()
    Sorry that has not removed the error, i have tried what you said.

    c=fgetc(fp1); //This is here in order to read in the text
    Last edited by Thedon; 11-12-2011 at 08:54 AM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Thedon View Post
    Like i said something simple!!thanks for your help it removed the error now i just have to write the rest of the program!

    How can i mark this thread solved?
    Trust me... your problems are not over.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    Quote Originally Posted by CommonTater View Post
    Trust me... your problems are not over.
    I am aware of that. thanks for your help

  8. #8
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Is part of the assignment to rewrite the fopen function?

    fgetc - C++ Reference

    What would happen if I entered an absolute path filename with length 81+?
    Cprogramming.com FAQ > Why gets() is bad / Buffer Overflows

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparison between pointer and integer error
    By jb1989 in forum C Programming
    Replies: 2
    Last Post: 12-13-2010, 08:01 PM
  2. Comparison between pointer & integer
    By m88g88 in forum C Programming
    Replies: 5
    Last Post: 02-16-2010, 05:25 PM
  3. comparison pointer and integer
    By plodos in forum C Programming
    Replies: 19
    Last Post: 01-09-2009, 08:51 AM
  4. comparison between pointer and integer
    By R.Stiltskin in forum C Programming
    Replies: 13
    Last Post: 03-24-2007, 02:33 PM
  5. comparison between pointer and integer
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-07-2006, 01:15 PM