Hi Adak,
I have updated my syllogism program in the main and search functions.
Now the search sends main a sentence in a file.
Main asks if the syllogism was good, if not the sentence main read is written to bad.txt.
Then the next time the program is run serach opens bad.txt and if it finds a match the statements are not run.
This was the bad.txt doesn't fill up with duplicates.
But I want the search function to give me the next word which will be written to a file where it will be opened my main and the cycle starts again.
I haven't done this yet though, and I don't know how, that's why I'm posting this for an idea on how to get the next word in the list.
Here is main:
Code:
/* the main program */
int main ()
{
/* declaring and initiaizing variables */
FILE *read_to_review;
FILE *bad_list;
FILE *book;
FILE *a;
FILE *b;
FILE *c;
char * pch = malloc(300);
char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char buffer[LINES];
char copy_review[REVIEW];
int period = 0;
int test_result = 0;
int x = 0;
int good = 0;
int one = 1;
char sentence[4096] = {0};
/* The function is called so I don't have to type getch */
atexit(MyExit);
/* X recieves 1 so the program can run */
printf("Type 1 to run the program\n");
scanf("%d", &x);
while(!good)
{
switch(x)
{
case 1:
{
/* The sentence generator section is run*/
sentence_generator();
/* Now the syllogism section begins */
/* The function is called and creates the text file */
split_by_sentence();
/* The text file just created is opened */
book = fopen("readtext.txt", "r");
read_to_review = fopen("m_and_s.txt", "r");
bad_list = fopen("bad.txt", "a+");
if(!book)
{
perror("Error: file readtext.txt was not found or opened");
return 0;
}
/* read from file */
while(fgets(buffer, sizeof(buffer), book)!=NULL)
{
/* join two sentences */
strcat(sentence, buffer);
strcat(sentence, " ");
/* counting the periods in the input text file*/
if (strstr(buffer, "."))
{
period++;
}
/* After two periods are counted the while loop is exited */
if(period > one)
{
/* I find the alphabet in pch after it's run through strtok */
Camestros_percentage_calculation_numbers(sentence);
Baroco_percentage_calculation_numbers(sentence);
Darii_percentage_calculation_numbers(sentence);
Ferio_percentage_calculation_numbers(sentence);
Bocardo_percentage_calculation_numbers(sentence);
Celarent_Celaront_Felapton_percentage_calculation_numbers(sentence);
Darapti_Barbari_Barbara_percentage_calculation_numbers(sentence);
/* The printf asks if the syllogism was fine, if not copy_review is written to bad.txt */
/* Then the search function opens the bad.txt and decides if it runs or not */
fgets(copy_review, sizeof(copy_review), read_to_review);
printf("Was the syllogism logical? 1 for yes, 0 for no: \n");
scanf("%d",&test_result);
if(test_result==0)
fprintf(bad_list, "%s\n", copy_review);
/* The printf asks if I want to keep running the program or close it */
printf("Close program? [1=yes, 0=no]: ");
scanf("%d",&good);
if(period > one)
{
memset(&sentence[0], 0, sizeof(sentence));
}
period = 0;
break;
}
}
fclose(book);
fclose(bad_list);
fclose(read_to_review);
/* emptying the files so the program doesn't build them up each time it's used */
a = fopen("readtext.txt", "w");
if(!a)
{
perror("Error: file readtext.txt was not found or opened");
return 0;
}
b = fopen("readtext1.txt", "w");
if(!b)
{
perror("Error: file readtext1.txt was not found or opened");
return 0;
}
c = fopen("writelist.txt", "w");
if(!c)
{
perror("Error: file writelist.txt was not found or opened");
return 0;
}
fprintf(a, "");
fprintf(b, "");
fprintf(c, "");
fclose(a);
fclose(b);
fclose(c);
}
}
}
return 0;
}
Here is search:
Code:
void search(char *src, char *a, char *b, char *c)
{
FILE *sp;
FILE *read_bad;
FILE *write;
FILE *backup;
int number = 0;
// int n = 0; // This variable is used for the switch statement.
char used[300] = {0};
char byte [4] = {0};
char *filedata = malloc(300);
char *list_of_bad = malloc(300);
char *copy = malloc(500);
sp = fopen("readfile.txt", "r");
if(!sp)
{
perror("Error: file readlist.txt was not found or opened");
return;
}
backup = fopen("backup.txt", "w");
write = fopen("m_and_s.txt", "w");
read_bad = fopen("bad.txt", "r");
delete_char(c, '\n', 0);
// The printf and scanf are for the switch statement code.
/*printf("\nEnter a result number\n");
scanf("%d", &n);*/
while(fgets(filedata, 4096, sp)!=NULL)
{
byte[0] = filedata[0];
// I look at the first letter in the word from the file
// and compare it to a, and the word from the file must also must have b in it somewhere.
if( (!strncmp (byte, a, 1)) && (strpbrk(filedata, b)) )
{
number++;
while(number < 18)
{
if(number==17)
fprintf(backup, "%s\n", filedata);
break;
}
// I commented out the switch statement. The switch helped me tweak the while loop below number++.
/*switch(n)
{
case 1:
{
while(number < 19)
{
if(number==18)
strcat(src, filedata);
break;
}
}
break;
case 2:
{
while(number < 20)
{
if(number==19)
strcat(src, filedata);
break;
}
}
break;
case 3:
{
while(number < 21)
{
if(number==20)
strcat(src, filedata);
break;
}
}
break;
case 4:
{
while(number < 22)
{
if(number==21)
strcat(src, filedata);
break;
}
}
break;
case 5:
{
while(number < 23)
{
if(number==22)
strcat(src, filedata);
break;
}
}
break;
case 6:
{
while(number < 24)
{
if(number==23)
strcat(src, filedata);
break;
}
}
break;
case 7:
{
while(number < 25)
{
if(number==24)
strcat(src, filedata);
break;
}
}
break;
case 8:
{
while(number < 26)
{
if(number==25)
strcat(src, filedata);
break;
}
}
break;
case 9:
{*/
if(number==80)
{
/* The bad.txt is opened and checked it the filedata and c are being paired */
/* if they are being paired then the statements are not run */
while(fgets(list_of_bad, 4096, read_bad)!=NULL)
{
if(((strstr(list_of_bad, c)) && (strstr(list_of_bad, filedata)))!=1)
{
if(filedata != NULL)
{
strcat(src, filedata);
delete_char(c, '\n', 0);
delete_char(filedata, '\n', 0);
fprintf(write, "%s %s\n", c, filedata);
memmove (copy, filedata, strlen(filedata)+1);
strcat(c, " ");
strcat(c, copy);
strcat(c, "\n");
while(fgets(list_of_bad, 4096, read_bad)!=NULL)
if(strcmp(copy,list_of_bad))
printf("found one\n");
}
break;
}
/* If the statements above are not run then this message is printed. */
/* i want this to give the next word in the sp list though, but i don't know how to do that */
else
{
printf("sorry, that is wrong.");
break;
}
}
}
/*while(number < 38)
{
if(number==37)
strcat(src, filedata);
else if(filedata == NULL)
{
fgets(filedata, 4096, sp);
strcat(src, filedata);
}
break;
}*/
/*}
break;
default:
{
while(number < 5)
{
if(number==4)
strcat(src, filedata);
break;
}
}
}*/
}
}
fclose(read_bad);
fclose(write);
fclose(backup);
fclose(sp);
return;
}
I've been at this for a while. I've googled and read about incrementing pointers and tried that:
But that just removed a letter when I tested with printf.
So I'm stuck and need some help.