need help for this code

Reply

Join Date: Sep 2008
Posts: 5
Reputation: diyaz is an unknown quantity at this point 
Solved Threads: 0
diyaz diyaz is offline Offline
Newbie Poster

need help for this code

 
0
  #1
Sep 25th, 2008
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:
  1. #include <iostream>
  2. #include <fstream>
  3. #include<algorithm>
  4. #include <ctime>
  5. #include <string>
  6. #define SIZE 12
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. cout<<"Begin file creation"<<endl;
  12. int num;
  13. cout<<"Insert how many words"<<endl;
  14. cin>>num;
  15. string word;
  16. cout<<"Please insert the words that you want to scramble"<<endl;
  17. getline(cin,word,'!');
  18.  
  19. ofstream fout;
  20.  
  21. fout.open("string.txt");
  22. if (!fout)
  23. {
  24. cerr<<"Error open file";
  25. exit(100);
  26. }
  27. fout<<word;
  28. cout<<endl<<endl;
  29. cout<<"Original word...."<<endl;
  30. cout<<word<<endl;
  31.  
  32.  
  33. ifstream fin;
  34. fin.open("string.txt");
  35.  
  36.  
  37. if (!fin)
  38. {
  39. cerr<<"Error open file";
  40. exit(100);
  41. }
  42. getline(fin,word);
  43. cout<<word;
  44. fin.close();
  45. srand(time(0));
  46.  
  47. do{
  48. cout<<"scrambling character(s)..."<<endl;
  49. cout<<endl<<endl;
  50. cout<<"Iteration #"<<nom<<endl;
  51. nom++;
  52. cout<<endl;
  53.  
  54. ofstream fin;
  55. fin.open("shuffle.txt");
  56.  
  57. if(!fin)
  58. {
  59. cerr<<"Invalid!";
  60. exit(100);
  61. }
  62.  
  63.  
  64. srand (time(NULL));
  65. num=strlen(jumble);
  66. randomChoice = rand()%num;
  67. //strcpy(jumble,num[randomChoice]);
  68. for (j=0;jumble[j]!='\0';j++)// to scramble the word
  69. {
  70. k = rand() % num;
  71. temp = jumble[j];
  72. jumble[j] = jumble[k];
  73. jumble[k] = temp;
  74. }
  75.  
  76. cout<<jumble<<endl;
  77.  
  78.  
  79. fin.close();
  80. cout<<endl<<endl;
  81.  
  82. cout<<"Reshuffle?"<<endl;
  83. cin>>shuffle;
  84.  
  85. }while((shuffle=='Y')||(shuffle=='y'));
  86.  
  87. if((shuffle=='N')||(shuffle=='n'))
  88. {
  89. cout<<"Stop at iteration #"<<nom-1<<endl;
  90. cout<<"Thank you"<<endl;
  91. }
  92. else
  93. cout<<"Wrong Input!"<<endl;
  94.  
  95. system("PAUSE");
  96. return 0;
  97. }
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!
Last edited by Narue; Sep 25th, 2008 at 4:33 pm. Reason: added code tags, snipped email
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: need help for this code

 
0
  #2
Sep 25th, 2008
That code doesn't even compile so how you get an output file who knows?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 5
Reputation: diyaz is an unknown quantity at this point 
Solved Threads: 0
diyaz diyaz is offline Offline
Newbie Poster

Re: need help for this code

 
0
  #3
Sep 25th, 2008
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
Last edited by diyaz; Sep 25th, 2008 at 1:33 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: need help for this code

 
0
  #4
Sep 25th, 2008
>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.
Last edited by iamthwee; Sep 25th, 2008 at 1:38 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 5
Reputation: diyaz is an unknown quantity at this point 
Solved Threads: 0
diyaz diyaz is offline Offline
Newbie Poster

Re: need help for this code

 
0
  #5
Sep 25th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: need help for this code

 
0
  #6
