Thread: Little Help

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    126

    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;
    }
    }
    Last edited by Sorinx; 10-23-2012 at 09:03 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Sorinx!

    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?

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Pretty sure my random from file is completely wrong

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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?
    Last edited by Adak; 10-23-2012 at 09:11 PM.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Doesn't make a difference

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A little less laconic and a bit more loquacious, if you please.

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    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

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  10. #10
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    How would I call the string by the random number?

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    for(i=0; i<YourRandomNumber; i++){   
       fscanf(fp, "%s", wordToGuess);
    }

  12. #12
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    So something like this?

    Code:
    if (fp){
    for(i=0;i<n;i++)
    while (!feof(fp))
    {
        fgets(buffer,500,fp);
    fscanf(fp, "%s", &wordToGuess);
    }

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Sorinx View Post
    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).

  14. #14
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    How would that get that specific random line, wouldn't it get every string up til that line

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.
    Last edited by Adak; 10-23-2012 at 11:18 PM.

Popular pages Recent additions subscribe to a feed