Logical error. e

This is a discussion on Logical error. e within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #include<string.h> #include<stdlib.h> #define CLASS_SIZE 7 void GPA_BillProcessingSystem(); void Student_Records_Maintenance(); void Add(); void Modify(); void Delete(); void Process_Examination_Report(); void ...

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

    Post Logical error. e

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    
    #define CLASS_SIZE  7
    
    
    void GPA_BillProcessingSystem();
    void Student_Records_Maintenance();
    void Add();
    void Modify();
    void Delete();
    void Process_Examination_Report();
    void Generate_Bill();
    
    
    
    
    typedef struct{
    	char StudentId[30];
    	char Name[30];
    	char Gender;
    	char Programme[5];
    	int Year;
    	int Sem;
    	double Fees;
    }StudentData;
    StudentData data[CLASS_SIZE];
    
    
    FILE *StudentRecord;
    char conf, a1;
    char ID[30];
    int choice;
    int i=0, count=0, a=0;
    char NEWid[30];
    	char NEWname[30];
    	char NEWgender;
    	char NEWprogram[5];
    	int NEWyear;
    	int NEWsem;
    	double NEWfees;
    
    
    
    
    ///////MAIN
    int main()
    {
    	do{
    	system("cls");
    
    
    	printf(" ______    ______     ____  \n"
    		"   |         |      |   |    | \n"
    		"   |     ___ |______|   |____| \n"
    		"   |______|  |          |    | \n\n"
    
    
    	    "\t\t GPA and Bill Processing System \n"
    		"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
    	    "\t[1] Student Records Maintenance\n"
        	"\t[2] Process Examination Report\n"
    		"\t[3] Generate Bill\n"
    		"\t[4] Exit\n\n"
    		"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
    
    
        printf("CHOOESE Your Choice :) ");
    	scanf(" %d", &choice);
    
    
    	switch(choice)
    	{
    	case 1:
    		Student_Records_Maintenance();
    		break;
    	case 2:
    		Process_Examination_Report();
    		break;
    	case 3:
    		Generate_Bill();
    		break;
    	case 4:
    		system("exit");
    	default:
    		printf("INVALID =) !\n");
    	}
    	}while(choice!=4);
    
    
    	return 0;
    }
    
    
    //////////STUDENT RECORDS MAINTENANCE
    void Student_Records_Maintenance()
    {
    	do{
    	system("cls");
    
    
    	printf(" ______    ______     __     __  \n"
    		"   |         |      |   |  \\  /  | \n"
    		"   |______   |______|   |   \\/   | \n"
    		"    ______|  |    \\    |         | \n\n"
    
    
    	    "\t\t Student Records Maintenance\n"
    		"=================================\n"
    		"[1] ADD Student Record\n"
    		"[2] MODIFY Student Record\n"
    		"[3] DELETE Student Record\n"
    		"[4] Main Menu\n\n");
    
    
    	printf("CHOOSE Your Choice :) ");
    	scanf("%d", &choice);
    
    
    	switch(choice)
    	{
    	case 1:
    		Add();
    		break;
    	case 2:
    		Modify();
    		break;
    	case 3:
    		Delete();
    		break;
    	case 4:
    		main();
    		break;
    	default:
    		printf("INVALID =) !\n");
    	}
    	}while(choice!=4);
    }
    
    
    ///////ADD STUDENT
    void Add()
    {
    	StudentRecord = fopen("StudentRecord.txt","a+");
    
    
    	if((StudentRecord = fopen("StudentRecord.txt","a+"))==NULL)
    	{
    		printf("Cannot Open StudentRecord.txt file!\n");
    		exit(-1);
    	}
    
    
    	for(i=0 ; i<CLASS_SIZE ; i++)
    	{
    	do
    	{
    		system("cls");
    		printf(" ____    _____    _____  \n"
    			"   |    |  |     )  |     ) \n"
    			"   |____|  |     )  |     ) \n"
    			"   |    |  |_____)  |_____) \n\n"
    
    
    			"\t\t ADD Student Records\n"
    			"###########################\n\n");
    
    
    	printf("Student ID : ");
    	scanf(" %s", &data[i].StudentId);
    	fflush(stdin);
    
    
    	printf("Name : ");
    	scanf(" %s", &data[i].Name);
    	fflush(stdin);
    
    
    	printf("Gender : ");
    	scanf(" %c", &data[i].Gender);
    	fflush(stdin);
    
    
    	printf("Programme : ");
    	scanf(" %s", &data[i].Programme);
    	fflush(stdin);
    
    
    	printf("Year of Study : ");
    	scanf(" %d", &data[i].Year);
    	fflush(stdin);
    
    
    	printf("Semester : ");
    	scanf(" %d", &data[i].Sem);
    	fflush(stdin);
    
    
    	printf("Fees : ");
    	scanf(" %lf", &data[i].Fees);
    	fflush(stdin);
    
    
    
    
    	fprintf(StudentRecord, " %s | %[^\n] | %c | %s | %d | %d | %.2f \n", data[i].StudentId, data[i].Name, data[i].Gender, data[i].Programme, data[i].Year, data[i].Sem, data[i].Fees);
    
    
    	
    	printf("More Record [Y/N] :? " );
    	scanf(" %c", &conf);
    	fflush(stdin);
    
    
    	count++;
    	}while(conf=='Y' || conf=='y');
    	fclose(StudentRecord);
    }
    
    
    
    
        for(a=0; a<30; a++)
    	{
    		_sleep(30);
    		printf("*");
    	}
    	printf("ADDED Student Record ^-^ !\n");
    	system("cls");
    
    
    }
    
    
    ///////MODIFY STUDENT
    void Modify()
    {
    
    
    
    
    	StudentRecord = fopen("StudentRecord.txt","r");
    
    
    	if(StudentRecord==NULL)
    	{
    		printf("NOT FOUND!!\n");
    	}
    
    
    	while(fscanf(StudentRecord, " %s", data[i].StudentId)!=EOF)
    	{
    		fscanf(StudentRecord, " %[^\n] | %c | %[^\n] | %d | %d | %.2f", data[i].Name, &data[i].Gender, data[i].Programme, &data[i].Year, &data[i].Sem, &data[i].Fees);
    		i++;
    		fflush(stdin);
    	}
    	fclose(StudentRecord);
    
    
    
    
    	system("cls");
    	printf("Enter Student's ID to search :) ");
    	scanf(" %s", ID);
    
    
    	for(i=0;i<CLASS_SIZE+count;i++)
    	{
    		if(strcmp(data[i].StudentId, ID)==0)
    		{
    			system("cls");
    
    
    			printf(" __    __    _____    _____ \n"
    				"   | \\  /  |  (     )  |     )\n"
    				"   |  \\/   |  (     )  |     )\n"
    				"   |        |  (_____)  |_____)\n\n"
    
    
    				"\t\t MODIFY Student Records\n"
    				"##############################\n\n");
    			printf("Student ID : ");
    			scanf(" %s", NEWid);
    			strcpy(data[i].StudentId, NEWid);
    			fflush(stdin);
    
    
    			printf("Student Name : ");
    			scanf(" %[^\n]", NEWname);
    			strcpy(data[i].Name, NEWname);
    			fflush(stdin);
    
    
    			printf("Gender : ");
    			scanf(" %c", &NEWgender);
       			data[i].Gender = NEWgender;
    			fflush(stdin);
    
    
    			printf("Programme : ");
    			scanf(" %s", NEWprogram);
    			strcpy(data[i].Programme, NEWprogram);
    			fflush(stdin);
    
    
    			printf("Year of Study : ");
    			scanf(" %d", &NEWyear);
    			data[i].Year = NEWyear;
    			fflush(stdin);
    
    
    			printf("Semester : ");
    			scanf(" %d", &NEWsem);
    			data[i].Sem = NEWsem;
    			fflush(stdin);
    
    
    			printf("Fees : ");
    			scanf(" %lf", &NEWfees);
    			data[i].Fees = NEWfees;
    			fflush(stdin);
    
    
    			system("cls");
    			for(a=0; a<30; a++)
    			{
    				_sleep(30);
    				printf(".");
    			}
    			printf("MODIFIED Student Record ^-^ !\n"
    				"==================================\n");
    			printf("Student ID    :) %s     \n", data[i].StudentId);
    			printf("Name          :) %[^\n] \n", data[i].Name);
    			printf("Gender        :) %c     \n", data[i].Gender);
    			printf("Programme     :) %s     \n", data[i].Programme);
    			printf("Year of Study :) %d     \n", data[i].Year);
    			printf("Semester      :) %d     \n", data[i].Sem);
    			printf("Fees          :) %.2f   \n", data[i].Fees);
    			fprintf(StudentRecord, " %s | %[^\n] | %c | %s | %d | %d | %.2f \n", data[i].StudentId, data[i].Name, data[i].Gender, data[i].Programme, data[i].Year, data[i].Sem, data[i].Fees);
    		}
    
    
    		else
    		{
    			for(a=0; a<30; a++);
    			{
    				_sleep(30);
    				printf(".");
    			}
    			printf("Data NOT Found!!");
    			system("cls");
    			main();
    		}
    		fclose(StudentRecord);
    
    
    		system("cls");
    		main();
    
    
    }
    }
    
    
    /////// DELETE STUDENT
    void Delete()
    {
    	system("cls");
    
    
    	StudentRecord = fopen("StudentRecord.txt","r");
    
    
    	if(StudentRecord==NULL)
    	{
    		printf("NOT FOUND =) !\n");
    	}
    
    
    	while(fscanf(StudentRecord, "%s | %[^\n] | %c | %s | %d | %d | %lf", data[i].StudentId, data[i].Name, data[i].Gender, data[i].Programme, data[i].Year, data[i].Sem, data[i].Fees)!=EOF)
    		{
    			i++;
    		}
    
    
    		printf(" ______    ______            \n"
    			"   |      )  |          |       \n"
    			"   |      )  |______    |       \n"
    			"   |______)  |______    |______ \n\n"
    
    
    	     	"\t\t DELETE Student Record\n"
    			"#############################\n\n");
    		printf("Enter Student's ID to Delete :) ");
    		scanf(" %s", ID);
    		fflush(stdin);
    
    
    
    
    		if(strcmp(data[i].StudentId, ID)==0)
    		{
    		  //data[i].StudentId = '\0';
    		}
    		else
    		{
    			printf("INVALID =) !\n");
    		}
    	fclose(StudentRecord);
    }
    
    
    
    
    
    
    ///////PROCESS EXAMINATION REPORT
    void Process_Examination_Report()
    {
    
    
    	char size[CLASS_SIZE][5]={0}, grade;
    	double avgGPA=0, GPA=0, highGPA=0, lowGPA=0, total=0, num, sum=0;
    	int index1=0, index=0, j=0;
    
    
    	system("cls");
    	printf(" ______    ______    ______    \n"
    		"   |      |  |         |      |   \n"
    		"   |______|  |______   |______|   \n"
    		"   |         |______   |    \\    \n\n"
    		"\t\t Process Examination Report \n"
    		"===================================\n\n");
    
    
    	for(i=0; i<CLASS_SIZE + count; i++) //for row
    	{
    		printf("Enter the Grade for Student Number %d =)\n", i+1);
    		for(j=0; j<CLASS_SIZE + count; j++) //for column
    		{
    			printf("Subject %d =) ", j+1);
    			scanf(" %c", &size[i][5]);
    		}
    	}
    	fflush(stdin);
    
    
    	printf("  ______    ______    _______    _______    _______    _______ \n"
    		"    |      |  |         |       |  (       )  |       |      |    \n"
    		"    |______|  |______   |_______|  (       )  |_______|      |    \n"
    		"    |    \\   |______   |          (_______)  |     \\       |    \n\n"
    		"\t\t Examination Report \n"
    		"===========================\n\n"
    		"Student No. \t\t Subject  \n"
    		"\t\t\t| S1  |  S2  |  S3  |  S4  |  S5  |  GPA  | \n"
            "\t\t\t|=====+======+======+======+======+=======| \n");
    	for(i=0; i<CLASS_SIZE + count; i++)
    	{
    		//total
    		printf("| %d |\n", i+1);
    
    
    		for(j=0; j<5; j++)
    		{
    			//num
    			printf("| %c |",size[i][5]);
    
    
    		    total+=num;
    			num=(double)grade;
    			if(total<lowGPA)
    			{
    				lowGPA=total;
    				index=1;
    			}
    			if(total>highGPA)
    			{
    				highGPA=total;
    				index1=1;
    			}
    			printf("| %.2f |", total/5);
    			avgGPA = total/5;
    
    
        printf("\nAverage GPA = %.2f \n", avgGPA / (CLASS_SIZE + count));
        printf("Highest GPA = %.2f obtained by Student No %d \n", highGPA, index1);
    	printf("Lowest GPA = %.2f obtained by Student No %d \n", lowGPA, index);
    
    
    	 switch(grade)
    	{
    	 case 'A': case 'a':
    		printf("Grade Point = 4.0\n");
    		break;
    	 case 'B': case 'b':
    		printf("Grade Point = 3.0\n");
    		break;
    	case 'C': case 'c':
    		 printf("Grade Point = 2.0\n");
    		 break;
    	case 'D': case'd':
    		 printf("Grade Point = 1.0\n");
    		 break;
    	case 'F': case 'f':
    		 printf("Grade Point = 0.0\n");
    		 break;
    	default:
    		 printf("Error!\n");
    	}
    }
    	}
    }
    ///////GENERATE BILL
    void Generate_Bill(){
    
    
    
    
    
    
    {
    	char programme[5];
    	double GPA, Fees, Bill, Sum;
    
    
    	FILE *BILL;
    	BILL = fopen("Bill.txt","w");
    
    
    
    
    	printf(" _______    _______                      \n"
    		"   |       |      |       |         |       \n"
    		"   |_______|      |       |         |       \n"
    		"   |_______|   ___|___    |_____    |_____  \n\n"
    		"\t\t Generate Bill \n"
    		"======================\n\n");
    	printf("Enter Your Programme :) ");
    	scanf(" %s", programme);
    	fflush(stdin);
    
    
    	printf("Enter Your Programme's Fees :) RM ");
    	scanf(" %lf", &Fees);
    	fflush(stdin);
    
    
    	printf("Enter Your GPA :)  ");
    	scanf(" %lf", &GPA);
    	fflush(stdin);
    
    
    	if(GPA>=3.85)
    	{
    		Bill = Fees * 1.00;
    		Sum = Bill;
    		printf("ScholarShip 100%% ! RM%.2f \n", Sum);
    	}
    	else if(GPA>=3.75)
    	{
    		Bill = Fees * 0.75;
    		Sum = Bill;
    		printf("ScholarShip 75%% ! RM%.2f \n", Sum);
    	}
    	else if(GPA>=3.65)
    	{
    		Bill = Fees * 0.50;
    		Sum = Bill;
    		printf("ScholarShip 50%% ! RM%.2f \n", Sum);
    	}
    	else
    	{
    		Bill = Fees;
    		Sum = Bill;
    		printf("ScholarShip NOT available..Thank You =) !\n");
    	}
    
    
        fprintf(BILL, "|BBBBBB   IIIIII LLLLLL LLLLLLL|\n");
    	fprintf(BILL, "|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|\n");
    	fprintf(BILL, "|Programme      |    %s        |\n", programme);
    	fprintf(BILL, "|Programme Fees | RM %.2f      |\n", Fees);
    	fprintf(BILL, "|ScholarShip    |    %.2f      |\n", Bill);
    	fprintf(BILL, "|Sum            | RM %.2f      |\n", Sum);
    	fprintf(BILL, "|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|\n");
    
    
    
    
    }
    }


    Is running but when i type something in the Add function there keep pop out "window exploere is stop running" ...
    and i cant find any logical error ..please help,,,

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,541
    Code:
    fflush(stdin);
    Flush stdin is NOT defined; it could be causing the crash. http://faq.cprogramming.com/cgi-bin/...&id=1043284351


    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    Tim S.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Code:
    FILE *BILL;
    BILL = fopen("Bill.txt","w");
    .
    .
    .
    Bill = Fees * 1.00;
    What? You're totally messing with the file pointer, which will surely cause a crash when you use fprintf on it later.

  4. #4
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    I think now would be a good time to remind everyone that C is case-sensitive . That said, having two variable names that differ only by case is considered bad practice; even if it isn't causing a problem here and now, it will only lead to confusion down the road.
    Consider this post signed

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Perhaps fix your compiler (to warn you about more things), then fix your code.
    Code:
    ||=== forum, Debug ===|
    main.c||In function 'Add':|
    C:\Users\sc\Documents\Projects\forum\main.c|172|warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[30]'|
    main.c|177|warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[30]'|
    main.c|187|warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[5]'|
    main.c|208|warning: unknown conversion type character '[' in format|
    main.c|208|warning: format '%c' expects type 'int', but argument 4 has type 'char *'|
    main.c|208|warning: format '%s' expects type 'char *', but argument 5 has type 'int'|
    main.c|208|warning: format '%d' expects type 'int', but argument 6 has type 'char *'|
    main.c|208|warning: format '%.2f' expects type 'double', but argument 8 has type 'int'|
    main.c|208|warning: too many arguments for format|
    main.c|227|warning: '_sleep' is deprecated (declared at c:\program files\codeblocks\mingw\bin\../lib/gcc/mingw32/4.4.1/../../../../include/stdlib.h:408)|
    main.c||In function 'Modify':|
    main.c|255|warning: unknown conversion type character '.' in format|
    main.c|255|warning: too many arguments for format|
    main.c|329|warning: '_sleep' is deprecated (declared at c:\program files\codeblocks\mingw\bin\../lib/gcc/mingw32/4.4.1/../../../../include/stdlib.h:408)|
    main.c|335|warning: unknown conversion type character '[' in format|
    main.c|335|warning: too many arguments for format|
    main.c|341|warning: unknown conversion type character '[' in format|
    main.c|341|warning: format '%c' expects type 'int', but argument 4 has type 'char *'|
    main.c|341|warning: format '%s' expects type 'char *', but argument 5 has type 'int'|
    main.c|341|warning: format '%d' expects type 'int', but argument 6 has type 'char *'|
    main.c|341|warning: format '%.2f' expects type 'double', but argument 8 has type 'int'|
    main.c|341|warning: too many arguments for format|
    main.c|349|warning: '_sleep' is deprecated (declared at c:\program files\codeblocks\mingw\bin\../lib/gcc/mingw32/4.4.1/../../../../include/stdlib.h:408)|
    main.c||In function 'Delete':|
    main.c|382|warning: format '%c' expects type 'char *', but argument 5 has type 'int'|
    main.c|382|warning: format '%d' expects type 'int *', but argument 7 has type 'int'|
    main.c|382|warning: format '%d' expects type 'int *', but argument 8 has type 'int'|
    main.c|382|warning: format '%lf' expects type 'double *', but argument 9 has type 'double'|
    main.c||In function 'Process_Examination_Report':|
    main.c|424|warning: missing braces around initializer|
    main.c|424|warning: (near initialization for 'size[0]')|
    main.c|425|warning: unused variable 'sum'|
    main.c|425|warning: unused variable 'GPA'|
    main.c|472|warning: 'grade' may be used uninitialized in this function|
    ||=== Build finished: 0 errors, 31 warnings ===|
    Messing up printf (and especially scanf) formats vs. parameters is a good way to make it crash.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Quote Originally Posted by bernt View Post
    I think now would be a good time to remind everyone that C is case-sensitive . That said, having two variable names that differ only by case is considered bad practice; even if it isn't causing a problem here and now, it will only lead to confusion down the road.
    Good lord...that's what drive-by posting will accomplish! Consider myself chastised. Your point regarding variable names is right on nonetheless

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Error
    By Luigi1 in forum C Programming
    Replies: 1
    Last Post: 02-27-2011, 01:41 PM
  2. Logical Error in Clock program
    By SVXX in forum C++ Programming
    Replies: 0
    Last Post: 05-10-2009, 12:12 AM
  3. Logical error with array
    By swgh in forum C++ Programming
    Replies: 6
    Last Post: 03-30-2009, 12:21 PM
  4. weirdest logical error i've ever run into
    By Leeman_s in forum C++ Programming
    Replies: 6
    Last Post: 05-26-2003, 10:50 AM
  5. Logical Error
    By gardenair in forum C Programming
    Replies: 2
    Last Post: 04-06-2003, 04:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21