Help setting a hidden word

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mcdef
    New Member
    • Apr 2007
    • 5

    Help setting a hidden word

    I am trying to write a hangman program that has only two functions other than main.

    One of the functions:
    void sethiddenword(c har hidden[]);
    is supposed to randomly assign a word to hidden. I have a list of 10 words i would like to use but can't use a control statment to set one of them into the array.

    So far i have written
    void sethiddenword(c har hidden[]){
    int rnum;
    srand(time(0));
    rnum = rand() %10;
    }

    This gives me the random number but i dont know how to assign the words to hidden. If you can do it with control statements i would like to see that. Either way, thanks.
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by mcdef
    I am trying to write a hangman program that has only two functions other than main.

    One of the functions:
    void sethiddenword(c har hidden[]);
    is supposed to randomly assign a word to hidden. I have a list of 10 words i would like to use but can't use a control statment to set one of them into the array.

    So far i have written
    void sethiddenword(c har hidden[]){
    int rnum;
    srand(time(0));
    rnum = rand() %10;
    }

    This gives me the random number but i dont know how to assign the words to hidden. If you can do it with control statements i would like to see that. Either way, thanks.
    Do you have a string array of 10 words? If yes, then you can have the index be the random number.
    Code:
    void sethiddenword(){     // no need to pass an array
          int rnum;
          srand(time(0));
          rnum = rand() %10;
          string hidden = wordlist[rnum]; //you can do it with strings
          return hidden;
                     or
          char hidden[] = wordlist[rnum];       // or with char arrays
          return hidden;
    }
    That's what I did when I made my hangman. Also, you might want to change the function from a void to string return or a char array so you can return the word to main. I recommend using strings if you are using C++ but you can also do it with char arrays.

    Comment

    • mcdef
      New Member
      • Apr 2007
      • 5

      #3
      Originally posted by ilikepython
      Do you have a string array of 10 words? If yes, then you can have the index be the random number.
      Code:
      void sethiddenword(){     // no need to pass an array
            int rnum;
            srand(time(0));
            rnum = rand() %10;
            string hidden = wordlist[rnum]; //you can do it with strings
            return hidden;
                       or
            char hidden[] = wordlist[rnum];       // or with char arrays
            return hidden;
      }
      That's what I did when I made my hangman. Also, you might want to change the function from a void to string return or a char array so you can return the word to main. I recommend using strings if you are using C++ but you can also do it with char arrays.
      I have to do it in just C i cant do it in C++ so i guess i have to use char arrays. I do not know what a word list is, but i have 10 words on paper that i can use, like "computer", and "hello", i do not know how to bring them into th program without doing something like.
      char wordone[size] = "word"; 10 times.

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by mcdef
        I have to do it in just C i cant do it in C++ so i guess i have to use char arrays. I do not know what a word list is, but i have 10 words on paper that i can use, like "computer", and "hello", i do not know how to bring them into th program without doing something like.
        char wordone[size] = "word"; 10 times.
        Well you can store the words in a text file and read them in to an array from the file. That can help when you decide to add more words.
        Code:
        int count = 0;    //used to store index in array
        ifstream file ("location_of_file");
        while (!file.eof()){
            getline(file, wordlist[count]);     //wordlist is an array
            count++;}   //increment index
        file.close();   //important to close file
        Hope that helps.

        Comment

        • mcdef
          New Member
          • Apr 2007
          • 5

          #5
          Originally posted by ilikepython
          Well you can store the words in a text file and read them in to an array from the file. That can help when you decide to add more words.
          Code:
          int count = 0;    //used to store index in array
          ifstream file ("location_of_file");
          while (!file.eof()){
              getline(file, wordlist[count]);     //wordlist is an array
              count++;}   //increment index
          file.close();   //important to close file
          Hope that helps.
          I dont think we are allowed to use file i/o. How do you set, inside of sethiddenword() , an array of char strings. Like if the array is hidden, how can i set 10 words to 1 array. ie. hidden[0] = "word1", hidden[1] = "word2", i know that this will not work because the number inside the array is the size of the array.

          Comment

          • ilikepython
            Recognized Expert Contributor
            • Feb 2007
            • 844

            #6
            Originally posted by mcdef
            I dont think we are allowed to use file i/o. How do you set, inside of sethiddenword() , an array of char strings. Like if the array is hidden, how can i set 10 words to 1 array. ie. hidden[0] = "word1", hidden[1] = "word2", i know that this will not work because the number inside the array is the size of the array.
            You can declare the array like this:
            Code:
            char* hidden[10];
            //that makes it an array of char arrays
            then:
            Code:
            hidden[0] = "word1";
            hidden[1] = "word2";
            etc.
            Does that work for you?

            Comment

            • mcdef
              New Member
              • Apr 2007
              • 5

              #7
              Originally posted by ilikepython
              You can declare the array like this:
              Code:
              char* hidden[10];
              //that makes it an array of char arrays
              then:
              Code:
              hidden[0] = "word1";
              hidden[1] = "word2";
              etc.
              Does that work for you?
              if my function is
              void sethiddenword(c har hidden[]){
              int rnum;
              char *words[10];
              srand(time(0));
              rnum = rand() % 10;
              printf("%d\n", rnum);
              words[0] = "clemson";
              words[1] = "tigers";
              printf("%s\n", words[0]);
              }
              The printf stuff is just to see if my code worked. How do i assign the word to hidden[].

              hidden[] = words[rnum]; Does not work?

              Comment

              • ilikepython
                Recognized Expert Contributor
                • Feb 2007
                • 844

                #8
                Originally posted by mcdef
                if my function is
                void sethiddenword(c har hidden[]){
                int rnum;
                char *words[10];
                srand(time(0));
                rnum = rand() % 10;
                printf("%d\n", rnum);
                words[0] = "clemson";
                words[1] = "tigers";
                printf("%s\n", words[0]);
                }
                The printf stuff is just to see if my code worked. How do i assign the word to hidden[].

                hidden[] = words[rnum]; Does not work?
                You can either try:
                hidden = words[rnum];
                and make the arguement from char hidden[] to char *hidden which I'm not sure will work,

                or
                have the function return the array like this:
                Code:
                char* sethiddenword(){
                    int rnum;
                    char *words[10];
                    srand(time(0));
                    rnum = rand() % 10;
                    words[0] = "clemson";
                    words[1] = "tigers";
                    char hidden[] = words[rnum];
                    return hidden;}

                Comment

                • mcdef
                  New Member
                  • Apr 2007
                  • 5

                  #9
                  Originally posted by ilikepython
                  You can either try:
                  hidden = words[rnum];
                  and make the arguement from char hidden[] to char *hidden which I'm not sure will work,

                  or
                  have the function return the array like this:
                  Code:
                  char* sethiddenword(){
                      int rnum;
                      char *words[10];
                      srand(time(0));
                      rnum = rand() % 10;
                      words[0] = "clemson";
                      words[1] = "tigers";
                      char hidden[] = words[rnum];
                      return hidden;}
                  can i PM you what i have so far so you can get an idea of what my problem is.

                  I changed it to hidden = words[rnum] and that worked.
                  but when i call sethiddenword from my main function it doesnt not return the value of hidden.

                  Comment

                  • ilikepython
                    Recognized Expert Contributor
                    • Feb 2007
                    • 844

                    #10
                    Originally posted by mcdef
                    can i PM you what i have so far so you can get an idea of what my problem is.

                    I changed it to hidden = words[rnum] and that worked.
                    but when i call sethiddenword from my main function it doesnt not return the value of hidden.
                    Yea sure you can PM me

                    Comment

                    Working...