You are required to write a program which will:
· Fetch inputs from the user
· Ask the user how many words that he want to insert
· Append the original inputs to the screen (and save to a file called as string.txt)
· Append the scrambled inputs to the screen (and save to a file called as
shuffle.txt)
· Shuffled inputs contain the new modified sentence which all the words are
randomly scattered.
· The amount of reshuffled repeated are based from the user’s intention
· Read/search on randomize ( rand(), srand() ) to help you on this. Refer
page 150 and above in your textbook- Chapter 4. Also, find a function call within
algorithm library which allow you to reshuffle arrays’ element-do Internet
search! (which means you have to do #include<algorithm> as the
processor directive.
· Since you are dealing with words, use “string” data type.
the example output
See the example output in the screen below:
Begin file creation
Insert how many words
6
Please insert the words that you want to scramble
I HAVE A CAT, BIG CAT!
Original word....
I HAVE A CAT, BIG CAT!

Scrambling character(s)...
Iteration #1
I CAT! CAT, BIG HAVE A
Reshuffle?
Y

Scrambling character(s)...
Iteration #2
I HAVE A CAT! CAT, BIG
Reshuffle?
Y

Scrambling character(s)...
Iteration #3
I BIG CAT, HAVE CAT! A
Reshuffle?
Y

Scrambling character(s)...
Iteration #4
I A CAT! CAT, BIG HAVE
Reshuffle?
Y

Scrambling character(s)...
Iteration #5
I HAVE BIG CAT! A CAT,
Reshuffle?
N
Stop at iteration #5
Thank you!

Contents of string.txt
I HAVE A CAT, BIG CAT!

Contents of shuffle.txt
I CAT! CAT, BIG HAVE A
I HAVE A CAT! CAT, BIG
I BIG CAT, HAVE CAT! A
I A CAT! CAT, BIG HAVE
I HAVE BIG CAT! A CAT,

ok, i've done this code using dev-c++. the problem is, when the program random the word, the word that came out is unreadable and i can't store the output to shuffle.txt file. can somebody define the mistake of this code?

this is the code:

#include <iostream>
#include <fstream>
#include<algorithm>
#include <ctime>
#include <string>
#define SIZE 12 
using namespace std;

int main()
{
    cout<<"Begin file creation"<<endl;
    int num;
    cout<<"Insert how many words"<<endl;
    cin>>num;
    string word;
    cout<<"Please insert the words that you want to scramble"<<endl;
    getline(cin,word,'!');
    
    ofstream fout;
    
    fout.open("string.txt");
    if (!fout)
    {
              cerr<<"Error open file";
              exit(100);
    }
    fout<<word;
    cout<<endl<<endl;
    cout<<"Original word...."<<endl;
    cout<<word<<endl;

    
    ifstream fin;
    fin.open("string.txt");
       
    
    if (!fin)
    {
              cerr<<"Error open file";
              exit(100);
    }
    getline(fin,word);
    cout<<word;
    fin.close();
    srand(time(0));
      
    do{
    cout<<"scrambling character(s)..."<<endl;
    cout<<endl<<endl;
    cout<<"Iteration #"<<nom<<endl;
    nom++;
    cout<<endl; 
    
    ofstream fin;
    fin.open("shuffle.txt");
    
    if(!fin)
    {
            cerr<<"Invalid!";
            exit(100);
    }
    

    srand (time(NULL));
    num=strlen(jumble);
    randomChoice = rand()%num;
    //strcpy(jumble,num[randomChoice]);
        for (j=0;jumble[j]!='\0';j++)// to scramble the word
        {                                
        k = rand() % num;
        temp      = jumble[j];
        jumble[j] = jumble[k];
        jumble[k] = temp;
        }
        
        cout<<jumble<<endl;
    
     
     fin.close();
     cout<<endl<<endl;
     
     cout<<"Reshuffle?"<<endl;
     cin>>shuffle; 
  
    }while((shuffle=='Y')||(shuffle=='y'));
    
    if((shuffle=='N')||(shuffle=='n'))
      {
      cout<<"Stop at iteration #"<<nom-1<<endl;
      cout<<"Thank you"<<endl;
      }
    else
    cout<<"Wrong Input!"<<endl;
    
    system("PAUSE");
    return 0;
}

my example how many word is '6'
my example word is 'i have a cat, big cat!'

i need a quick answer actually and thank u for helping.
beside reply it here, you can reply it to my e-mail [snipped email]
i really need the answer as fast as you can. sorry for trouble!

That code doesn't even compile so how you get an output file who knows?

u can't? is something wrong with code cos my compiler can compile it. the problem occurred when it comes to random the word and to save it to the shuffle.txt

>is something wrong with code cos my compiler can compile it

This is a lie. Please don't lie to us. (I am using dev-cpp) Also, read your assignment, aren't you supposed to use random_shuffle from the header file #include <algorithm> or something I don't see you doing that.

so, my code is really wrong. i'm sorry coz i'm still new using this dev c++. but i really don't understand about the algorithm. i can't use this code at all? sorry for making trouble. can u tell me which part is wrong but this code can run right?

Firstly, you shouldn't lie to us. Don't pretend you are new to dev-cpp and you have compiled that code you posted and got an output, because clearly you haven't!


Second I shall give you a few clues.

-You need to use getline to read in a string. (you may be doing this already)
-you need to separate the string into tokens, this is generally called tokenising your input, the whitespace will be your delimiter here.
-now you can put your words into an array or vector, this depends on what you teacher is expecting.
-Then you can shuffle the words about using the code:
http://www.cplusplus.com/reference/algorithm/random_shuffle.html

Then write that to a file.

Third, we don't do favours, it's only urgent to you and therefore you should have organised you time better.

thank you very much and sorry for troubling you but i really can run this code and i dun lie.
it become like this:


but anyway, thank u so much.

Well then the code you posted above is wrong, or you posted the wrong one by accident.

Well then the code you posted above is wrong, or you posted the wrong one by accident.

Yes, the code you posted would not compile anywhere. You don't define nom . Copy and paste the code you posted verbatim and try compiling it again.

opss, its really my fault! sorry,sorry!this is the correct code:

#include <iostream>
#include <fstream>
#include<algorithm>
#include <ctime>
#include <string>
#define SIZE 12      
using namespace std;

char ans;
int no=1;

int rand(int num)
{
    return rand()%num;
}
int main()
{
    int randomWord;
    char sentence[SIZE];
    int i, j; 
    char temp;
    int num;
    string word ;
    cout<<"Begin file creation "<<endl;
    cout<<"Insert how many words "<<endl;
    cin>>num;
    cout<<"Please insert the words that you want to scramble"<<endl;
    getline(cin,word,'!');
    ofstream fout;  
    fout.open("string.txt");
    if (!fout)
    {
              cerr<<"Error open file";
              exit(100);
    }
    fout<<word;
    cout<<endl<<endl;
    cout<<"Original word...."<<endl;
    cout<<word<<endl;    
    ifstream fin;
    fin.open("string.txt");    
    if (!fin)
    {
              cerr<<"Error open file";
              exit(100);
    }
    getline(fin,word);
    cout<<word;
    fin.close();
    srand(time(NULL));  
    do{
    cout<<"scrambling character(s)..."<<endl;
    cout<<endl<<endl;
    cout<<"Iteration #"<<no<<endl;
    no++;
    cout<<endl;    
    ofstream fin;
    fin.open("shuffle.txt");    
    if(!fin)
    {
            cerr<<"Invalid!";
            exit(100);
    }
    srand (time(NULL));
    randomWord=rand()%num;
    //strcpy(jumble,num[randomChoice]);
    num=strlen(sentence);
        for (i=0;sentence[i]!='\0';i++)// to scramble the word
        {                                
        j = rand() % num;
        temp      = sentence[i];
        sentence[i] = sentence[j];
        sentence[j] = temp;
        }
     cout<<sentence<<endl;
     fin.close();
     cout<<endl<<endl;
     cout<<"Reshuffle?(Y/N)"<<endl;
     cin>>ans; 
  
    }while(ans=='Y'||ans=='y');
    if(ans=='N'||ans=='n')
      {
      cout<<"Stop at iteration #"<<no-1<<endl;
      cout<<"Thank you"<<endl;
      }
    else
    cout<<"Wrong Input!"<<endl;
    
    system("PAUSE");
    return 0;
}

so, i just follow what u write it before right? using that random_shuffle but which part did i have to put it?

Here is some sample code which may be of use to you:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <ctime>
#include <string>
#include <sstream>

using namespace std;

int main()
{

  string line;
  cout << "Please insert the words that you want to scramble" << endl;
  getline ( cin, line, '\n' ); 
  string array[100];

  stringstream ss ( line );

  string word;
  string newLine = "";

  int i = 0;
  while ( ss >> word )
  {
    array[i] = word;
    i++;
  } 
  char ans;

  do
  {
    cout << "\nReshuffle (Y/N)" << endl;
    cin >> ans;

    srand ( unsigned ( time ( NULL ) ) );
    random_shuffle ( array, array + i );

    //print shuffled sentence
    cout << "\n";
    for ( int j = 0 ; j < i; j++ )
    {
      cout << array[j] << " ";
    }
  }while ( ans != 'N' ); 
  system ( "PAUSE" );
  return 0;
}

You might not be able to use stringstreams to separate your sentence into bits...

output

Begin file creation
Please insert the words that you want to scramble
HELLO, I AM A FOOL!

RESHUFFLE (Y/N)
Y

A FOOL! HELLO, I AM
RESHUFFLE (Y/N)
Y

I AM FOOL! A HELLO,
RESHUFFLE (Y/N)
Y

A HELLO, AM I FOOL!
RESHUFFLE (Y/N)
Y

I FOOL! HELLO, A AM
RESHUFFLE (Y/N)
Y

AM I FOOL! HELLO, A
RESHUFFLE (Y/N)