segmentation fault upon reload

This is a discussion on segmentation fault upon reload within the C Programming forums, part of the General Programming Boards category; hello people this program stores info in two data files but when i run the program and enter data into ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    5

    segmentation fault upon reload

    hello people

    this program stores info in two data files but when i run the program and enter data into the files and then close the program, and reopen it i get a segmentation fault? can someone please help me? here is my code. thanks!

    Code:
    /*header file inclusion*/
    #include <stdio.h>				/*for I/O*/
    #include <string.h>				/*for string operation*/
    #include <ctype.h>				/*or isalpha()*/
    #include <stdlib.h>				/*for atoi and atof*/
    
    /************************************************************************/
    /* function : readstring(char *s)                                       */
    /* reads a string from the user											*/
    /* Parameter : the char array to read in								*/
    /*				read_type: 0:first read	1: second read					*/
    /* Return value : None													*/ 
    /************************************************************************/
    void readstring(char *s,int read_type){
    	char c;						/*holds the character*/
    	int i;				     	/*counter variable*/
    	fflush(stdout);              /*clears the buffers*/
        i=0;
    	if(read_type==0)			/*previous newline remains*/
    		c=getchar();			/*skip it*/
    	while((c=getchar())!='\n'){	/*reads until enter*/
            /*fflush(stdout);*/            /*flushes buffer*/	
        	if(c=='\b'){               /*backspace pressed*/
               if(i>0) {               /*not at the beginning of string*/
                  i--;                 /*reduce counter variable*/
                  /*printf("\b \b");*/      /*clears the printed char*/
               }                    /*endif*/
            }                    /*endif*/
            else{                      /*not backspace*/
                 /*printf("%c",c); */      /*print the character in the screen*/
                 s[i]=c;					/*place the character in array*/
    		     i++;					/*increase counter*/
    		}	/*end while*/
    	}
    	s[i]=0;						/*terminating the string*/
        /*printf("\n"); */        /*prints a newline*/
    
    }
    /************************************************************************/
    /* function : main()                                                    */
    /* Starting point of the program and does the actual work				*/
    /* Parameter : None														*/
    /* Return value: None													*/
    /************************************************************************/
    main(){
    	int transaction_account[20];				/*holds account no. for transaction*/
    	char transaction_description[20][31];		/*holds the description of the transaction*/
    	float transaction_amount[20];				/*holds amount for transaction*/
    	char transaction_type[20];					/*holds type of transaction debit or credit*/
    
    	int active_account[10];						/*holds active account list*/
    	char account_description[10][31];			/*description of the account*/
    	float account_balance[10];					/*holds the balance of the account*/
    
    	int choice;									/*selection from main menu*/
    	char option;								/*selection from main menu temporary*/
    	
    	int tot_transactions;						/*total number of transactions recorded*/
    	int tot_accounts;							/*total number of account encountered*/
    	
    	int ac_no;									/*temporary storage for current account number*/
    	float amount;								/*temporary storage for current amount*/
    	char type;									/*temporary storage for current transaction type*/
    	
    	int i;										/*counter variable*/
    	int j;
    	float tot_asset;							/*total balance of asset accounts*/
    	float tot_liability;						/*total balance of liability accounts*/
    	float tot_equity;							/*total balance of equity accounts*/
    	float tot_revenue;							/*total balance of revenue accounts*/
    	float tot_expense;							/*total balance of expense accounts*/
    
    	char input[100];							/*reads the user input directly*/
    	int isinvalid;								/*if an user input is invalid*/
    	int gotdot;									/*how many dots already encountered*/
    
    	FILE *fp_account;							/*pointer for handling account file*/
    	FILE *fp_transaction;						/*pointer for handling transaction file*/
    	
    	char c;										/*temporary variable used for file reading*/
    	int new_account;							/*a new account has been created*/
    
    	tot_accounts=tot_transactions=0;			/*initializing total accounts and total transactions*/
    	
    	/*checking for accounts file*/
    	fp_account=fopen("accounts.dat","rt");		
    	if(!fp_account){							/*accounts.dat does not exist*/
    		printf(" **File \"accounts.dat\" could not be read\n");	/*prompt message*/
    		printf(" **Previously entered account titles are not available\n");
    		printf(" **Any previously entered transactions will not be used\n\n");
    
    		fp_transaction=fopen("transactions.dat","wt");	/*clearing/creating transaction file*/
    		if(fp_transaction)						/*transaction file created/cleared*/
    			fclose(fp_transaction);				/*close it*/
    	}											/*end if*/
    	else{										/*account file exist*/
    		while(!feof(fp_account)){				/*read until file finishes*/
    			c=getc(fp_account);					/*reads a character*/
    			if(c==EOF)							/*End of file reached*/
    				break;							/*completed reading*/
    			i=0;								/*initialize counter*/
    			while(c!=':'){						/*read until : is found*/
    				input[i]=c;						/*store in array*/
    				c=getc(fp_account);				/*read next char*/
    				i++;							/*increase counter*/
    			}									/*end while*/
    			input[i]=0;							/*end of string*/
    			active_account[tot_accounts]=atoi(input);	/*store account name*/
    			
    			i=0;								/*initialize counter*/
    			c=getc(fp_account);					/*read a character from file*/
    			while(c!='\n'){						/*look for newline*/
    				input[i]=c;						/*store in array*/
    				c=getc(fp_account);				/*read next char*/
    				i++;							/*increment counter*/
    			}									/*end while*/
    			input[i]=0;							/*end of string*/
    			
    			strcpy(account_description[tot_accounts],input);	/*save account description*/
    			account_balance[tot_accounts]=0;	/*initialize balance*/
    			tot_accounts++;						/*increase total number of accounts encountered*/
    		}										/*end else*/
    		fclose(fp_account);						/*closing account file*/
    		
    		fp_transaction=fopen("transactions.dat","rt");	/*opening transactions file*/
    		if(fp_transaction){						/*transaction file opened*/
    			while(!feof(fp_transaction)){		/*read until file ends*/
    				c=getc(fp_transaction);			/*read the first character*/
    				if(c==EOF)						/*end of file reached*/
    					break;						/*reading done*/
    				i=0;							/*initialize counter*/
    				while(c!=':'){					/*read until : is found*/
    					input[i]=c;					/*store in array*/
    					c=getc(fp_transaction);		/*read next char*/
    					i++;						/*increase counter*/
    				}								/*end while*/
    				input[i]=0;						/*end of string*/
    				transaction_account[tot_transactions]=atoi(input);	/*load transaction account*/
    				
    				i=0;							/*initialize counter*/
    				c=getc(fp_transaction);			/*read a char*/
    				while(c!=':'){				/*read until : is found*/
    					input[i]=c;				/*load in array*/
    					c=getc(fp_transaction);	/*read next char*/
    					i++;					/*increment counter*/
    				}							/*end while*/
    				input[i]=0;					/*end of string*/
    				strcpy(transaction_description[tot_transactions],input);	/*load transaction description*/
    				
    				c=getc(fp_transaction);			/*read next char*/
    				transaction_type[tot_transactions]=c;	/*load transaction type*/
    				c=getc(fp_transaction);			/*skip next :*/
    				
    				i=0;							/*initialize counter*/
    				c=getc(fp_transaction);			/*read a char*/
    				while(c!='\n'){					/*read until newline*/
    					input[i]=c;					/*load in array*/
    					c=getc(fp_account);			/*read next char*/
    					i++;						/*increment counter*/
    				}								/*end while*/
    				input[i]=0;						/*end of string*/
    				transaction_amount[tot_transactions]=atof(input);	/*load transaction amount*/
    			
    				/*updating account balance*/
    				for(j=0;j<tot_accounts;j++){		/*search all accounts	*/
    					if(active_account[j]==transaction_account[tot_transactions]){	/*corresponing account found*/
    						if(transaction_type[tot_transactions]=='d')					/*debit transaction*/
    							account_balance[j]+=transaction_amount[tot_transactions];	/*increase balance*/
    						else														/*credit transaction*/
    							account_balance[j]-=transaction_amount[tot_transactions];	/*decrement balance*/
    					}		/*end if*/
    				}			/*end for*/
    
    				tot_transactions++;	/*increase no.of transactions encountered*/
    			}				/*end while*/
    			fclose(fp_transaction);		/*close transaction file*/
    		}					/*end if*/
    	}						/*end else*/
    
    	while(1){									/*main menu repeats forever*/
    		/*printing the main menu*/
    		printf("Options available:\n\n");		
    		printf("1 - Enter a transaction\n");
    		printf("2 - View the general ledger\n");
    		printf("3 - View the balance sheet\n");
    		printf("4 - View the income statement\n");
    		printf("q - Quit the program\n\n");
    		
    		/*prompting user for choice*/
    		printf("Please enter 1, 2, 3, 4, or q: ");
    		/*user choice*/
    		while(1){								/*repeat until correctly entered*/
    			scanf("%s",input);					/*reads in input*/
    			if(strlen(input)>1){				/*more than one characters*/
    				printf(" **Trailing character encountered\n");	/*print error message*/
    				printf(" **Please reenter : ");					/*prompt for reenter*/
    			}
    			else{								/*single character entered*/
    				option=input[0];				/*store the character in option*/
    				if(option=='q' || option=='Q'){	/*user choose to quit*/
    					choice=5;					/*choice = 4 indicates quit*/
    					break;
    				}
    				else if(option > '0' && option < '5' ){	/*user chooses from 1 to 3*/
    					choice=option-'0';			/*convert to corresponding digit*/
    					break;
    				}
    				else{							/*any other character than 1, 2, 3, or q*/
    					printf(" **Invaid input\n");		/*print error message*/
    					printf(" **Please reenter : ");		/*prompt for reenter*/
    				}		/*end else*/
    			}			/*end else*/
    		}				/*end while*/
    
    		/*take action based on choice*/
    		switch(choice){
    		case 5:							/*user choose to quit*/
    			return;						/*quit program*/
    
    		case 1:							/*insert a transaction*/
    			new_account=0;
    			if(tot_transactions==20){	/*maximum transaction already done*/
    				 printf(" **Maximum number of transactions has been entered\n");
    				 break;					/*can not do more transaction*/
    			}
    			/*getting the account number*/
    			printf("\nPlease insert account number (between 1000 and 5999) : ");
    			while(1){					/*read the account number*/
    				scanf("%s",input);		/*read in input*/
    				isinvalid=0;			/*initialize isinvalid*/
    				if(isalpha(input[0])){	/*first digit is alphabet*/
    					printf(" **Invaid input\n");	/*print error message*/
    					printf(" **Please reenter : ");	/*prompt to reenter*/
    				}
    				else{
    					for(i=0;i < strlen(input); i++){	/*check the whole string*/
    						if(isalpha(input[i]) || input[i]=='.'){		/*alphabet or dot found*/
    							isinvalid=1;				/*mark invalid*/
    							printf(" **Trailing character encountered\n");	/*print error message*/
    							printf(" **Please reenter : ");	/*prompt to reenter*/
    							break;
    						}
    					}
    					if(!isinvalid){				/*it is an integer*/
    						ac_no=atoi(input);		/*convert to integer*/
    						if(ac_no < 1000 || ac_no > 5999){	/*out of range*/
    							printf(" **Out of range\n");	/*print error message*/
    							printf(" **Please reenter : ");	/*prompt to reenter*/
    						}
    						else					/*valid input found*/
    							break;
    					}			/*end if*/
    				}				/*end else*/
    			}					/*end while*/
    
    			/*checking for new account*/
    			for(i=0;i<tot_accounts;i++){		/*checking all accounts*/
    				if(active_account[i]==ac_no){	/*found in existing*/
    					break;
    				}	/*end if*/
    			}		/*end for*/
    			if(i==tot_accounts){				/*new account found*/
    				if(tot_accounts==10){			/*maximum number of accounts are already there*/
    					printf(" **Maximum number of accounts has been entered\n"); /*print error message*/
    					break;
    				}		/*end if*/
    				new_account=1;
    				printf("\nEnter title for new account %d: ",ac_no);		/*user inputs title of account*/
    				while(1){			/*repeat until correctly entered*/
    					readstring(input,0);		/*reads a line*/
    					if(strlen(input) > 30){		/*too long string*/
    						printf(" **Title entered is longer than 30 characters\n");	/*print error message*/
    						printf(" **Please reenter : ");		/*prompt for reenter*/
    					}			/*end if*/
    					else{		/*correctly entered*/
    						strcpy(account_description[tot_accounts],input);	/*store account description*/
    						break;		/*o need to repeat		*/	
    					}			/*end else*/
    				}			/*end while*/
    			}			/*end for*/
    
    			printf("\nEnter title for transaction: ");		/*user input title*/
    			while(1){										/*repeat until correctly entered*/
    				if(new_account==0)
    					readstring(input,0);							/*reads a line*/
    				else
    					readstring(input,1);
    
    				if(strlen(input) > 30){						/*too long string*/
    					printf(" **Title entered is longer than 30 characters\n");	/*print error message*/
    					printf(" **Please reenter : ");				/*prompt for reenter*/
    				}			/*end if*/
    				else{		/*valid string*/
    					strcpy(transaction_description[tot_transactions],input);	/*store account description*/
    					break;		/*no need to report*/
    				}				/*end else*/
    			}					/*end while*/
    			
    			/*getting the account type*/
    			printf("\nEnter d (debit) or c (credit): ");	/*prompt the user*/
    			while(1){										/*reading the input*/
    				scanf("%s",input);							/*loading in input*/
    				if(strlen(input)>1){						/*more than one characters entered*/
    					printf(" **Trailing character encountered\n"); /*print error message*/
    					printf(" **Please reenter : ");	/*prompt to reenter*/
    				}			/*end if*/
    				else if(input[0]=='c' || input[0]=='C'){	/*credit transaction*/
    					type='c';								/*setting type of transaction to credit*/
    					break;
    				}		/*end else if*/
    				else if(input[0]=='d' || input[0]=='D'){	/*debit transaction*/
    					type='d';								/*setting type of transaction to debit*/
    					break;
    				}		/*end else if*/
    				else{										/*anything other than c or d*/
    					printf(" **Invaid input\n");	/*print error message*/
    					printf(" **Please reenter : ");	/*prompt to reenter*/
    				}		/*end else*/
    			}			/*end while*/
    			
    			/*getting transaction amount*/
    			printf("\nEnter transaction amount: ");		/*prompt the user*/
    			while(1){									/*repeat until valid input*/
    				scanf("%s",input);						/*reading in input*/
    				isinvalid=0;							/*initialize isinvalid*/
    				gotdot=0;								/*dot encountered 0*/
    				if(isalpha(input[0])){					/*first character is alphabet*/
    					printf(" **Invaid input\n");	/*print error message*/
    					printf(" **Please reenter : ");	/*prompt to reenter*/
    				}									/*end if*/
    				else{								/*first character is number*/
    					for(i=0;i<strlen(input);i++){	/*check the entire string*/
    						if(isalpha(input[i])){		/*is the digit alphabetic*/
    							isinvalid=1;			/*invalid number found*/
    							printf(" **Trailing character encountered\n");	/*print error message*/
    							printf(" **Please reenter : ");		/*prompt to reenter*/
    							break;
    						}							/*end if*/
    						if(input[i]=='.'){			/*found dot*/
    							gotdot++;				/*increase number of dots*/
    							if(gotdot==2){			/*more than one dot*/
    								isinvalid=1;		/*number is invalid*/
    								printf(" **Trailing character encountered\n");	/*print error message*/
    								printf(" **Please reenter : ");		/*prompt to reenter*/
    								break;
    							}		/*end if*/
    						}			/*end if*/
    					}				/*end for*/
    					if(!isinvalid){	/*for not invalid numbers*/
    						amount=(float)atof(input);		/*convert to float*/
    						if(amount < 0){			/*negative number inserted*/
    							printf(" **Out of range\n");	/*print error message*/
    							printf(" **Please reenter : ");	/*prompt to reenter*/
    						}		/*end if*/
    						else	/*valid number found*/
    							break;
    					}			/*end if*/
    				}				/*end else*/
    			}					/*end while*/
    					
    			/*storing the transaction*/
    			transaction_account[tot_transactions]=ac_no;		/*store the transaction acc no.*/
    			transaction_type[tot_transactions]=type;			/*store the transaction acc type*/
    			transaction_amount[tot_transactions]=amount;		/*store the transaction amount*/
    			tot_transactions++;									/*increase total transactions*/
    			for(i=0;i<tot_accounts;i++){						/*check all accounts*/
    				if(active_account[i]==ac_no){					/*the current account found*/
    					if(type=='c'){								/*credit transaction*/
    						account_balance[i]-=amount;				/*update balance*/
    						break;
    					}		/*end if*/
    					else if(type=='d'){							/*debit transaction*/
    						account_balance[i]+=amount;				/*update balance*/
    						break;
    					}		/*end else*/
    				}			/*end if*/
    			}				/*end for*/
    			if(i==tot_accounts){			/*new account encountered*/
    				active_account[tot_accounts]=ac_no;		/*save account no. of new account*/
    				if(type=='c')							/*credit transaction*/
    					account_balance[tot_accounts]=0-amount;		/*initialize balance*/
    				else									/*debit transaction*/
    					account_balance[tot_accounts]=amount;	/*initialize balance*/
    				tot_accounts++;							/*increase total number of accounts*/
    				
    				fp_account=fopen("accounts.dat","at");		/*open account file in append mode*/
    				sprintf(input,"%d",active_account[tot_accounts-1]);	/*write account no. in string*/
    				fprintf(fp_account,"%s:",input);	/*write account no. to file*/
    				fprintf(fp_account,"%s\n",account_description[tot_accounts-1]);	/*writing account description to file*/
    				fclose(fp_account);			/*close account file*/
    			}				/*end if*/
    
    			fp_transaction=fopen("transactions.dat","at");		/*open transaction file in append mode*/
    			if(!fp_transaction){		/*transaction file can not be opened for writing*/
    				printf(" **File \"transactions.dat\" could not be written\n");		/*prompt message*/
    				printf(" **Transaction title \"%s\"  will not be saved\n",transaction_description[tot_transactions-1]);
    			}		/*end if*/
    			else{		/*transaction file opened*/
    				sprintf(input,"%d",transaction_account[tot_transactions-1]);	/*write account no. in string*/
    				fprintf(fp_account,"%s:",input);			/*write account no. to file*/
    				fprintf(fp_transaction,"%s:",transaction_description[tot_transactions-1]);	/*write description to file*/
    				fprintf(fp_transaction,"%c:",transaction_type[tot_transactions-1]);		/*write transaction type*/
    				sprintf(input,"%.2f",transaction_amount[tot_transactions-1]);		/*write transaction amount to string*/
    				fprintf(fp_account,"%s\n",input);	/*write transaction amount to file*/
    				fclose(fp_transaction);				/*close transaction file*/
    			}										/*end else*/
    			break;			/*case 1 ends*/
    		
    		case 2:				/*show general ledger*/
    			printf("\n\t\t\tGeneral Journal\n");	/*header for ledger*/
    			printf("Account Description                          Debit      Credit\n");	
    			printf("------- ------------------------------ ----------- -----------\n");
    			for(i=0;i<tot_transactions;i++){		/*for each transaction*/
    				printf("%7d",transaction_account[i]);		/*print account number*/
    				printf(" %s",transaction_description[i]);		/*printing description*/
    				for(j=30-strlen(transaction_description[i]);j>=0;j--)	/*printing blanks*/
    					printf(" ");
    				if(transaction_type[i]=='d')				/*debit*/
    					printf("%11.2f\n",transaction_amount[i]);	/*print at debit column*/
    				else{										/*credit*/
    					printf("            %11.2f\n",transaction_amount[i]);	/*print at credit column*/
    				}
    			}				/*end for*/
    			break;			/*case 2 ends*/
    		
    		case 3:				/*show balance sheet*/
    			tot_asset=tot_liability=tot_equity=0;	/*initialize asset, liability and equity*/
    			printf("\n\t\t\tBalance Sheet\n");		/*header for balance sheet*/
    			printf("Account Description                          Debit      Credit\n");
    			printf("------- ------------------------------ ----------- -----------\n");
    			for(i=0;i<tot_accounts;i++){			/*for each transaction*/
    				if(active_account[i]>=1000 && active_account[i]<=1999){		/*asset accounts*/
    					tot_asset+=account_balance[i];		/*update total asset*/
    					printf("%7d",active_account[i]);	/*print account number*/
    					printf(" %s",account_description[i]);		/*printing description*/
    					for(j=30-strlen(account_description[i]);j>=0;j--)	/*printing blanks*/
    						printf(" ");
    					if(account_balance[i]>0)						/*debit*/
    						printf("%11.2f\n",account_balance[i]);	/*print at debit column*/
    					else											/*credit*/
    						printf("            %11.2f\n",(-1)*account_balance[i]);	/*print at credit column*/
    				}		/*end if*/
    			}			/*end for*/
    			printf("                                       ----------- -----------\n");
    			printf("          Total Assets                 ");		/*total of asset accounts*/
    			if(tot_asset>0)			/*debit major*/
    				printf("%11.2f\n\n",tot_asset);		/*print at debit column*/
    			else						/*credit major*/
    				printf("            %11.2f\n\n",(-1)*tot_asset);		/*print at credit column*/
    
    
    			for(i=0;i<tot_accounts;i++){			/*for each transaction*/
    				if(active_account[i]>=2000 && active_account[i]<=2999){		/*liability accounts*/
    					tot_liability+=account_balance[i];		/*update total liability*/
    					printf("%7d",active_account[i]);		/*print account number*/
    					printf(" %s",account_description[i]);
    					for(j=30-strlen(account_description[i]);j>=0;j--)	/*printing blanks*/
    						printf(" ");
    					if(account_balance[i]>0)		/*debit*/
    						printf("%11.2f\n",account_balance[i]);	/*print at debit column*/
    					else							/*credit*/
    						printf("            %11.2f\n",(-1)*account_balance[i]);	/*print at credit column*/
    				}		/*end if*/
    			}			/*end for*/
    			printf("                                       ----------- -----------\n");
    			printf("          Total Liabilities            ");		/*total liabilities printing*/
    			if(tot_liability>0)	/*debit major*/
    				printf("%11.2f\n\n",tot_liability);		/*print at debit column*/
    			else					/*credit major*/
    				printf("            %11.2f\n\n",(-1)*tot_liability);	/*print at credit column*/
    
    
    			for(i=0;i<tot_accounts;i++){			/*for each transaction*/
    				if(active_account[i]>=3000 && active_account[i]<=3999){		/*equity accounts*/
    					tot_equity+=account_balance[i];			/*update total equity*/
    					printf("%7d",active_account[i]);		/*print account number*/
    					printf(" %s",account_description[i]);		/*printing description*/
    					for(j=30-strlen(account_description[i]);j>=0;j--)	/*printing blanks*/
    						printf(" ");
    					if(account_balance[i]>0)	/*debit*/
    						printf("%11.2f\n",account_balance[i]);	/*print at debit column*/
    					else						/*credit*/
    						printf("            %11.2f\n",(-1)*account_balance[i]);		/*print at credit column*/
    				}		/*end if*/
    			}			/*end for*/
    			printf("                                       ----------- -----------\n");
    			printf("          Total Equity                 ");		/*printing total equity*/
    			if(tot_equity>0)		/*debit major*/
    				printf("%11.2f\n",tot_equity);		/*print at debit column*/
    			else					/*credit major*/
    				printf("            %11.2f\n",(-1)*tot_equity);		/*print at credit column*/
    			
    			printf("                                       ----------- -----------\n");
    			printf("          Total Liabilities and Equity ");		/*total liabilities and equity printing*/
    
    			if((tot_equity+tot_liability)>0)	/*debit major*/
    				printf("%11.2f\n",(tot_equity+tot_liability));	/*print at debit column*/
    			else								/*credit major*/
    				printf("            %11.2f\n",(-1)*(tot_equity+tot_liability));		/*print at credit column*/
    
    			break;		/*case 3 ends*/
    
    		case 4:							/*printing income statement*/
    			tot_revenue=tot_expense=0;	/*initialize asset, liability and equity*/
    			printf("\n\t\t\tIncome Statement\n");		/*header for balance sheet*/
    			printf("Account Description                          Debit      Credit\n");
    			printf("------- ------------------------------ ----------- -----------\n");
    			for(i=0;i<tot_accounts;i++){			/*for each transaction*/
    				if(active_account[i]>=4000 && active_account[i]<=4999){		/*revenue accounts*/
    					tot_revenue+=account_balance[i];		/*update total revenue*/
    					printf("%7d",active_account[i]);		/*print account number*/
    					printf(" %s",account_description[i]);		/*printing description*/
    					for(j=30-strlen(account_description[i]);j>=0;j--)	/*printing blanks*/
    						printf(" ");
    					if(account_balance[i]>0)		/*debit*/
    						printf("%11.2f\n",account_balance[i]);	/*print at debit column*/
    					else							/*credit*/
    						printf("            %11.2f\n",(-1)*account_balance[i]);	/*print at credit column*/
    				}		/*end if*/
    			}			/*end for*/
    			printf("                                       ----------- -----------\n");
    			printf("          Total Revenue                ");		/*total liabilities printing*/
    			if(tot_revenue>0)	/*debit major*/
    				printf("%11.2f\n\n",tot_revenue);		/*print at debit column*/
    			else					/*credit major*/
    				printf("            %11.2f\n\n",(-1)*tot_revenue);	/*print at credit column*/
    
    
    			for(i=0;i<tot_accounts;i++){			/*for each transaction*/
    				if(active_account[i]>=5000 && active_account[i]<=5999){		/*equity accounts*/
    					tot_expense+=account_balance[i];			/*update total expense*/
    					printf("%7d",active_account[i]);		/*print account number*/
    					printf(" %s",account_description[i]);		/*printing description*/
    					for(j=30-strlen(account_description[i]);j>=0;j--)	/*printing blanks*/
    						printf(" ");
    					if(account_balance[i]>0)	/*debit*/
    						printf("%11.2f\n",account_balance[i]);	/*print at debit column*/
    					else						/*credit*/
    						printf("            %11.2f\n",(-1)*account_balance[i]);		/*print at credit column*/
    				}		/*end if*/
    			}			/*end for*/
    			printf("                                       ----------- -----------\n");
    			printf("          Total Expense                ");		/*printing total equity*/
    			if(tot_expense>0)		/*debit major*/
    				printf("%11.2f\n",tot_expense);		/*print at debit column*/
    			else					/*credit major*/
    				printf("            %11.2f\n",(-1)*tot_expense);		/*print at credit column*/
    			
    			printf("                                       ----------- -----------\n");
    			printf("          Net Income                   ");		/*total liabilities and equity printing*/
    
    			if((tot_expense+tot_revenue)>0)	/*debit major*/
    				printf("%11.2f\n",(tot_expense+tot_revenue));	/*print at debit column*/
    			else								/*credit major*/
    				printf("            %11.2f\n",(-1)*(tot_expense+tot_revenue));		/*print at credit column*/
    
    			break;		/*case 4 ends*/	
    		}				/*switch ends*/
    		printf("\n\n");	/*printing two line space*/
    	}					/*end main while loop*/
    }						/*end main function*/

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Would you mind posting the shortest code that reproduces your error ?

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    1. int main()
    2. Why it's bad to use feof() to control a loop
    3. you don't check the bounds of the input array
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    while(c!='\n'){						/*look for newline*/
    				input[i]=c;						/*store in array*/
    				c=getc(fp_account);				/*read next char*/
    				i++;							/*increment counter*/
    			}									/*end while*/
    			input[i]=0;							/*end of string*/
    			
    			strcpy(account_description[tot_accounts],input);
    why not
    Code:
    fgets(account_description[tot_accounts], sizeof(account_description[tot_accounts]),fp_account);
    ?
    you will avoid overwriting both input array and account_description[tot_accounts] array
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    5
    so i should just replace that and it will work?

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by yabud
    so i should just replace that and it will work?
    not exactly - after fgets you should scan the string for '\n' symbol if it is present and replace it by the '\0'
    The sample can be found in the FAQ...

    For your crash issue - there is too much code that can bring the problem, so I don't know if that is the main problem, or just one of the many I noted on the quick look...

    As suggested - you should determine the smallest possible sample representing your crash and post just it.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    5
    the problem is im not exactly sure why its causing a segmentation fault. it only occurs when i enter data to the files and then close the program, reopen and try to view...

    and sorry so to confirm i should replace that with

    Code:
    fgets(account_description[tot_accounts], sizeof(account_description[tot_accounts]),fp_account);
    followed by what in code?
    Last edited by yabud; 12-18-2006 at 05:41 AM.

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    look for the sample usage of fgets
    and specially the code after the phrase
    "Now test for, and remove that newline character"

    also you maybe interested in the sample:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    OPTION 3 Read As A String and Convert

    Just note that in your case you're reading from the file, not from the stdin
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    >the problem is im not exactly sure why its causing a segmentation fault
    So use the debugger then.

    gcc -g prog.c
    gdb a.out
    run

    The debugger will catch the segfault and halt the program at that point, for you to investigate what went wrong.

    On another note, learn how to use functions.
    A 500+ line main program is REALLY hard to navigate even with good indentation.
    For example
    - reading and writing each of the files should be separate functions (4 in all)
    - each of your 4 main menu actions should be separate functions (4 in all)
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 06:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 01:53 PM

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