|
I've to rearrange each string(stored in a vector),store it back to an array and print it. In addition to my strings some unexpected chars like 'w' "MN" are printed. I've to avoid these chars.
|
|
|
|
|
T.RATHA KRISHNAN wrote: I've to return the rearranged string and store it in an array
But you are not rearranging the string.
You are trying to replace some characters at particular positions (depending on the length of the input string) with some random characters.
If you really want to randomly rearrange the string then just use the standard library algorithm - std::random_shuffle[^].
Here is an example usage:
std::string s = "some test string";
std::random_shuffle(s.begin(), s.end());
std::random_shuffle(s.begin(), s.begin() + 5);
If you want to change some of the chars of your string then check out std::transform[^].
Also this snippet is from your code:
for(register int i = 0; i<players.size()-1; i++)
{
srand(t);
nameSecondPart.push_back(swapChars(players[++i]));
t++;
}
|
|
|
|
|
Replacing is what I want. But why some additional characters are getting printed. I'm reading the strings from a database and replacing some characters with some random chars. My database has 391 strings. If I print the size of the where I stored the rearranged strings, it prints 394. When I print the contents of the array some unexpected chars gets printed. Where do these additional strings come from?
|
|
|
|
|
I pointed out one of the problems with your for loop.
Also see the post[^] from Ash for the other problems.
You should really replace this loop with something normal like:
srand(time(0));
const int total_players = players.size();
for(int i = 0; i < total_players; ++i)
{
nameSecondPart.push_back(swapChars(players[i]));
}
Try with the loop above and see what happens. Also use your debugger, it is your friend!
And about your hand-crafted swapChars function, why not use std::transform ?
|
|
|
|
|
There are several surreal elements to that last block of code...
- calling srand each time around the loop
- using register and std::vector::push_back() together
- the bounds checking on the for loop
Kids of today eh?
Cheers,
Ash
|
|
|
|
|
I've to get a random char to replace with each string. That's why I called srand() inside for loop. Can u please tell me if anything would happen seriously with this call?
|
|
|
|
|
T.RATHA KRISHNAN wrote: Can u please tell me if anything would happen seriously with this call?
Aren't you interested on anything that would happen waggishly?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Generally you only seed your random number generator once - there's not really a lot of point in doing it more than that. Every time you call it you effectively start another sequence of random numbers. There's no real problem calling it more than once provided the seed is different every time.
Cheers,
Ash
|
|
|
|
|
Aescleal wrote: There's no real problem calling it more than once provided the seed is different every time.
Provided the seeds wouldn't alter the statistical distribution of the generated numbers, I would say.
--Carlo The Nitpick
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
|
Aescleal wrote: Kids of today eh?
Regards,
Nuri
|
|
|
|
|
Aescleal wrote: several surreal elements
The Salvador Dali of C++. Who knows, this code might be worth a fortune in, say, a 100 years?
|
|
|
|
|
There's a chance for some stuff I wrote years ago then! Does infinite looping make me Escher?
|
|
|
|
|
Pls send me a copy urgentzly, and I'll add it to the inheritance for my son!
|
|
|
|
|
I did black box testing, now does it make me like Picasso?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
I think I would be less artist and more of an Alan Turing myself. Some of the algorithms I wrote required infinite computational power...
|
|
|
|
|
And 'infinite computational power' carefully sidesteps your algorithms...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Hehe, you're quite right
|
|
|
|
|
How can i create word editor in c++?
Please Help me.
|
|
|
|
|
Have a look at the WordPad sample.
|
|
|
|
|
Hi All,
I am using a function foo() in "a.cpp" in Proj.lib
The function extern foo() is decalred in "b.h" in Proj.lib.
The extern foo() is defined in c.cpp in another Sample.dll
I am getting LNK2001:unresolved external symbol foo in a.obj..
How can i solve this ....
Thanks,
Hari
|
|
|
|
|
Put the name of the lib file corresponding to the library in which foo is defined in Project -> Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies .
|
|
|
|
|
Thats done ... i have added the lib in the Proj settings...
|
|
|
|
|
I have made that Proj setting changes and its still not working...
|
|
|
|
|
is the function being exported ? with __declspec(dllexport) ?
|
|
|
|