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;
}