Basic C++ question
by David J Weaver · in Technical Issues · 01/21/2008 (8:23 pm) · 10 replies
Well this is the programming board so I thought I might get some help from yall.
What I have is:
a LOT of random letters made from this function:
All this does is get 1 of 27 possible letters (space included) over and over, the 'int monkeys' is passed from another function where the user inputs a number. Anyway I also have another String that the user inputs lets just say it returns "Hello world" (dear god im glad it doesn't).
What im stuck at is how to test the user input, versus the random letters, and output if one or all of the words are found.
here is the function that i have so far ( I KNOW ITS WRONG)
And I cant for the life of me figure out how to find 1 word (or more) out of 500k+ random letters.
Can anyone help me with this?
BTW: Its a very basic console app, and I know I should be using pointers and references etc. but im trying to learn as I go, and this is all self taught, so I dont have a teacher to go to.
edited for a little more clairity.
What I have is:
a LOT of random letters made from this function:
string randomLetters(int Monkeys)
{
int num_times = 10; // placeholder till i pass the real stuff
string name;
string letters[27] = {" ", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
for (int q = 0; q != num_times; ++q)
{
for (int i = 0; i != Monkeys; ++i)
{
name += letters[rand()%27]; //gets a random letter from the above list
}
}
return name;
}All this does is get 1 of 27 possible letters (space included) over and over, the 'int monkeys' is passed from another function where the user inputs a number. Anyway I also have another String that the user inputs lets just say it returns "Hello world" (dear god im glad it doesn't).
What im stuck at is how to test the user input, versus the random letters, and output if one or all of the words are found.
here is the function that i have so far ( I KNOW ITS WRONG)
string findWord(string name, string phrase)
{
for(int i = 0; i != name.size(); ++i)
{
string testCharTwo = name;
int res = -1;
for (int t = 0; name[t] != '[[4f39f105a56a2]]'; ++t)
{
if (phrase[t] == testCharTwo[t])
{
res = t;
}
}
if (res != -1)
{
cout << "yay!"; // cout the right word here
}
}
return "me"; // just untill i know what im returning
}And I cant for the life of me figure out how to find 1 word (or more) out of 500k+ random letters.
Can anyone help me with this?
BTW: Its a very basic console app, and I know I should be using pointers and references etc. but im trying to learn as I go, and this is all self taught, so I dont have a teacher to go to.
edited for a little more clairity.
About the author
#2
Also why should I use char versus a string?
01/22/2008 (8:24 am)
Yeah i have 'using namespace std' at the top and i have read on other C++ forums that I should'nt use that. I still don't understand why. Also why should I use char versus a string?
#3
in other words, are you testing 'David' from user input for instance, against 'qoisl' for instance generated by random characters and questioning why you're not finding 'D' for the first character in the random set, 'a' in the second character of the random set, 'v' for the third character of the random set, etc. for every character position, there's only a 1 in 26 chance that the characters will match.
01/22/2008 (8:55 am)
David maybe I don't understand, or maybe your post needs more clarification, but why should the user inputted characters match a set of random characters? in other words, are you testing 'David' from user input for instance, against 'qoisl' for instance generated by random characters and questioning why you're not finding 'D' for the first character in the random set, 'a' in the second character of the random set, 'v' for the third character of the random set, etc. for every character position, there's only a 1 in 26 chance that the characters will match.
#4
the user also inputs a number (probably pretty large number) the number is the number of "monkeys" and the "monkeys" make random letters, basically for now all i want is to test to see if it can find "Here rests William" in the random goboly-gook. I think that just to start maybe I should only use 1 word. If that word isn't in the "goboly-gook" then try again and see how long it takes to finally make that word.
I know the code above doesn't go through and try again, but im sure thats fairly easy, i just need to find the userinput. And yes ive done the math and I understand that "(27) to the power of (number of characters)" is the chances..
later ill look at time.h to see how to set the epoch and see how long it will take to match up the input.
01/22/2008 (9:24 am)
Just to clairify, if anyone has ever heard of the "infinite monkeys typing shakespeare" problem, the program is similar to that. what I have working is the user inputs a string (spaces included) "Here rests william" Caps dont matter because i use 'tolower' on the input. (more advanced version will test for caps, punctuation etc..) the user also inputs a number (probably pretty large number) the number is the number of "monkeys" and the "monkeys" make random letters, basically for now all i want is to test to see if it can find "Here rests William" in the random goboly-gook. I think that just to start maybe I should only use 1 word. If that word isn't in the "goboly-gook" then try again and see how long it takes to finally make that word.
I know the code above doesn't go through and try again, but im sure thats fairly easy, i just need to find the userinput. And yes ive done the math and I understand that "(27) to the power of (number of characters)" is the chances..
later ill look at time.h to see how to set the epoch and see how long it will take to match up the input.
#5
It would work, but it's gonna be like 100 times slower that way.
I dont see why you shouldnt use the std namespace. It really depends on what you're doing. If it's just a test, go ahead and use it :)
When you get your thing working and are concerned about speeding it up, perhaps you can replace the STL strings with something else that doesn't continuously allocate memory.
As far as "find" and similar algorithms are concerned, the std::string way of doing it is probably one of the best options you have. Admittedly, Microsoft's implementation of the STL library isn't the best in the world, but there are good chances that it's gonna perform better than what most programmers will ever be able to achieve.
EDIT:
By the way, when doing your maths to calculate how long it will take to match the user input, dont forget that the output of rand() starts repeating after 32768 calls or so, if i remember correctly
01/22/2008 (9:38 am)
The reason why you should use "char" instead of "string" for your letters is that strings would be significantly less efficient.It would work, but it's gonna be like 100 times slower that way.
I dont see why you shouldnt use the std namespace. It really depends on what you're doing. If it's just a test, go ahead and use it :)
When you get your thing working and are concerned about speeding it up, perhaps you can replace the STL strings with something else that doesn't continuously allocate memory.
As far as "find" and similar algorithms are concerned, the std::string way of doing it is probably one of the best options you have. Admittedly, Microsoft's implementation of the STL library isn't the best in the world, but there are good chances that it's gonna perform better than what most programmers will ever be able to achieve.
EDIT:
By the way, when doing your maths to calculate how long it will take to match the user input, dont forget that the output of rand() starts repeating after 32768 calls or so, if i remember correctly
#6
01/22/2008 (6:57 pm)
Thanks for your help Hadoken and Sean, Yeah i heard that rand() starts repeating after a while. I have downloaded a new RNG, mersaine twister or something like that, and another on called mother-of-all. Also i tryed to change it to 'char' but the compiler is telling me that I have too many initializers.
#7
01/23/2008 (2:49 am)
Char letters[] = "abcdefghijklmnopqrstuvwxyz ";
#8
01/23/2008 (8:47 am)
Doh! man I feel like a retard! I really should have known that, Thank you.
#9
Nevermind, didn't notice the space character. :)
Original post:
Also, you should change the 27 from
to a 26 unless you intended to have a chance at getting a NULL character and end up not seeing the entire result upon printf().
01/23/2008 (7:21 pm)
Edit:Nevermind, didn't notice the space character. :)
Original post:
Also, you should change the 27 from
name += letters[rand()%27];
to a 26 unless you intended to have a chance at getting a NULL character and end up not seeing the entire result upon printf().
#10
09/19/2008 (9:35 pm)
Trojke its khan!!!! your emails down or something.
Torque Owner Hadoken
string letters[27]
It should probably be
char letters[27]
If your "string" class is in fact, as I think it should be, a std::string, you can simply do
name.find(phrase)