Thread: Random Poem Generator

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ineedmunchies
    I'm using this as code, it compiles ok, but it crashes when it opens the file.
    One problem is that you do not check if you were successful in opening the file. However, the likely cause of your problem is that you attempt to write to elements of the vector, but the vector is empty. A possible fix for this is:

    Code:
    vector<string> strVector;
    string line;
    while (getline(myfile, line))
    {
        strVector.push_back(line);
    }
    However, you still cannot just assume that after the loop strVector will have at least three elements; you should check before you access strVector[2].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Dec 2009
    Posts
    40
    Quote Originally Posted by laserlight View Post
    However, you still cannot just assume that after the loop strVector will have at least three elements; you should check before you access strVector[2].
    Ok thank you, I will try that. It works great thank you. Now i am trying to print out the strings read from the file.

    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    #include <iterator>
    #include <fstream>
    
    using namespace std;
    fstream myfile;
    int i=0;
    vector<string> strVector;
    
    int main()
    {
        myfile.open ("H:\\Programming\\Poem\\adjectives.txt");
    
        while (myfile != NULL)
        {
            string line;
            while (getline(myfile, line))
                {
                strVector.push_back(line);
                i++;
                }
            myfile.close();
        }
    
        int j;
        for (j=0; j=i; j++){
        cout << strVector[j];
        }
    }
    This is crashing again now. Not sure what's going wrong this time. I have moved some of the variables to be global variables.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    for (j=0; j=i; j++){
    Not exactly how for loops work.

  4. #19
    Registered User
    Join Date
    Dec 2009
    Posts
    40
    Code:
        int j=0;
        for (j; j<i+1; j++){
        cout << strVector[j];
        cout << "\n";
    Oopps sorry bit rusty.

    It runs now, and prints them to screen but still throws up error. Saying that the exe has encountered an error and needs to close. Seems like a file problem to me?

  5. #20
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    index i is out of bounds...

    and you do not really need to count lines by yourself - vector has size() member
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #21
    Registered User
    Join Date
    Dec 2009
    Posts
    40
    Ah well spotted thank you. So instead of
    Code:
    j<i
    you would suggest

    EDIT::
    Code:
    j<strVector.size

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't know size(strVector). I do know of strVector.size(), though.

  8. #23
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by ineedmunchies View Post
    Ah well spotted thank you. So instead of
    Code:
    j<i
    you would suggest
    Code:
    j<size(strVector)
    strVector.size() is closer to what I was thinking of
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #24
    Registered User
    Join Date
    Dec 2009
    Posts
    40
    Ha sorry, i just realised that and edited it as you were posting there. Thank you guys.

  10. #25
    Registered User
    Join Date
    Dec 2009
    Posts
    40
    Ok so yet another issue, I'm trying to create a function that will create a different vector for each file I read from.

    Code:
    void vectorize (fstream in_file, vector<string> out_vector)
    {while (in_file != NULL)
        {
            string line;
            while (getline(in_file, line))
                {
                out_vector.push_back(line);
                }
            in_file.close();
        }
    }
    This is what I have so far, but it won't compile. It says its an error within this context and that ios_base is private. (this error is flagged on the line that this function is called in main)

  11. #26
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're not allowed to copy fstreams. Since the normal parameter passing mechanism involves making a copy, don't do that; use a reference instead.
    Code:
    void vectorize (fstream &in_file, vector<string> out_vector)

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Good catch. Furthermore, the vector should be passed by reference too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I would pass the filepath in and open it there, then return a vector, so the prototype would be:
    Code:
    vector<string> vectorize(string filepath);
    which could be used:
    Code:
    vector<string> filedata = vectorize("somefile.txt");
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #29
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by MK27 View Post
    I would pass the filepath in and open it there, then return a vector, so the prototype would be:
    Code:
    vector<string> vectorize(string filepath);
    I would pass istream, because function should not really bother to know where from the data is coming
    Code:
    std::vector<std::string> vectorize(std::istream& in);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #30
    Registered User
    Join Date
    Dec 2009
    Posts
    40
    Hey thanks guys, got it working

    "A Healthy Suit
    A healthy suit kept in accordance with the amused elbow
    Deceivingly the suit left
    The elbow threw in place of a ugliest jam"

    Ha not the best poetry ever, but least it's working. Now all I have to do is get it working as a windows programme. eeeeeek!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. How exactly does the random number generator work?
    By Finchie_88 in forum C++ Programming
    Replies: 6
    Last Post: 08-24-2007, 12:46 AM
  3. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  4. Testing Random Number Generator
    By Roaring_Tiger in forum C Programming
    Replies: 7
    Last Post: 08-12-2005, 12:48 AM
  5. Random number generator
    By Caze in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2002, 08:10 AM