Page 2 of 2 FirstFirst 1 2
Results 21 to 37 of 37

Thread: c++ random letters     submit to reddit submit to twitter

  1. #21
    E. Body
    Join Date
    Jun 2005
    Posts
    2,226
    BG Level
    7
    FFXI Server
    Caitsith

    I appreciate you picking apart code I wrote in 15 minutes before class Snicky, lol. Honestly it was pretty sloppy, but I only meant it as a guideline to how i'd handle it. I tested with a file that had 3 words in it lol so it wasn't super extensive. (psst, who uses endl! rep the \n :D )

    Anyway, I hope between the two of us something good should appear xD. I'll try and debug some later if I get free time. In the middle of some C# work so my C gets pretty sloppy. D: BG needs a Code forum!

  2. #22
    Murder machine with a motor in her nose
    Join Date
    Apr 2007
    Posts
    368
    BG Level
    4
    FFXI Server
    Carbuncle

    You use endl because it flushes the output buffer, otherwise (in certain circumstances) you may not see the end of what was output immediately.

    Now if you were doing a multi-lined output you could use \n at the end of each line, but when you're finished, you should use endl to make sure the user sees what you think they're seeing. (or just do cout.flush())

    Plus there's a portability issue since a newline can be \r, \r\n, or \n depending on what you're doing (what OS you're using). It's generally safer to use endl to avoid that particular issue.


    As for picking apart your code, it's nothing personal. Hopefully, you'll see why I made the points that I did. It's constructive criticism. :3
    Looked back over my comments and they were all "you shouldn't do this: here's why" and meant to be instructive, not harsh. Given that Skie didn't catch the issues when he compiled it, it made sense to point them out as they would have caused issues.

  3. #23
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Code:
    #include <string>
    #include <time.h>
    #include <fstream>
    #include <iostream>
    
    
    using namespace std;
    
    void main (void) {
        string inputWord;
        ifstream inputFile;
        ifstream outputFile;
        inputFile.open("input.txt");
        outputFile.open("output.txt");
        srand((unsigned int)time(NULL));
        
        if(inputFile.is_open()) 
    	{
    		while(!inputFile.eof()) 
    			for(int i=1;i<(int)inputWord.length()-2;i++) {
    				int offset=i+(rand()%(inputWord.length()-(2+i)))+1;
    				char temp=inputWord[i];
    				inputWord[i]=inputWord[offset];
    				inputWord[offset]=temp;
    			}		
    		cout <<endl;
    		inputFile.close();
    		outputFile << inputWord << endl;
            outputFile.close();
    
        } 
    	else 
    		cerr << "Error: File Not Found"<<endl;
    		
    }
    now only problem is that i dont see what the value for the new jumbled words are to print it to the file, also did i print to the file right?

  4. #24
    Murder machine with a motor in her nose
    Join Date
    Apr 2007
    Posts
    368
    BG Level
    4
    FFXI Server
    Carbuncle

    Code:
    		while(!inputFile.eof()) 
    		{
    			inputFile >> inputWord;
    			for(int i=1;i<(int)inputWord.length()-2;i++)
    			{
    			//	swap(inputWord[i],inputWord[i+(rand()%(inputWord.length()-(2+i)))+1]);
    				int offset=i+(rand()%(inputWord.length()-(2+i)))+1;
    				char temp=inputWord[i];
    				inputWord[i]=inputWord[offset];
    				inputWord[offset]=temp;
    			}
    
    			outputFile << inputWord<<" ";
    		}
    		outputFile<<endl;
    		outputFile.close();
    		inputFile.close();

  5. #25
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Code:
    #include <string>
    #include <time.h>
    #include <fstream>
    #include <iostream>
    
    
    using namespace std;
    
    void main (void) {
        string inputWord;
        ifstream inputFile;
        ifstream outputFile;
        inputFile.open("input.txt");
        outputFile.open("output.txt");
        srand((unsigned int)time(NULL));
        
        if(inputFile.is_open()) 
    	{
    		while(!inputFile.eof()) 
    			for(int i=1;i<(int)inputWord.length()-2;i++) {
    				int offset=i+(rand()%(inputWord.length()-(2+i)))+1;
    				char temp=inputWord[i];
    				inputWord[i]=inputWord[offset];
    				inputWord[offset]=temp;
    				outputFile << inputWord <<" ";
    			}		
    		outputFile.close();
    		inputFile.close();
    
        } 
    	else 
    		cerr << "Error: File Not Found"<<endl;
    		
    }
    gives 1 error:

    'operator<<' not implmeneted in type 'ifstream' for arguments of type ' string' in function main

    if i leave the:

    Code:
    outputFile<<endl;
    in there it says i over load it

  6. #26
    Murder machine with a motor in her nose
    Join Date
    Apr 2007
    Posts
    368
    BG Level
    4
    FFXI Server
    Carbuncle

    outputfile needs to be ofstream
    ifstream = input file stream
    ofstream = output file stream

  7. #27
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Quote Originally Posted by SnickySnacks View Post
    outputfile needs to be ofstream
    ifstream = input file stream
    ofstream = output file stream

    not sure what your meaning

    Code:
        if(inputFile.is_open()) 
    	{
    		while(!inputFile.eof()) 
    			for(int i=1;i<(int)inputWord.length()-2;i++) {
    				int offset=i+(rand()%(inputWord.length()-(2+i)))+1;
    				char temp=inputWord[i];
    				inputWord[i]=inputWord[offset];
    				inputWord[offset]=temp;
    				ofstream << inputWord << " ";
    			}
    		outputFile.close();
    		inputFile.close();
    is that what you mean? just need to print to file now to see if it works but keeps saying

  8. #28
    Bagel
    Join Date
    Jan 2006
    Posts
    1,428
    BG Level
    6

    outputFile needs to be declared as type ofstream.

    eg:

    ofstream outputFile;

  9. #29
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    oh ok ok i get it.

    Code:
    #include <string>
    #include <time.h>
    #include <fstream>
    #include <iostream>
    
    
    
    using namespace std;
    
    void main (void) {
        string inputWord;
        ifstream inputFile;
        ifstream outputFile;
        inputFile.open("input.txt");
        outputFile.open("output.txt");
        srand((unsigned int)time(NULL));
        
        if(inputFile.is_open()) 
    	{
    		while(!inputFile.eof()) 
    			for(int i=1;i<(int)inputWord.length()-2;i++) {
    				int offset=i+(rand()%(inputWord.length()-(2+i)))+1;
    				char temp=inputWord[i];
    				inputWord[i]=inputWord[offset];
    				inputWord[offset]=temp;
    				ofstream output;
    				output << inputWord << " ";
    			}
    		outputFile.close();
    		inputFile.close();
    
    
        } 
    	else 
    		cerr << "Error: File Not Found"<<endl;
    		
    }
    compiles fine... does not create output.txt file >< on command prompt just drops down a lvl and flashes, unable to type or anything

  10. #30
    Murder machine with a motor in her nose
    Join Date
    Apr 2007
    Posts
    368
    BG Level
    4
    FFXI Server
    Carbuncle

    because you're not opening the file before you try to output.

    up at the top, change
    ifstream outputFile;
    to
    ofstream outputFile;
    and remove the second local declaration of outputFile.

    Code:
    #include <string>
    #include <time.h>
    #include <fstream>
    #include <iostream>
    
    
    
    using namespace std;
    
    void main (void) {
        string inputWord;
        ifstream inputFile;
        ofstream outputFile;
        inputFile.open("input.txt");
        outputFile.open("output.txt");
        srand((unsigned int)time(NULL));
        
        if(inputFile.is_open() && outputFile.is_open()) 
    	{
    		while(!inputFile.eof()) 
    			for(int i=1;i<(int)inputWord.length()-2;i++) {
    				int offset=i+(rand()%(inputWord.length()-(2+i)))+1;
    				char temp=inputWord[i];
    				inputWord[i]=inputWord[offset];
    				inputWord[offset]=temp;
    				outputFile << inputWord << " ";
    			}
    		outputFile.close();
    		inputFile.close();
    
    
        } 
    	else 
    		cerr << "Error: File Not Found"<<endl;
    		
    }

  11. #31
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    ok changed like u said and compiled fine, created output.txt, and edits it but doesnt put anything in it :/

    note:

    i had to add

    Code:
    ofstream output;
    output << inputWord << " ";
    otherwise it errors in compile

  12. #32
    Murder machine with a motor in her nose
    Join Date
    Apr 2007
    Posts
    368
    BG Level
    4
    FFXI Server
    Carbuncle

    remove ofstream output;
    change output to outputfile in the output << inputWord thing.

  13. #33
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    compiled without errors but still does the same thing. output.txt is still empty (yes input.txt has the paragraph in it.)

  14. #34
    Murder machine with a motor in her nose
    Join Date
    Apr 2007
    Posts
    368
    BG Level
    4
    FFXI Server
    Carbuncle

    Looks like you took out some neccesary brackets, which changed the flow control all around:

    Code:
    #include <string>
    #include <time.h>
    #include <fstream>
    #include <iostream>
    
    
    
    using namespace std;
    
    void main (void) 
    {
    
        string inputWord;
        ifstream inputFile;
        ofstream outputFile;
        inputFile.open("input.txt");
        outputFile.open("output.txt");
        srand((unsigned int)time(NULL));
        if(inputFile.is_open()) 
    	{
    		while(!inputFile.eof()) 
    		{
    			inputFile >> inputWord;
    			for(int i=1;i<(int)inputWord.length()-2;i++)
    			{
    				int offset=i+(rand()%(inputWord.length()-(2+i)))+1;
    				char temp=inputWord[i];
    				inputWord[i]=inputWord[offset];
    				inputWord[offset]=temp;
    			}
    
    			outputFile << inputWord<<" ";
    		}
    		outputFile<<endl;
    		outputFile.close();
    		inputFile.close();
        } 
    	else 
    		cerr << "Error: File Not Found"<<endl;
    }

  15. #35
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    works thanks, only problem now is that when it scrambles if it has a pronunciation beind it (IE: end. or however,) it jumbles the last letter along with the mix. i have something to refrence to in order to make mine now though so i can start working on it, thanks aton!

  16. #36
    Murder machine with a motor in her nose
    Join Date
    Apr 2007
    Posts
    368
    BG Level
    4
    FFXI Server
    Carbuncle

    My suggestion would be to check the last letter of the word, if it's not alphabetic, subtract 3 instead of 2 in the two lines that use the length and check the first letter for the same and start at int i=2 if it's not alphabetic.

    (the real way to do this would be to calculate internally the length of any string of alphabetic characters and scramble appropriately, the problem with the above method is that it only catches first/last punctuation and only single punctuation. Something like

    She said "Don't make others suffer for your personal hatred."

    would cause problems due to the leading quotes, apostrophe, and trailing period/quote for example.

    I'm not even sure how you'd scramble something like Don't. Dno't? d'ont? Do you scramble the apostrophe?

    Assuming you don't mind internal apostrophes and hyphens being scrambled, just figure out how many characters in the first letter is and start at int i=1+(# of starting non-alpha characters) and calculate the end with -(2+# of ending non-alpha characters)

  17. #37
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    oh ok thanks that helps a lot! i'll post if get any bugs or snags with my program.

Page 2 of 2 FirstFirst 1 2