| | |
need help for this code
![]() |
•
•
Join Date: Sep 2008
Posts: 5
Reputation:
Solved Threads: 0
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:
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!
· 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:
C++ Syntax (Toggle Plain Text)
#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 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!
Last edited by Narue; Sep 25th, 2008 at 4:33 pm. Reason: added code tags, snipped email
>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
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. Last edited by iamthwee; Sep 25th, 2008 at 1:38 pm.
*Voted best profile in the world*
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/a...m_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.
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/a...m_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.
Last edited by iamthwee; Sep 25th, 2008 at 1:55 pm.
*Voted best profile in the world*
•
•
Join Date: Sep 2008
Posts: 5
Reputation:
Solved Threads: 0
thank you very much and sorry for troubling you but i really can run this code and i dun lie.
it become like this:
untitled.bmp
but anyway, thank u so much.
it become like this:
untitled.bmp
but anyway, thank u so much.
•
•
Join Date: Sep 2008
Posts: 5
Reputation:
Solved Threads: 0
opss, its really my fault! sorry,sorry!this is the correct code:
so, i just follow what u write it before right? using that random_shuffle but which part did i have to put it?
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by Narue; Sep 25th, 2008 at 4:34 pm. Reason: added code tags
![]() |
Similar Threads
- Code 19 Registry Error (Windows NT / 2000 / XP)
- Why won't this code work? (VB.NET)
- Need help with DirectX code (C)
- Tutorials & Code Submissions - Questions? (DaniWeb Community Feedback)
- Some Basic Code Hopefully (Help Needed) (HTML and CSS)
Other Threads in the C++ Forum
- Previous Thread: Opening and closing a file very often?
- Next Thread: Delete all leaf node in BST
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes classified code coding compatible compile console conversion count date delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file filewrite forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper homeworksolutions iamthwee icon if...else ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node object output play pointer problem program programming project python random read recursion reference rpg string strings struct symbol temperature template test text text-file toolkit tree url values variable vector video win32 windows winsock wordfrequency wxwidgets






