-
Little Help
I'm sure I'm making many mistakes, any guidance would be appreciated, thanks
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void errorPrint(int wrong);
char userGuess(char guess[]);
void getWord(char wordToGuess[]);
int main ()
{
int wrong=0, randomNum, i, n=0;
char guess[27], wordToGuess[50], complWord[50]={0}, guessed;
userGuess(guess);
getWord(wordToGuess);
while(guessed<8 && strcmp(wordToGuess,complWord) != 0)
{
for(i=0;i<32;i++){
if (wordToGuess[i]==guess[n]){
complWord[i]=wordToGuess[i];
userGuess(guess);
n++;
}
else {
++wrong;
errorPrint(wrong);
}
}
}
}
char userGuess(char guess[])
{
int guessed=0;
printf("Choose a letter you wish to guess");
scanf("%c", &guess[guessed]);
guessed++;
}
void getWord(char wordToGuess[])
{
FILE *fp=fopen("word.txt", "r");
int i, n=0;
n=rand()% ((31+1)-1)+1;
char text[n][50];
for(i=0; i<50; i++){
fscanf(fp, "%s", &text);
}
fclose(fp);
}
void errorPrint(int wrong)
{
switch(wrong){
case 0:
break;
case 1:
printf("You have guessed %d wrong", wrong);
printf(" _______\n");
printf(" | |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf("_________|\n");
break;
case 2:
printf("You have guessed %d wrong", wrong);
printf(" _______\n");
printf(" | |\n");
printf(" 0 |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf("_________|\n");
break;
case 3:
printf("You have guessed %d wrong", wrong);
printf(" _______\n");
printf(" | |/\n");
printf(" 0 |\n");
printf("| | |\n");
printf(" |\n");
printf(" |\n");
printf("_________|\n");
break;
case 4:
printf("You have guessed %d wrong", wrong);
printf(" _______\n");
printf(" | |/\n");
printf(" 0 |\n");
printf("//| | |\n");
printf(" |\n");
printf(" |\n");
printf("___________|\n");
break;
case 5:
printf("You have guessed %d wrong", wrong);
printf(" _______\n");
printf(" | |\n");
printf(" 0 |\n");
printf("//| |\\ |\n");
printf(" |\n");
printf(" |\n");
printf("___________|\n");
break;
case 6:
printf("You have guessed %d wrong", wrong);
printf(" _______\n");
printf(" | |/\n");
printf(" 0 |\n");
printf("//| |\\ |\n");
printf(" || |\n");
printf(" |\n");
printf("____________|\n");
break;
default:
printf("You have guessed %d wrong", wrong);
printf(" _______\n");
printf(" | |/\n");
printf(" 0 |\n");
printf("//| |\\ |\n");
printf(" || || |\n");
printf(" |\n");
printf("____________|\n");
break;
}
}
-
Welcome to the forum, Sorinx! :cool:
This is hangman's game?
What is the problem? I can't offer specific advise on a very general type of complaint.
Do you have compiler errors or warnings? Run time errors? Logic errors in the game itself?
-
Pretty sure my random from file is completely wrong
-
The array you create in a function, goes to byte heaven, as soon as the function is left (unless it's static, in which case it's "held").
Anyway, make your word array in main, and pass it to the getword function, so all will be well. OK, you have wordToGuess[], so you can use that array. I'd include the size of the array, however: wordToGuess[50].
(P.S. English has no words longer than 30 letters, unless you count chemical "descriptions" as names.)
How many words are in your file?
-
Doesn't make a difference
-
What did you do?
(EDIT: Basically, saying that it "Doesn't make a difference" doesn't help people help you. I could reply: "so, you did something wrong", which is true, but isn't much help to you because it is so vague)
-
A little less laconic and a bit more loquacious, if you please.
-
Lol my fault, I guess my issue is with my understanding of how I would pull the random string from file into the array since it isn't working at all
-
First, test if(fp) to make sure the file has been opened. If so, then go into the for loop, and fgets() every word (one word per line for this to work), and when you reach the random number, then exit the loop, and quit the function.
-
How would I call the string by the random number?
-
Code:
for(i=0; i<YourRandomNumber; i++){
fscanf(fp, "%s", wordToGuess);
}
-
So something like this?
Code:
if (fp){
for(i=0;i<n;i++)
while (!feof(fp))
{
fgets(buffer,500,fp);
fscanf(fp, "%s", &wordToGuess);
}
-
Quote:
Originally Posted by
Sorinx
So something like this?
Code:
if (fp) {
for(i=0;i<n;i++)
fscanf(fp, "%s", wordToGuess);
}
Where n is your random number. You don't need much else. (and feof() doesn't work as you might think, I wouldn't use it).
-
How would that get that specific random line, wouldn't it get every string up til that line
-
Yes, but it would overwrite it as it went. This all done in RAM so it's blink of an eye stuff. The HD is the bottleneck. You could write up code to allow it to be skipped over, but with a text file, and words of different length, it's messy before long. Somehow, you have to move the fp forward to the random number 'th word.
You might need to add a *c onto the scanf() format specification, because you will have a newline in the file, at the end of each word.
fscanf(fp, "%s*c",wordToGuess);
will EAT the newline, and not store it. Where c is a char variable.