unsigned long long exec_insert_stmt(MYSQL_STMT **stmt, MYSQL_BIND *parameters) {
	
	int state;
	int connection;
	MYSQL_STMT *local;
	unsigned long long result;
	
	connection = db_get_conn();
	if (connection < 0) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("Unable to get an available connection for the query.");
		#endif
		return 0;
	}
		
	local = *(stmt + connection);
	if (local == NULL) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("Unable to get a valid prepared statement pointer.");
		#endif
		db_free_conn(connection);
		return 0;
	}
	
	if (reset_stmt(local) != 1) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("Unable to get a reset the prepared statement.");
		#endif
		db_free_conn(connection);
		return 0;
	}
	
	if (bind_param_stmt(local, parameters) != 1) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("Unable to bind the parameters to the prepared statement.");
		#endif
		db_free_conn(connection);
		return 0;
	}
	
	state = mysql_stmt_execute_d(local);
	
	if (state != 0) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("An error occurred while executing a prepared statement. %s", mysql_stmt_error_d(local));
		#endif
		db_free_conn(connection);
		return 0;
	}
	
	result = mysql_stmt_insert_id_d(local);
	#ifdef DEBUG_FRAMEWORK
	if (result == 0) {
		lavalog("We did not get back a valid insert id. %s", mysql_stmt_error_d(local));
	}
	#endif
	
	db_free_conn(connection);
	return result;
}
/*remove files of members too*/
int delete_grp(char *grpname, char * grppass,char * mem[16], char * pass[16], int * num_mem, struct name* arruser[16]){
	int j;
	int i;
	printf(" you have chosen to delete the group %s .\n", grpname);
	printf(" please enter 1 to confirm, 0 to discontinue\n");
	scanf("%d",&j);
	if(j == 0){
		return 2;
	}
	for(i = 0; i< (*num_mem); i++){
		 remove(mem[i]);
	}
	grpname[0] = '\0';
	grppass[0] = '\0';
	for(i = 0; i < (*num_mem); i++){
		free(mem[i]);
		free(pass[i]);
		mem[i] = NULL;
		pass[i] = NULL;
	}
	reset_data(arruser);
	reset_stmt(arruser);
	(*num_mem) = 0;
	return 1;
}
sql_result_t * exec_query_res_stmt(MYSQL_STMT **stmt, MYSQL_BIND *parameters) {
	
	int connection;
	MYSQL_STMT *local;
	sql_result_t *result;
	
	connection = db_get_conn();
	if (connection < 0) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("Unable to get an available connection for the query.");
		#endif
		return NULL;
	}
	
	local = *(stmt + connection);
	if (local == NULL) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("Unable to get a valid prepared statement pointer.");
		#endif
		db_free_conn(connection);
		return NULL;
	}
	
	if (reset_stmt(local) != 1) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("Unable to get a reset the prepared statement.");
		#endif
		db_free_conn(connection);
		return NULL;
	}
	
	if (bind_param_stmt(local, parameters) != 1) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("Unable to bind the parameters to the prepared statement.");
		#endif
		db_free_conn(connection);
		return NULL;
	}
	
	if (mysql_stmt_execute_d(local) != 0) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("An error occurred while executing a prepared statement. %s", mysql_stmt_error_d(local));
		#endif
		db_free_conn(connection);
		return NULL;
	}
	
	result = store_db_result(local);
	if (result == NULL) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("Unable to store the database result.");
		#endif
		db_free_conn(connection);
		return NULL;
	}
	
	db_free_conn(connection);
	return result;
}
unsigned long long exec_query_rows_stmt_tran(MYSQL_STMT **stmt, MYSQL_BIND *parameters, const int transaction) {
	
	MYSQL_STMT *local;
	unsigned long long rows;
	
	local = *(stmt + transaction);
	if (local == NULL) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("Unable to get a valid prepared statement pointer.");
		#endif
		return 0;
	}
	
	if (reset_stmt(local) != 1) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("Unable to get a reset the prepared statement.");
		#endif
		return 0;
	}
	
	if (bind_param_stmt(local, parameters) != 1) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("Unable to bind the parameters to the prepared statement.");
		#endif
		return 0;
	}
	
	if (mysql_stmt_execute_d(local) != 0) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("An error occurred while executing a prepared statement. %s", mysql_stmt_error_d(local));
		#endif
		return 0;
	}
	
	if (mysql_stmt_store_result_d(local) != 0) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("An error occurred while attempting to buffer a prepared statement result set. %s", mysql_stmt_error_d(local));
		#endif
		return 0;
	}
	
	// Store the number of rows.
	rows = mysql_stmt_num_rows_d(local);
	
	#ifdef DEBUG_FRAMEWORK
	if (mysql_stmt_free_result_d(local) != 0) {
		lavalog("An error occurred while attempting to free a prepared statment result buffer. %s", mysql_stmt_error_d(local));
	}
	#else
	mysql_stmt_free_result_d(local);
	#endif
	
	return rows;
}
unsigned long long exec_insert_stmt_tran(MYSQL_STMT **stmt, MYSQL_BIND *parameters, const int transaction) {
	
	int state;
	MYSQL_STMT *local;
	unsigned long long result;
		
	local = *(stmt + transaction);
	if (local == NULL) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("Unable to get a valid prepared statement pointer.");
		#endif
		return 0;
	}
	
	if (reset_stmt(local) != 1) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("Unable to get a reset the prepared statement.");
		#endif
		return 0;
	}
	
	if (bind_param_stmt(local, parameters) != 1) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("Unable to bind the parameters to the prepared statement.");
		#endif
		return 0;
	}
	
	state = mysql_stmt_execute_d(local);
	
	if (state != 0) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("An error occurred while executing a prepared statement. %s", mysql_stmt_error_d(local));
		#endif
		return 0;
	}
	
	result = mysql_stmt_insert_id_d(local);
	#ifdef DEBUG_FRAMEWORK
	if (result == 0) {
		lavalog("We did not get back a valid insert id. %s", mysql_stmt_error_d(local));
	}
	#endif
	
	return result;
}
sql_result_t * exec_query_res_tran_stmt(MYSQL_STMT **stmt, MYSQL_BIND *parameters, const int transaction) {

	MYSQL_STMT *local;
	sql_result_t *result;
	
	local = *(stmt + transaction);
	if (local == NULL) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("Unable to get a valid prepared statement pointer.");
		#endif
		return NULL;
	}
	
	if (reset_stmt(local) != 1) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("Unable to get a reset the prepared statement.");
		#endif
		return NULL;
	}
	
	if (bind_param_stmt(local, parameters) != 1) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("Unable to bind the parameters to the prepared statement.");
		#endif
		return NULL;
	}
	
	if (mysql_stmt_execute_d(local) != 0) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("An error occurred while executing a prepared statement. %s", mysql_stmt_error_d(local));
		#endif
		return NULL;
	}
	
	result = store_db_result(local);
	if (result == NULL) {
		return NULL;
	}
	
	return result;
}
int exec_query_stmt_tran(MYSQL_STMT **stmt, MYSQL_BIND *parameters, const int transaction) {
	
	int state;
	MYSQL_STMT *local;
	
	local = *(stmt + transaction);
	if (local == NULL) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("Unable to get a valid prepared statement pointer.");
		#endif
		return -1;
	}
	
	if (reset_stmt(local) != 1) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("Unable to get a reset the prepared statement.");
		#endif
		return -1;
	}
	
	if (bind_param_stmt(local, parameters) != 1) {
		#ifdef DEBUG_FRAMEWORK
		lavalog("Unable to bind the parameters to the prepared statement.");
		#endif
		return -1;
	}
	
	state = mysql_stmt_execute_d(local);
	
	#ifdef DEBUG_FRAMEWORK
	if (state != 0) {
		lavalog("An error occurred while executing a prepared statement. %s", mysql_stmt_error_d(local));
	}
	#endif
	
	return state;
}
int main(){
/* this function is the actual main function in my program*/
	struct name * arruser[16];
	int num_mem;/*this is the total number of members in the group*/
	char * mem[16];/* this the array which stores addresses of usernames*/
	char * pass[16];/* this array stores addresses of respective passwords*/
	char grpname[10];/* char array containing the group name*/
	char grppass[10]; /* this array contains the password of the group*/
	int user;/* this is the current user that has logged in( users index in the array)*/
	int i, j, k;/* these are some random variables to be used as counters.*/
	int state = 1;/* this acs like flag between two different switch cases which go on in an infinite loop wher only few condition can break out of it depending on the value of this flag.*/
	int choice; /*this variable selects the case in switch*/
	struct data * curr1;
	struct list * curr2;
	char buffer[10];
	char * buffer1;
	/*now to start calling functions one by one:*/
	printf("Loading...\n");
	print_line();
	j = load_file(grpname, grppass, mem, &num_mem, pass, arruser);
	if(j == 0){
	 printf(" the standard file could not be opened\n you can still go ahead with working but previous data is lost now\n");
	 }
	 if(j == 2){
	 printf(" Looks like you are a new user!\n welcome to splitwise!\n");
	 }
	 if(j == 1){
	 printf("All previous user data is now loaded!\n welcome back to splitwise!\n");
	 }
	 if(j == 4){
	 	printf(" welcome back to splitwise!\n you already have a group created\n but there are no members in this group\n please add yourself to the group first\n");
	 	printf(" please enter your username\n");
	 	scanf(" %s", buffer);
	 	mem[0] = (char * ) malloc(10);
	 	if(mem[0] == NULL){
	 		printf(" not enough memory\n");
	 		exit(1);
	 	}
	 	strcpy(mem[0], buffer);
	 	buffer1 = getpass(" hello, please enter you password\n");
	 	pass[0] = (char*)malloc(10);
	 	if(pass[0] == NULL){
	 		printf(" not enough memory\n");
	 		exit(1);
	 	}
	 	strcpy(pass[0], buffer1);
	 	printf(" you are now added to the group %s \n welcome!\n", grpname);
	 	arruser[0] = (struct name*)malloc(sizeof(struct name));
	 	arruser[0]->q = NULL;
	 	arruser[0]->p = NULL;
	 	arruser[0]->num_data = 0;
	 	arruser[0]->num_stmt = 0;
	 }
	 print_line();
	 print_home();
	 scanf("%d", &choice);
	 while(1){
	 	
	 	if(state == 1){
	 		switch(choice){
	 			case 1: 
	 				if(num_mem == 0){
	 					printf(" there aren't any group members\n");
	 					create_grp(grpname, grppass,mem, pass, &num_mem,arruser); 
	 					print_home();
	 					scanf("%d", &choice);
	 					print_line();
	 					state = 1;
	 					/* check the return values*/
	 					break;
	 				}
	 				j = login(mem, pass, &user);
	 				print_line();
	 				if(j == 0){
	 					state = 3;
	 					break;
	 				}	
	 				if(j == 1){
	 					print_menu();
	 					scanf("%d", &choice);
	 					print_line();
	 					state = 2;
	 				}
	 				break;
	 			case 2: 
					j = check_grp(grpname);
					if(j == 0){
						/* no group exists*/
						k = create_grp(grpname, grppass, mem, pass, &num_mem, arruser);
						if(k == 0){
							printf(" creating group failed\n");
							state = 3;
						}
						if(k == 1){
							print_home();
	 						scanf("%d", &choice);
							print_line();
							state = 1;
						}
					}
					else{
						printf("A group alredy exists. you cannot create multiple groups. if youi want to create a new group the current group will have to be deleted.\n press 1 to delete existing group\n press 2 to exit\n");
						scanf("%d", &k);
						print_line();
						if(k == 1){
							delete_grp(grpname, grppass, mem, pass, &num_mem, arruser);
							i = create_grp(grpname, grppass, mem, pass, &num_mem,arruser);
							if(i == 0){
							printf(" creating group failed\n");
							state = 3;
							}
							if(i == 1){
								print_home();
	 							scanf("%d", &choice);
								print_line();
								state = 1;
							}
						}
						if(k == 2){
						print_home();
						scanf("%d", &choice);
							print_line();
							state  = 1;
						}
					}
					break;
				
				case 3:
					j = check_grp(grpname);
					if(j == 0){
					;
					}
					else{
						delete_grp(grpname, grppass, mem, pass, &num_mem, arruser);
					}
					print_home();
					scanf("%d", &choice);
					print_line();
					state = 1;
					break;
				
				case 4: delete_member(grpname, grppass, mem, pass, &num_mem, arruser);
					print_home();
					scanf("%d", &choice);
					print_line();
					state = 1;
					break;
				case 5:
					state = 3;
					break;
					
				default:
						printf(" you have entered an invalid choice\n please enter again\n");
						print_home();
						scanf("%d", &choice);
						print_line();
						state = 1;
					break;
			}/* switch closed*/
		}/* if closed*/
		if( state == 2){
			switch(choice){
				case 1:
					j = add_expense(user, arruser);
					if(j == 0){
						printf(" sorry could not add any more expenses\n");
						print_menu();
	 					scanf("%d", &choice);
						print_line();
						state  = 2;
					}
					if(j == 1){
						print_menu();
	 					scanf("%d", &choice);
						print_line();
						state = 2;
					}
				break;
				 case 2:
				 	j = get_statement(user, arruser);
				 	if(j == 0){
				 		printf(" sorry, the file could no be opened\n");
				 	}
				 	print_menu();
	 				scanf("%d", &choice);
				 	print_line();
				 	state = 2;
				 	break;
				 	
				 case 3:
				 	j = split_expense(mem, arruser);
				 	print_menu();
	 				scanf("%d", &choice);
				 	print_line();
				 	state = 2;
				 	break;
				 
				 case 4:
				 	view_my_contri(user, arruser);
				 	print_menu();
	 				scanf("%d", &choice);
				 	print_line();
				 	state = 2;
				 	break;
				 	
				 case 5:
				 	j = split_expense(mem, arruser);
				 	if( j == 0){
						print_menu();
	 					scanf("%d", &choice);
						print_line();
				 		/* unsucessful*/
				 		state = 2;
				 		break;
				 	}
				 	j = add_member(grpname, grppass, mem, pass, &num_mem, arruser);
				 	if (j == 0){
				 	/* unsuccessful*/
				 		printf(" adding member was unsuccessful\n please try again\n");
				 		print_menu();
	 					scanf("%d", &choice);
				 		print_line();
				 		state = 2;
				 		break;
				 	}
				 	print_menu();
	 				scanf("%d", &choice);
				 	print_line();
				 	state = 2;
				 	break;
				 
				 case 6:
					 reset_1stmt(arruser, user);
					 printf("successful in clearing you statements!\n");
					 print_menu();
			       	 	 scanf("%d",&choice);	
			       	 	print_line();
			       	 	 state = 2;
				 	break;
				 	
				 case 7:
				 	user = -1;
				 	print_home();
				 	scanf("%d", &choice);
				 	print_line();
				 	state = 1;
				 	break;
				 
				 case 8 :
				 	state = 3;
				 	break;
				 	
				 default : printf(" you have entered a wrong choice\n please enter again\n");
				 print_menu();
	 			scanf("%d", &choice);
				print_line();
				 state = 2;
				 break;
			 }/* switch closed*/
		}/* if closed*/
		if(state == 3){
			FILE * fp;
			char data[] = "stddata";
			fp = fopen(data, "w");
			if(fp == NULL){
				printf(" file cannot be opened to store all the data you create in this session.\n sorry\n");
				return 0;
			}
			fprintf(fp, "%s\n", grpname);
			fprintf(fp, "%s\n",grppass);
			fprintf(fp, "%d\n", num_mem);
			for(i = 0; i< num_mem; i++){
				if(mem[i] != NULL){
					fprintf(fp, "%s\n", mem[i]);
					fprintf(fp, "%s\n", pass[i]);
					
				}/* if closed*/
			}
			fclose(fp);
			for(i = 0; i< num_mem; i++){
				fp = fopen(mem[i], "w+");
				fprintf(fp, "%d\n", arruser[i]->num_data);
				curr1 = arruser[i]->q;
				while(curr1){
					fprintf(fp, "%d ", curr1->amt);
					fprintf(fp, "%s\n", curr1->des);
					curr1 = curr1->n;
				}
				fprintf(fp, "%d ", arruser[i]->num_stmt);
				curr2 = arruser[i]->p;
				while(curr2){
					fprintf(fp, "%s\n", curr2->stmt);
					curr2 = curr2->next;
				}
				fclose(fp);
			}
			reset_data(arruser);
			reset_stmt(arruser);	
			for(i = 0; i < num_mem; i++){
				free(mem[i]);
				free(pass[i]);
				
			}
			break;
		}
	}/* while closed*/
	return 0;
}/* main closed*/