jeudi 29 janvier 2015

c++ program crashing because of if statment

My c++ program is crashing i think because of an if statement. I am using MinGW compiler and am given no errors. I have no idea as to why i am getting the error. The if statement in my generate function looks fine to me. Im comparing a string with an instance of a vector string.


here is the cpp code:



#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
#include "Insultgenerator_0hl14.h"

using namespace std;


FileException::FileException(const string& m) : message(m){}

string& FileException::what(){ return message;}

NumInsultsOutOfBounds::NumInsultsOutOfBounds(const string& m) : message(m){}

string& NumInsultsOutOfBounds::what(){ return message;}

InsultGenerator::InsultGenerator(const InsultGenerator& ) {};
InsultGenerator::InsultGenerator(){};

void InsultGenerator::initialize() {
int cols(0);

srand ( time(NULL));
string words ;

string filename("InsultsSource.txt");
ifstream filetoread(filename.c_str());

if(filetoread.fail()){
throw FileException("File not read.");
}
while(filetoread >> words){

if(cols==0){
colA.push_back(words);
cols++;
} else if(cols==1){
colB.push_back(words);
cols++;
}else{
colC.push_back(words);
cols= cols -2;
}


}


//for (int i=0;i<50;i++){
// cout << " "<< colA[i];
//}
}






string InsultGenerator::talkToMe() const{
string Thou = "Thou";

string a= Thou + " " + colA[(rand()%50)] + " " + colB[rand()%50] + " " + colC[rand()%50] +"!" ;

//cout << a << endl;

return a;
};//end talkToMe

vector<string> InsultGenerator::generate(const int num){

if (num<0){
throw NumInsultsOutOfBounds("You must be insulted at least once");
} else if (num >10000 ){
throw NumInsultsOutOfBounds("You are being insulted too many times!");
}

vector<string> insultList;
string list;

for(int i=0; i<num;i++ ){
list = talkToMe();

if(list != insultList[i]){
//insultList.push_back(list);
//cout << insultList[i]<< endl;
}
}



return insultList;
};//end generate

//int InsultGenerator::generateAndSave(const string filename, const int n) const{

//};//end generateAndSave

int main(){
InsultGenerator ig;
ig.initialize();
ig.talkToMe();
ig.generate(10);


}


Here is the header file :



#ifndef INSULTGENERATOR_0HL14_H_
#define INSULTGENERATOR_0HL14_H_

#include <string>
#include <vector>

using namespace std;

class InsultGenerator{
public:
InsultGenerator();
InsultGenerator(const InsultGenerator&);
void initialize() ;
string talkToMe() const;
vector<string> generate(const int) ;
int generateAndSave (const string, const int) const;

private:
vector<string> colA;
vector<string> colB;
vector<string> colC;
};

class FileException{
public:
FileException(const string&);
string& what();
private:
string message;
};

class NumInsultsOutOfBounds{
public:
NumInsultsOutOfBounds(const string &);
string& what();
private:
string message;
};

#endif

Aucun commentaire:

Enregistrer un commentaire