Hi everyone, I am having some trouble here...


I am asking the user to enter a file, and that file will read the contents and determine if the student will pass or fail the class.

That part I am having trouble with is when I have to decide if the student passes or fails the class.

#include <stdio.h>

int main()
{
  
  FILE* pFile = NULL;
  int nsid = 0;
  char lastname[255];
  char filename[80];
  float midterm = 0;
  float final = 0;
  float wavg = 0;
  char decision[80];
  
  // asking the user to enter a filename, the file will read the conents
  printf("Please enter a filename for input: ");
  scanf("%s", &filename); 
  
  pFile = fopen(filename, "r");
  if (pFile == NULL)
  {
    printf ("Error opening file!\n");
    return -1;
  }
  
  //the file will read nsid, lastname, midterm, and final marks then calculate the weighted average and decide if the student passes
  printf("%-10s %-10s %-10s %-10s %-10s %-10s\n", "nsid" , "lastname" , "midterm" , "final" , "w.avg" , "decision");
  while (!feof(pFile))
  {
    fscanf(pFile, "%d %s %f %f\n", &nsid, lastname, &midterm, &final);
    
    //weighted average is calculated
    wavg = (midterm * 0.45) + (final * 0.60);
    
    //problem starts here
    if (wavg > 50.00)
    { 
     char (&decision = "PASSED");
    }
    else
    {
      char (&decision = "FAILED");
    }
    
    printf("%-10d %-10s %-10f %-10f %-10f %-10s \n", nsid, lastname, midterm, final, wavg, decision);
    
  }
  
   fclose(pFile);
  
  return 0;
}

These are the errors it shows:

a2.c: In function ‘main’:
a2.c:36: error: expected identifier or ‘(’ before ‘&’ token
a2.c:40: error: expected identifier or ‘(’ before ‘&’ token

Can anyone please help?

char (&decision = "PASSED");

The above code is not allowed. If you need to compare strings then include the string.h header and use strcmp(). Ooops, I think you might be trying to set decision equal to "PASSED". If you want that to happen then change decision to a character pointer...

char *decision;

decision = "PASSED";
char (&decision = "PASSED");

The above code is not allowed. If you need to compare strings then include the string.h header and use strcmp(). Ooops, I think you might be trying to set decision equal to "PASSED". If you want that to happen then change decision to a character pointer...

char *decision;

decision = "PASSED";

hey thanks, it works now!

#include <stdio.h>
int main()
{
	FILE* pFile = NULL;
	int nsid = 0;
	char lastname[255];
	char filename[80];
	float midterm = 0;
	float final = 0;
	float wavg = 0;
	char * decision;
	// asking the user to enter a filename, the file will read the conents
	printf("Please enter a filename for input: ");
	scanf("%s", &filename);
	pFile = fopen(filename, "w+");
	if (pFile == NULL)
	{
		printf ("Error opening file!\n");
		return -1;
	}
	//the file will read nsid, lastname, midterm, and final marks then calculate the weighted average and decide if the student passes
	printf("%-10s %-10s %-10s %-10s %-10s %-10s\n", "nsid" , "lastname" , "midterm" , "final" , "w.avg" , "decision");
	while (!feof(pFile))
	{
		fscanf(pFile, "%d %s %f %f\n", &nsid, lastname, &midterm, &final);
		//weighted average is calculated
		wavg = (midterm * 0.45) + (final * 0.60);
		//problem starts here
		if (wavg > 50.00)
		{
			decision = "PASSED";
		}
		else
		{
			decision = "FAILED";
		}
		printf("%-10d %-10s %-10f %-10f %-10f %-10s \n", nsid, lastname, midterm, final, wavg, decision);
	}
	fclose(pFile);
	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.