Random word problem

This is a discussion on Random word problem within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I am having trouble with my program. I should point out that I am fairly new to C++ ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    73

    Random word problem

    Hi everyone,

    I am having trouble with my program. I should point out that I am fairly new to C++ although I have done C programming before. I have five sentences (each stored in an array of char) and I need to display ONE random word from the first sentence followed by ONE random word from the second sentence followed by ONE random word from the third sentence and so on. After each word is displayed, the user hits a key to continue and then a random word will be displayed from the next sentence. After the last sentence, I need to continue the process and go back to the first sentence and display another random word with of course the possibility of the same word to be displayed because it's completely at random.

    Here is my code so far for this program:

    Code:
    int randword(*char);
    
    int main (int argc, char *argv[]) {
    
       char word;
    
       char five[100][5]={"Mary walked along the road.\0",
                        "Computer are cool.\0",
                        "This is all about C++.\0",
                        "Today is a nice day.\0",
                        "There are tons of things to see\0"};
    
       word = randword(five);
    
    }
    
    int randword(char *line) {
    
       int i,x,z,total;
       total=0;
       z=strlen(line);
    
       for(i=0;i<z;i++){
          if (line+i==' ')
          total++;
       }
    
       x=rand() % (total + 1);
       return x;
    }
    I apologize if any of this is hard to understand but that is probably because I can't get it working yet. I need to figure out how to display only ONE word from the sentence at random and then go to the next sentence and display ONE word from that sentence and so on. That is the part I am having the most difficulty with.

    Any help, comments or suggestions would be greatly appreciated. Thanks!

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Code:
    int randword(*char);   //that * is on the wrong side
    that's the only build/compile error I see. So randword counts how many words there are in the string, than gives you a random number from 0 to that number. Printing that word would require you to search the string again. Also, that's only going to give you the number of words for the first sentence, strlen() stops once it finds the '\0' (which you don't have to put there explicitly by the way), so you need something to iterate through those sentences too. Taking the C approach, you would probably go with some strtok()'s and printf()'s, but here in C++ land, you can check out the string class and cout. There in the FAQ.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    73

    Re:

    Thanks for the advice. I had a compiler error and didn't even know it. LOL. Oh well. Well I'll play around with it and see what happens.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  2. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 05:44 PM
  3. Word COM problem
    By UnclePunker in forum Windows Programming
    Replies: 6
    Last Post: 01-06-2005, 10:51 AM
  4. problem with random integer
    By techno logic in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2002, 02:20 PM
  5. random problem
    By niroopan in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2002, 02:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21