Reading And Randomising From File
Hi I am making a H A N G M A N. I have posted earlier as well. Now I have developed it roughly and would like to post it here. Please help me out on this. What Its supposed to do is Read from a file then keeps it in memory, then asks the user to guess them. Please help me on this. HELP ME PLEASE
Code:
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
int func(void);
void wrong(void);
void hello(void);
int main(void)
#define MAX_WORDS 1024
{
typedef struct
{
char* m_Array [MAX_WORDS]; // Every entry is a pointer to string
int m_iCount; // How many filled in 'm_Array'
}
WORDS;
void CreateWords (WORDS* pWords, char* pstrFileName)
{
FILE* pFile = fopen (pstrFileName, "rt");
char strLine [32]; // Assume words are no longer than 31 chars
int iLastCharPos;
if (pFile) {
pWords->m_iCount = 0;
while (fgets (strLine, 31, pFile)) {
// Remove '\n', at the end, if any:
iLastCharPos = strlen (strLine) - 1;
if (strLine [iLastCharPos] == '\n') {
strLine [iLastCharPos] = '\0';
}
pWords->m_Array [pWords->m_iCount] = strdup (strLine);
(pWords->m_iCount)++;
if (pWords->m_iCount == MAX_WORDS) {
break; // Array is full
}
}
fclose (pFile);
}
}
char* GetRandomWord (WORDS* pWords)
{
return pWords->m_Array [rand () % pWords->m_iCount];
}
void DestroyWords (WORDS* pWords)
{
for (int i=0; i < pWords->m_iCount; i++) {
free (pWords->m_Array [i]);
}
}
{
char words[10][10] = {"mythology", "astrology", "interface", "programme", "schedules",
"faithless", "sandstorm", "bulgarian", "boulevard", "salvation"};
char temp[10] = "_________";
int count, random, flag;
char current[10];
char ch;
int i,t,q;
count = 50;
flag = 2;
strcpy(temp,"_________");
random=func();
for(q=0;q<strlen(words[random]);q++){
current[q]=words[random][q];
}
do
{
printf("\n Points left: %d ",count);
printf(temp);
ch=getch();
flag = 2;
for(t=0;t<strlen(current);t++)
{
if(ch==current[t])temp[t]=ch;
flag=0;
}
if(flag<2)count--;
if(count<1){
wrong();
return 0;
}
}
while(strcmp(temp,words[random]));
printf("\nYou did it. \nPress any key to exit...");
ch=getche();
return 0;
}
}
int func(void){
srand((unsigned)time(NULL));
return (int)rand()%10;
}
void wrong(void)
{
printf("\n\nSorry, But You Have Run Out Of Your Tries. Please Retry The Program");
}