DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   need help for this code (http://www.daniweb.com/forums/thread147679.html)

diyaz Sep 25th, 2008 1:10 pm
need help for this code
 
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!

iamthwee Sep 25th, 2008 1:24 pm
Re: need help for this code
 
That code doesn't even compile so how you get an output file who knows?

diyaz Sep 25th, 2008 1:32 pm
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

iamthwee Sep 25th, 2008 1:35 pm
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.

diyaz Sep 25th, 2008 1:46 pm
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?

iamthwee Sep 25th, 2008 1:53 pm
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.

diyaz Sep 25th, 2008 2:09 pm
Re: need help for this code
 
1 Attachment(s)
thank you very much and sorry for troubling you but i really can run this code and i dun lie.
it become like this:
Attachment 7507

but anyway, thank u so much.

iamthwee Sep 25th, 2008 2:11 pm
Re: need help for this code
 
Well then the code you posted above is wrong, or you posted the wrong one by accident.

VernonDozier Sep 25th, 2008 2:18 pm
Re: need help for this code
 
Quote:

Originally Posted by iamthwee (Post 698949)
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.

diyaz Sep 25th, 2008 2:22 pm
Re: need help for this code
 
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?


All times are GMT -4. The time now is 1:09 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC