hello,

I'm trying to write a simple program that proves mathematical theorems (predicate calculus). What I need to do is to have a small database that holds all of the propositions ('abc'; 'b'; 'cde'; etc.). What I'm having a problem with is creating a function that would act as a database. I've created this function called 'database' that has two arguments: list2 (which holds the string to be searched' and remove (which either indicates if the string is supposed to be searched or removed). The problem that I have is that my function will update the 'dbase' string. But, next time the function is called, because of my 'dbase' declaration in the beginning of the function, the dbase character will be updated with the original declaration. Does anybody have any idea how I could modify either this function or even to create an entirely different function where I would either search the database (in this case, the content of the 'dbase' variable) or remove one entry in the database? Thanks a million for any suggestions (I tried looking on the web for any ideas but I was not successful).

r.

// function that holds the database
char database (char list2[], int remove)
{
	char dbase [10][10]={"abc\0","b\0","cde\0","d\0","ef\0","f\0"};
	int i, j, k;
	i=0;

	// database is NOT to be updated
	// but return the next string in database
	if (remove == '0')
	{
		while (i != 6) 
		{
			if ((list2[0] == dbase[i][0])&&(list2[1] == dbase[i][1])&&(list2[2] == dbase[i][2]))
			{
				for (j = 0;j<3;j++)
				{
					list2[j] = dbase[i+1][j];
				}
				i=6;
			}
			else
			{
				i++;		
			}
		}


		return list2[5];
	}

	// database is to be updated
	else
	{	
		while (i != 6) 
		{
			if ((list2[0] == dbase[i][0])&&(list2[1] == dbase[i][1])&&(list2[2] == dbase[i][2]))
			{
				k = i;
				while (k != 6)
				{
					for (j = 0;j<3;j++)
					{
						dbase[k][j] = dbase[k+1][j];
					}
					k++;
				}
				i=6;
			}
			else
			{
				i++;		
			}
		}
	}
}

Well if you had static char dbase then it would remember the state you left it in for next time.
But if you ever want to get back to the initial state, then you'd need to code that yourself.

> "abc\0","b\0","cde\0","d\0","ef\0","f\0"
You don't need those \0, the compiler adds them for you.

> return list2[5];
If you take the else, then you return garbage.
Whatever is the caller supposed to make of that?

You could probably simplify the code with strstr() and strcpy()

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.