Sep 25th, 2008
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.
Last edited by iamthwee; Sep 25th, 2008 at 1:55 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 5
Reputation: diyaz is an unknown quantity at this point 
Solved Threads: 0
diyaz diyaz is offline Offline
Newbie Poster

Re: need help for this code

 
0
  #7
Sep 25th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: need help for this code

 
0
  #8
Sep 25th, 2008
Well then the code you posted above is wrong, or you posted the wrong one by accident.
Last edited by iamthwee; Sep 25th, 2008 at 2:12 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,752
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 491
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: need help for this code

 
0
  #9
Sep 25th, 2008
Originally Posted by iamthwee View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 5
Reputation: diyaz is an unknown quantity at this point 
Solved Threads: 0
diyaz diyaz is offline Offline
Newbie Poster

Re: need help for this code

 
0
  #10
Sep 25th, 2008
opss, its really my fault! sorry,sorry!this is the correct code:
  1. #include <iostream>
  2. #include <fstream>
  3. #include<algorithm>
  4. #include <ctime>
  5. #include <string>
  6. #define SIZE 12
  7. using namespace std;
  8.  
  9. char ans;
  10. int no=1;
  11.  
  12. int rand(int num)
  13. {
  14. return rand()%num;
  15. }
  16. int main()
  17. {
  18. int randomWord;
  19. char sentence[SIZE];
  20. int i, j;
  21. char temp;
  22. int num;
  23. string word ;
  24. cout<<"Begin file creation "<<endl;
  25. cout<<"Insert how many words "<<endl;
  26. cin>>num;
  27. cout<<"Please insert the words that you want to scramble"<<endl;
  28. getline(cin,word,'!');
  29. ofstream fout;
  30. fout.open("string.txt");
  31. if (!fout)
  32. {
  33. cerr<<"Error open file";
  34. exit(100);
  35. }
  36. fout<<word;
  37. cout<<endl<<endl;
  38. cout<<"Original word...."<<endl;
  39. cout<<word<<endl;
  40. ifstream fin;
  41. fin.open("string.txt");
  42. if (!fin)
  43. {
  44. cerr<<"Error open file";
  45. exit(100);
  46. }
  47. getline(fin,word);
  48. cout<<word;
  49. fin.close();
  50. srand(time(NULL));
  51. do{
  52. cout<<"scrambling character(s)..."<<endl;
  53. cout<<endl<<endl;
  54. cout<<"Iteration #"<<no<<endl;
  55. no++;
  56. cout<<endl;
  57. ofstream fin;
  58. fin.open("shuffle.txt");
  59. if(!fin)
  60. {
  61. cerr<<"Invalid!";
  62. exit(100);
  63. }
  64. srand (time(NULL));
  65. randomWord=rand()%num;
  66. //strcpy(jumble,num[randomChoice]);
  67. num=strlen(sentence);
  68. for (i=0;sentence[i]!='\0';i++)// to scramble the word
  69. {
  70. j = rand() % num;
  71. temp = sentence[i];
  72. sentence[i] = sentence[j];
  73. sentence[j] = temp;
  74. }
  75. cout<<sentence<<endl;
  76. fin.close();
  77. cout<<endl<<endl;
  78. cout<<"Reshuffle?(Y/N)"<<endl;
  79. cin>>ans;
  80.  
  81. }while(ans=='Y'||ans=='y');
  82. if(ans=='N'||ans=='n')
  83. {
  84. cout<<"Stop at iteration #"<<no-1<<endl;
  85. cout<<"Thank you"<<endl;
  86. }
  87. else
  88. cout<<"Wrong Input!"<<endl;
  89.  
  90. system("PAUSE");
  91. return 0;
  92. }
so, i just follow what u write it before right? using that random_shuffle but which part did i have to put it?
Last edited by Narue; Sep 25th, 2008 at 4:34 pm. Reason: added code tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC