hello again can someone please help...
the program runs fine the first time but when i run it the second time it doesnt work as its suposed to-
1- when user is asked to enter y-to continue and n- to stop playing it doesnt matter what user enters it just continues the game
2-if i choose f to guess the film title the title is not covered by stars its name is just printed. sometimes the full movie sometimes half and tolower function doesnt work.
3- and when i choose c the second time i run the program it just asks me to choose again f or c and if i pick it again program stops running or asks me to choose again f works fine appart from the problem above.
if im still not giving enough info to get help please let me know ill write more...
Code:
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
FILE *fopen(), *fp;
int lines,c,z,i,j;
char star;
char film[5000];
char filmc[5000];
char opt;
char option2[100];
char charg;
char charf[100];
int errors;
void random(void);
void file(void);
void options(void);
void p_options(void);
void character(void);
void title(void);
void wait(void);
void yesno(void);
void main()
{
star='*';
i=0;
lines=1;
j=0;
errors = 1;
fp= fopen("films.txt","r");
random();
file();
printf("%d", z); //******
printf("%s",filmc);
options();
p_options();
fflush(stdin);
}
void random()
{
srand(time(NULL));
z = 1 + rand() %45;
}
void file()
{
while ( lines < z)
{
c = getc(fp);
if ( c == '\n')
{
lines++;
}
}
fgets(film,5000,fp);
strcpy(filmc,film);
while ( filmc[i] != '\0' ) /* not at end of string */
{ //fflush(stdin);
film[i] = tolower(film[i]);
//fflush(stdin);
if (( filmc[i] >= 'A' && filmc[i] <='Z') || ( filmc[i] >='a' && filmc[i] <='z' ))
{
filmc[i] = star;
}
i++;
}
}
void options()
{
fflush(stdin);
printf("\nEnter c-C or f_F to guess!?!?!?:O");
printf("\nc-C == CHARACTER!");
printf("\nf-F == FULL TITLE!\n\t~ ");
opt = getchar();
fflush(stdin);
opt = tolower(opt);
fflush(stdin);
}
void p_options()
{
if ( opt == 'c' )
{character();}
else if ( opt == 'f' )
{title();}
else
printf("Sorry but you have entered wrong character please\n try again!!idiot!!");
}
void character()
{
while(strcmp(film, filmc) != 0)
{
printf("\n%s",film); //****************
printf("%s\n", filmc);
fflush(stdin);
printf("\nenter a letter to guess:\t");
fgets(charf,100,stdin);
charf[0] = tolower(charf[0]);
if (isalpha(charf[0]))
{
for(j = 0; j < strlen(film); j++)
{
if(film[j] == charf[0])
{
filmc[j] = charf[0];
}
}
printf("%s\n", filmc);
}
else
printf("Only (a-z, A-Z)are allowed , try again:\n");
wait();
yesno();
options();
p_options();
fp= fopen("films.txt","r");
random();
file();
}
}
void title()
{
printf("\n%s",film); //****************
printf("%s\n", filmc);
printf("\nEnter the title of the film:");
fgets(charf,100,stdin);
if (strcmp(film,charf) == 0)
{
printf("\n ******** Correct ********\n");
printf("\n***** WOW! You guessed it in one go!!! ****\n");
}
else if(strcmp(film,charf) != 0)
{
printf("\n****Incorect, try again please ****\n");
}
wait();
yesno();
fp= fopen("films.txt","r");
random();
file();
options();
p_options();
}
void yesno()
{
printf("\nWould you like to play(agiain) or to leave the game?");
printf("\ny-Y == play(again)");
printf("\nn-N == to leave(stop)");
fflush(stdin);
gets(option2);
fflush(stdin);
//option2[100] = tolower(option2[100]);
if ( option2[0] == 'y' || option2[0] == 'Y' )
{
fp= fopen("films.txt","r");
random();
file();
options();
p_options();
}
else if (option2[0] == 'n' || option2[0] == 'N' )
{return;}
else
printf("sdasas");
}
void wait(void)
{
char dummy[100];
printf("\n\n Press RETURN to continue:");
gets( dummy ) ;
}