#include<stdio.h>
#include<ctype.h>
#define FILENAME "computing"


//void proceed(struct registration com, int matric);
//void again(struct registration com);

 struct registration
{
	char code[10];
	char subject[100];
	int credit;
};


int main()
{
	int matric;
	char name;

	FILE *ECE;
	ECE = fopen(FILENAME, "r");
	//struct registration com;

	puts("Welcome to pre-registration for sem 1 2008/2009\n");
	puts("Enter your Matric number\n");
	scanf("%d", &matric);
	
	/*f(matric<700000 && matric>600000)

		proceed(struct registration com,matric);

	else if(matric>=700000 && matric<800000)
	{

		again(struct registration com);
	}
	else

		printf("Wrong ID\n");*/

	return 0;

}

void proceed(struct registration com, int matric)
{
	int option,flag;
	float CGPA;
	FILE *ECE;
	ECE = fopen(FILENAME, "r");
	puts("Welcome %d\n",matric);
	puts("Enter current CGPA\n");
	scanf("%f",&CGPA);
	if(CGPA>=2.6 && CGPA<=4.0)
	{
		puts("\nmaximum credit hour is 18\n");
		puts("Choose option\n");
		puts("\n\t1 add subject\n\t2 drop subject\n");
		scanf("%d",&option);
		switch(option)
		
		{
		case 1:
			{
				puts("\nChoose subject below\n");
				while(fscanf(ECE,"%s %s %d",com.code,com.subject,&com.credit)!=EOF);
				{
					printf("subject code: %s\n subject name: %s\n credit hour: %d\n",com.code,com.subject, &com.credit);
				}

				fclose(ECE);

				while(flag==1){
					printf("Enter subject code(enter \'END\' when finished):");
					scanf("%[^\n]",com.code);
					if(strcmp(com.code,"END")==0)
						break;

					printf("subject name:");
					scanf("%[^\n]",com.subject);
					printf("credit hour:");
					scanf("%d",&com.credit);
				}

			}

		case 2:
			{
				FILE *ECE;
				ECE = fopen(FILENAME, "r");
				puts("choose subject to drop\n");
				while(fscanf(ECE,"%s %s %d",com.code,com.subject,&com.credit)!=EOF);
				{
					printf("subject code: %s\n subject name: %s\n credit hour: %d\n",com.code,com.subject, &com.credit);
				}
				
					fclose(ECE);

				while(flag==1){
					printf("Enter subject code(enter \'END\' when finished):");
					scanf("%[^\n]",com.code);
					if(strcmp(com.code,"END")==0)
						break;

					printf("subject name:");
					scanf("%[^\n]",com.subject);
					printf("credit hour:");
					scanf("%d",&com.credit);
				}
			}

i'm not finish yet, but, is this the way??
there's lot of error, and i'm not sure what is it!!
can i just copy the first code to use for other CGPA and for the other function header for the students who are not in the record??
the data file will contain the subjects name with the code and credit hour

Dani AI

Generated

Quick summary for : the current code has a few common C problems that cause most of the compile/runtime errors and logic bugs. already flagged the output call; beyond that the program needs safer I/O, proper prototypes, initialized variables, correct loop structure (no stray semicolons), and a clear data model for storing multiple subject selections. 's follow-up is useful, but several other problems remain.

Concrete fixes to apply now

  • Put function prototypes (or define functions) before main so the compiler knows their signatures.
  • Check fopen for NULL and handle the error.
  • Initialize variables like flag, option, and counters before use.
  • Remove stray semicolons after loop headers; they make loops do nothing.
  • Don’t pass an address to %d when printing an int—pass the int value.
  • Avoid scanf for free-text fields; use fgets with size limits or carefully width-limited scanf.
  • Use string.h for strcmp/strrchr and check scanf/sscanf return values.

Safe pattern for reading a file where the subject name may contain spaces

/* parse lines like: CODE Subject name possibly with spaces 3 */
typedef struct { char code[16]; char title[128]; int credit; } Course;
Course courses[200];
int n = 0;
char line[256];
FILE *f = fopen("computing.txt","r");
if (f) {
  while (fgets(line, sizeof line, f)) {
    char *p = line + strlen(line) - 1;
    while (p >= line && isspace((unsigned char)*p)) *p-- = '\0';    /* trim */
    char *last = strrchr(line, ' ');
    if (!last) continue;
    int credit;
    if (sscanf(last+1, "%d", &credit) != 1) continue;
    *last = '\0';
    if (sscanf(line, "%15s %127[^\n]", courses[n].code, courses[n].title) >= 1) {
      courses[n].credit = credit; n++;
    }
  }
  fclose(f);
}

Design advice for reuse (instead of copy/paste)

  • Write small reusable functions: load_courses(), print_courses(), add_selection(), drop_selection() and an eligibility check that accepts CGPA.
  • Use arrays or dynamic lists to hold selections and pass them (or pointers) to proceed() rather than copying code for each CGPA range.
  • Compile with warnings enabled to catch many mistakes: gcc -std=c11 -Wall -Wextra prog.c -o prog.

Recommended Answers

All 2 Replies

line 53: you have to use printf() to do that. puts() only accepts one parameter and that is the string that is to be displayed.

I don't know what other errors you want to know about. You'll have to be very specific.

#include<stdio.h>
#include<ctype.h>
#define FILENAME "computing"


//void proceed(struct registration com, int matric);
//void again(struct registration com);

 struct registration
{
	char code[10];
	char subject[100];
	int credit;
};


int main()
{
	int matric;
	char name;

	FILE *ECE;
	ECE = fopen(FILENAME, "r");
	//struct registration com;

	puts("Welcome to pre-registration for sem 1 2008/2009\n");
	puts("Enter your Matric number\n");
	scanf("%d", &matric);
	
	/*f(matric<700000 && matric>600000)

		proceed(struct registration com,matric);

	else if(matric>=700000 && matric<800000)
	{

		again(struct registration com);
	}
	else

		printf("Wrong ID\n");*/

	return 0;

}

void proceed(struct registration com, int matric)
{
	int option,flag;
	float CGPA;
	FILE *ECE;
	ECE = fopen(FILENAME, "r");
	printf("Welcome %d\n",matric);
	puts("Enter current CGPA\n");
	scanf("%f",&CGPA);
	if(CGPA>=2.6 && CGPA<=4.0)
	{
		puts("\nmaximum credit hour is 18\n");
		puts("Choose option\n");
		puts("\n\t1 add subject\n\t2 drop subject\n");
		scanf("%d",&option);
		switch(option)
		
		{
		case 1:
			{
				puts("\nChoose subject below\n");
				while(fscanf(ECE,"%s %s %d",com.code,com.subject,&com.credit)!=EOF);
				{
					printf("subject code: %s\n subject name: %s\n credit hour: %d\n",com.code,com.subject, &com.credit);
				}

				fclose(ECE);

				while(flag==1){
					printf("Enter subject code(enter \'END\' when finished):");
					scanf("%[^\n]",com.code);
					if(strcmp(com.code,"END")==0)
						break;

					printf("subject name:");
					scanf("%[^\n]",com.subject);
					printf("credit hour:");
					scanf("%d",&com.credit);
				}

			}

		case 2:
			{
				FILE *ECE;
				ECE = fopen(FILENAME, "r");
				puts("choose subject to drop\n");
				while(fscanf(ECE,"%s %s %d",com.code,com.subject,&com.credit)!=EOF);
				{
					printf("subject code: %s\n subject name: %s\n credit hour: %d\n",com.code,com.subject, &com.credit);
				}
				
					fclose(ECE);

				while(flag==1){
					printf("Enter subject code(enter \'END\' when finished):");
					scanf("%[^\n]",com.code);
					if(strcmp(com.code,"END")==0)
						break;

					printf("subject name:");
					scanf("%[^\n]",com.subject);
					printf("credit hour:");
					scanf("%d",&com.credit);
				}
			}

i'm not finish yet, but, is this the way??
there's lot of error, and i'm not sure what is it!!
can i just copy the first code to use for other CGPA and for the other function header for the students who are not in the record??
the data file will contain the subjects name with the code and credit hour

ERROR IN LINE 53 --->

You cannot serve more than 1 arument here, 'cause puts() accepts only 1 argument. i.e array name or string to be printed..

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.