rand() for arrays

I have to create a game, in which the computer chooses randomly a word out of 5 and randomizes its characters, so that the user has to guess the word. He can also get a hint of the computer. I have started the code, but dont know how to continue:
include <iostream>
include<ctime>
include <string>
include <iostream>
include <ctype.h>
include <cstring>
include <locale>
include <math.h>
include <stdio.h>
include <stdlib.h>
include <time.h>

using namespace std;

int main()
{

char aword[6] = {'h','o','u','s','e','\0'};
char bword[11] = {'u','n','i','v','e','r','s','i','t','y','\0'};
char cword[9] = {'c','o','m','p','u','t','e','r','\0'};
char dword[5] = {'t','e','x','t','\0'};
char eword[8] = {'p','i','c','t','u','r','e','\0'};
char words[100];
strcat(words, aword);
strcat(words, bword);
strcat(words, cword);
strcat(words, dword);
strcat(words, eword);
Looks like you started a thread, but dont know how to continue. ;-)

/Looking forward to play the game.
how about:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

using namespace std;
int main()
{
   char* aword = "house";
   char* bword = "university";
   char* cword = "computer";
   char* dword = "text";
   char* eword = "picture";

   int num = rand() % 5;
   if(num == 1)
   {
      cout<<aword<<endl;
   }
   if(num == 2)
   {
      cout<<bword<<endl;
   }
   if(num == 3)
   {
      cout<<cword<<endl;
   }
   if(num == 4)
   {
      cout<<dword<<endl;
   }
   if(num == 5)
   {
      cout<<eword<<endl;
   }
   return 0;
}


this is from the top of my head. Does this help as a start?
Last edited on
to random the letters in a word you could take like a number of 10 changes each time the computer picks a word and for example ... 1 change would be :
take 2 rand int between 0 and noofletters-1 and change their places within the word ....
you could do the change like this i think ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char* fChange (char* sWord)
{
char ch ;
int iC1, iC2 ;

srand (time (NULL)) ;

iC1 = rand() % strlen (sWord) ;
iC2 = rand() % strlen (sWord) ;

if(iC1 == iC2) iC2++ ;

ch = sWord [iC1] ;
sWord [iC1] = sWord [iC2] ;
sWord [iC2] = ch ;
return sWord ;
} 

I'm not sure about all those char* since I use string type most of the time ... but that was just an idea
Last edited on
If you find yourself naming things like "aword", "bword", or "word1", "word2", etc. You're probably doing it wrong.

Consider using an array. That way you don't have to have a million if's to figure out which variable to use.

1
2
3
4
5
6
7
8
9
10
11
const char* words[] = {
"house",
"university",
"computer",
"text",
"picture"
};

const char* randomword = words[ rand() % 5 ];

cout << randomword;
thanks to all of you, that was helpful!
what does the "% 5" mean in the 9. line of the code of Disch or the 12. on the one of TheDestroyer?
Your welcome.

Actually, it's the syntax for how the function has to be used to generate a random number. rand() % 5 gives a random number from 0 to 4 (perhaps from 1 to 5, I don't remember, check the result yourself).

Don't forget you have to initialise the time kernel of rand() with srand( time(NULL) ), so that you get a different random number each time you start the program.

http://www.cplusplus.com/reference/clibrary/cstdlib/rand/
Last edited on
% gives you the remainder after division. (19 % 7 == 5, because 19 / 7 == 2 remainder 5)

When coupled with rand, it effectively gives you a random number between 0 and x-1. So rand() % 5 will give you a random number between 0 and 4, because any number divided by 5 will have a remainder somewhere between 0 and 4.
Topic archived. No new replies allowed.