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!