Thread: Segmentation Fault Random Sentece Generator

  1. #1
    Registered User
    Join Date
    Oct 2010
    Location
    Knoxville, Tennessee, United States
    Posts
    20

    Segmentation Fault Random Sentece Generator

    Hey guys, I am fairly new to C++ and very new to these forums here, and I'd like to start out by saying I love what you guys are doing here!

    I have been working on a random sentence generator that could possibly have applications for an AI program. When ever I run the program right now, it crashes and gives me a segmentation fault when it selects to runs my "livingSentenceMaker", but when it selects to run the "inadamentSentenceMaker" it runs as expected. This is not a homework assignment, this is something that I have been working on my own for my own educational experience. I have no experince using a debugger right now, and so all I was able to get out of it was and that isn't proving so useful to me. I have tried commenting out different parts of my livingSentenceMaker function, but I can't seem to find what exactly is causing the problem. Can you guys point me in the right direction? Much appreciated!

    http://rapidshare.com/files/42408622...ence_Maker.cpp Entire Source Code

    Code:
    Child process PID: 3316
    Program received signal SIGSEGV, Segmentation fault.
    At C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/iostream:77
    Code:
    #0 004327AE    std::string::assign() (C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/iostream:77)
    #1 00434876    std::string::operator=() (C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/iostream:77)
    #2 004021B9    livingNounVerbLogic() (C:/Users/Jeremy/Documents/New folder/sentences/main.cpp:167)
    #3 00402627    livingSentenceMaker() (C:/Users/Jeremy/Documents/New folder/sentences/main.cpp:207)
    #4 004027CB    main() (C:/Users/Jeremy/Documents/New folder/sentences/main.cpp:232)
    Code:
    // Jeremy Langford's Code
    
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <string>
    #include <vector>
    
    using namespace std;
    
        string article;
        string noun;
        string adj;
        string verb;
        string adverb;
        string prepPhrase;
        int articleSeed;
        int inadamentNounsSeed;
        int inadamentVerbsSeed;
        int inadamentAdjsSeed;
        int inadamentAdvsSeed;
        int livingNounsSeed;
        int livingVerbsSeed;
        int livingAdjsSeed;
        int livingAdvsSeed;
        int modeChooser;
    
    
    
    // Tells the sentence generators which ariticles to use
    void articleLogic(){
         
         srand(time(0));
         char articleTestChar = adj[0];
    
    if  ((articleTestChar == 'a')||( articleTestChar ==  'e')|| ( articleTestChar ==  'i')||( articleTestChar ==  'o')){ // Vowell Test
    
        vector<string> articleContainer(2);
            articleContainer[0] = "An ";
            articleContainer[1] = "The ";
    
        articleSeed = rand()%articleContainer.size();
    
        article = articleContainer[articleSeed];}
    
        // "A" or "The" for constanents
    else{
        vector<string> articleContainer(2);     // For Constenants
            articleContainer[0] = "A ";
            articleContainer[1] = "The ";
    
        articleSeed = rand()%articleContainer.size();
    
        article = articleContainer[articleSeed];}}
    
    
    
    // selects an adj that works for living things
    void livingAdjLogic(){
            
            vector<string>livingAdjs(8);
            livingAdjs[0] = "brown ";
            livingAdjs[1] = "blue ";
            livingAdjs[2] = "orange ";
            livingAdjs[3] = "furry ";
            livingAdjs[4] = "cute ";
            livingAdjs[5] = "large ";
            livingAdjs[6] = "small ";
            livingAdjs[7] = "spotted ";
        
        srand(time(0));
       
        livingAdjsSeed = rand()%livingAdjs.size();
    
        adj = livingAdjs[livingAdjsSeed];}
    
    // a bank of living things and things they can do
    void livingNounVerbLogic(){
         vector<string>livingNouns(12);
            livingNouns[0] = "monkey ";
            livingNouns[1] = "cat ";
            livingNouns[3] = "dog ";
            livingNouns[4] = "bear ";
            livingNouns[5] = "snake ";
            livingNouns[6] = "worm ";
            livingNouns[7] = "fish ";
            livingNouns[8] = "ant ";
            livingNouns[9] = "spider ";
            livingNouns[10] = "bird ";
            livingNouns[11] = "duck ";
        
        srand(time(0));
        
        livingNounsSeed = rand()%livingNouns.size();
    
        noun = livingNouns[livingNounsSeed];
    
    
        vector<string>livingVerbs(3);
            livingVerbs[0] = "layed ";
            livingVerbs[1] = "sat ";
            livingVerbs[2] = "rested ";
            livingVerbs[3] = "crawled ";
            livingVerbs[4] = "jumped ";
            livingVerbs[5] = "swam ";
            livingVerbs[6] = "barked ";
            livingVerbs[7] = "ate ";
            livingVerbs[8] = "slept ";
            livingVerbs[9] = "relaxed ";
            livingVerbs[10] = "hid ";
    
    
        livingVerbsSeed = rand()%livingVerbs.size();
    
        verb = livingVerbs[livingVerbsSeed];}
    
    // selects an adv that works with a livng thing
    void livingAdvLogic(){
       
        vector<string>livingAdvs(3);
            livingAdvs[0] = "silently ";
            livingAdvs[1] = "lifelessly ";
            livingAdvs[2] = "invitingly ";
            livingAdvs[3] = "hastily ";
            livingAdvs[4] = "carefully ";
            livingAdvs[5] = "quickly ";
            livingAdvs[6] = "slowley ";
            livingAdvs[7] = "painfully ";
            livingAdvs[8] = "happily ";
            livingAdvs[9] = "strangley ";
    
        srand(time(0));
        
        livingAdvsSeed = rand()%livingAdvs.size();
    
        adverb = livingAdvs[livingAdvsSeed];}
    
    /* agregation of livng things functions for a sentence maker !TROUBLE FUNCTION CRASHES WHEN RUN!*/
    void livingSentenceMaker(){
    
        articleLogic();
        livingAdjLogic();
        livingNounVerbLogic();
        livingAdvLogic();
        cout << article << adj << noun << verb << adverb /*<< prepPhrase */<<"." << endl;}
    
    int main(){
    
    /* menu functions and loop that includes a randomly generated number to select either a simple sentence with a living or inadament object.*/
    char menu = 'y';
    
    
        while (true){
        
        srand(time(0));
        
        modeChooser = rand()%2;
        cout << modeChooser <<endl;
    
        cout << endl;
        cout << "Do you want to run the sentence generator? Y/N." << endl;
        cin >> menu;
        cout << endl;
    
        if ((menu == 'y')&&(modeChooser == 0)){
            inadamentSentenceMaker();}
        else if (( menu == 'y')&& (modeChooser == 1)){
            livingSentenceMaker();}
    
        if (menu == 'n'){
                break;}}
    
        return 0;
    }
    Last edited by jlangfo5; 10-09-2010 at 01:30 PM. Reason: Bubba's Sugestions

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    In the future post the section of code you feel has the error or where the debugger says the errors are. You can also attach your source to the post if you want. Reading source from attachments is much easier on the eyes than reading it here.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Location
    Knoxville, Tennessee, United States
    Posts
    20
    Thank you Bubba, I have excluded the parts of the code that were not being used by the problem livingSentenceMaker function, I didn't want to exclude to much, so I still left everything in there that the function used. I am ataching my source code to this post as well in case anyone want's to look at it in its entirety, and for easier viewing and debugging.
    Last edited by jlangfo5; 10-09-2010 at 01:28 PM.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Location
    Knoxville, Tennessee, United States
    Posts
    20
    Solved! I had the vectors assigned to be the wrong size, and when I was pulling a string out of them it was messing up because there wasn't the proper space in memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. Testing Random Number Generator
    By Roaring_Tiger in forum C Programming
    Replies: 7
    Last Post: 08-12-2005, 12:48 AM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM