Random Poem Generator

This is a discussion on Random Poem Generator within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by ineedmunchies I'm using this as code, it compiles ok, but it crashes when it opens the file. ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    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].
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,185
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,458
    index i is out of bounds...

    and you do not really need to count lines by yourself - vector has size() member
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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,185
    I don't know size(strVector). I do know of strVector.size(), though.

  8. #23
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,458
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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,185
    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
    21,313
    Good catch. Furthermore, the vector should be passed by reference too.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,458
    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);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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!

Page 2 of 2 FirstFirst 12
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, 09: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, 07:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21