PLEASE HELP ME!!

HOW TO JUMBLE A WORD COMING FROM A FILE?


FILE *DOCU;
DOCU = fopen ("file.txt", "r");
if (DOCU == 0)
{
printf ("\nFailed to open!\n");
exit (1);
}
else
{
 for(a=0; a<100; a++)
 {
 	if(!feof(DOCU))
 	 fscanf(DOCU, "\n%s",&string[a]);
 	 }
fclose (DOCU);
}
x = rand()% 100;


then what's next? please help me. :(
You are looking for "random_shuffle" from the 'algorithm' header file: http://www.cplusplus.com/reference/algorithm/random_shuffle/
Is there another way to shuffle the words? becausse I can't understand how to use that one.
Last edited on
Assuming your variable "string" is an actual std::string data type, then it has two member functions ".begin()" and ".end()". Feed these into "random_shuffle" and check out what "string" contains.
I will PM you, please PM me back..
Last edited on
Is there any reason you don't want your code posted? I've posted some absolute garbage to this website when I was first starting out so don't be embarrassed about your code. Anyway, after your read in the data from your file create a new std::string and set it equal to your variable called "string". That will let you use the ".begin()" and ".end()" iterators:

1
2
3
4
///Your code is here
std::string NewString = string[a]; //Create an std::string from your variable
std::random_shuffle(NewString.begin(), NewString.end());
///More of your code goes here 


EDIT: If it wasn't clear before, the variable "NewString" now contains a jumbled version of the original.
Last edited on
I can't understand. :( I don't know where I will place/put that one.
You put my code right below the "fscanf()" function in the code segment you posted above. You'll need to change a few things, rename your "string" variable (just capitalize the 's') and change your #include <string.h> at the top of your file into #include <string> and add #include <algorithm> at the top as well.

NOTE: I had also originally forgot to designate which char array to assign to the "NewString" variable, this has been fixed.
I get a new error.[Error] 'strcmp' was not declared in this scope
by the way. I'm using Dev c++
Then add #include <cstring> to the top as well.
wait. do I need to change NewString?


std::string NewString = string[a]; //Create an std::string from your variable
std::random_shuffle(NewString.begin(), NewString.end());


I'm sorry, I'm noob. beginner only. :)
Last edited on
You probably should because it's a terrible name for a variable, but you don't need to. You do have to change the name of the char array you are currently calling "string" though.
Urrrrgggh! can't get it. :(( im so stupid.. urrrgh! !@#$!@

please! can you do it to my codes that I've sent to you a while ago. please open your inbox. Pm me back please. :((
Topic archived. No new replies allowed.