So the program is really big so I'll just post the two parts that need to be fixed.
The first part is the main function:
This version doesn't work right, it asks if the answer was right but then it just gives the same answer when I choose zero:
Code:
/* the main program */
int main ()
{
/* declaring and initiaizing variables */
FILE *book;
FILE *a;
FILE *b;
FILE *c;
char * pch = malloc(300);
char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char buffer[LINES];
int period = 0;
int work = 0;
int one = 1;
char sentence[4096] = {0};
/* The function is called so I don't have to type getch */
atexit(MyExit);
/* Inputing a single sentence, generating one sentence then passing it to the rest of the program */
sentence_generator();
/* The function is called and creates the text file */
split_by_sentence();
/* The text file just created is opened */
book = fopen("readtext.txt", "r");
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);
printf("Does the work please you? 1 for yes, 0 for no\n");
scanf("%d", &work);
if(work < 1)
{
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);
printf("Does the work please you? 1 for yes, 0 for no\n");
scanf("%d", &work);
}
if(period > one)
{
memset(&sentence[0], 0, sizeof(sentence));
}
period = 0;
}
}
fclose(book);
/* 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 the search function. I tried to send the word filedata to the array used, but then when I chose zero in main the same word was used so the search isn't ignoring that word that was just used:
Code:
/* The function to search text file for compatible word */
void search(char *src, char *a, char *b, char *c)
{
FILE *sp;
char used[300] = {0};
char byte [4] = {0};
char *filedata = malloc(300);
sp = fopen("readfile.txt", "r");
if(!sp)
{
perror("Error: file readlist.txt was not found or opened");
return;
}
while(fgets(filedata, 4096, sp)!=NULL)
{
byte[0] = filedata[0];
if( (!strncmp (byte, a, 1)) && (strpbrk(filedata, b)) && strcmp(filedata, used))
{
strcat(src, filedata);
memmove(used, filedata, strlen(filedata)+1);
/* write the text file comparison result to file */
/*fprintf(fp, "%s\n", text);*/
break;
}
else if( (!strncmp (byte, a, 1)) && (strpbrk(filedata, b)) )
{
strcat(src, filedata);
memmove(used, filedata, strlen(filedata)+1);
/* write the text file comparison result to file */
/*fprintf(fp, "%s\n", text);*/
break;
}
}
fclose(sp);
return;
}
I can get the program to work fine if the first return from sp is all I want, but I want to get the next word that fits the if condition in the search function if I want.
I wasn't going to ask for help but I have been at this for a long time so I thought I should.