943,779 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 842
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 25th, 2008
0

need help for this code

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
diyaz is offline Offline
5 posts
since Sep 2008
Sep 25th, 2008
0

Re: need help for this code

That code doesn't even compile so how you get an output file who knows?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 25th, 2008
0

Re: need help for this code

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
diyaz is offline Offline
5 posts
since Sep 2008
Sep 25th, 2008
0

Re: need help for this code

>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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 25th, 2008
0

Re: need help for this code

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
diyaz is offline Offline
5 posts
since Sep 2008
Sep 25th, 2008
0

Re: need help for this code

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 25th, 2008
0

Re: need help for this code

thank you very much and sorry for troubling you but i really can run this code and i dun lie.
it become like this:
Name:  untitled.bmp
Views: 47
Size:  715.5 KB


but anyway, thank u so much.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
diyaz is offline Offline
5 posts
since Sep 2008
Sep 25th, 2008
0

Re: need help for this code

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 25th, 2008
0

Re: need help for this code

Click to Expand / Collapse  Quote originally posted by iamthwee ...
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
Sep 25th, 2008
0

Re: need help for this code

opss, its really my fault! sorry,sorry!this is the correct code:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
diyaz is offline Offline
5 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Opening and closing a file very often?
Next Thread in C++ Forum Timeline: Delete all leaf node in BST





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC