pointers - C++ Segmentation Fault - Core Dumped -


i've been having issue while , i've searched type of error , believe has memory leak or pointer pointing nothing.

i've checked code on , on again , i'm not able find issue occurring don't know how debug it. if try , breakpoint first line of code, crash.

it reading bunch of isbn's file , checking if they're valid or not.

although may seem it's lot, logic simple.

here code:

#include <iostream> #include <fstream> #include <iomanip> #include <list> #include <ctype.h> #include <string.h> #include <stdlib.h> #include <vector>  using namespace std;  class isbn {    private:    string isbncode;     public:     isbn()    {                }     isbn(string isbn): isbncode(isbn)    {                   }     ~isbn()    {                  }     string getisbn()    {       return isbncode;    }     void setisbn(string input)    {       isbncode = input;    } };  void setlistofisbn(const string filename, list<isbn> &listofisbn); void validateisbns(const list<isbn> listofisbn, list<bool> &validations); void printvalidations(const list<isbn> listofisbn,       const list<bool> validations); string btos(const bool bvalue);  int main(int argc, char *argv[]) {    list<isbn> listofisbn;    list<bool> validations;     string inputfile = argv[1];     setlistofisbn(inputfile, listofisbn);    validateisbns(listofisbn, validations);    printvalidations(listofisbn, validations);     return 0; }  void setlistofisbn(const string filename, list<isbn> &listofisbn) {    list<isbn>::const_iterator iterator;     ifstream fin;      fin.open(filename.c_str());     char ch;    string isbncode;     while (!fin.eof())     {        fin.get(ch);        if (ch == '\n')       {          isbn isbn;           (isbn).setisbn(isbncode);          listofisbn.push_back(isbn);          isbncode = "";                }       else       {          isbncode.append(reinterpret_cast<const char*>(ch));       }    }  }  void validateisbns(const list<isbn> listofisbn, list<bool> &validations) {    list<isbn>::const_iterator itr;     (itr = listofisbn.begin(); itr != listofisbn.end(); itr++)    {       isbn isbn = *itr;       string isbncode = isbn.getisbn();       string isbncodereform = "";       vector<int> products;       int sumofproducts = 0;       unsigned int i;        (i = 0; < isbncode.length(); itr++)       {          if(isalnum(isbncode[i]))          {             isbncodereform[i] = isbncode[i];          }       }        (i = 0; < (isbncodereform.length() - 1); itr++)       {          if(isbncodereform[i] == 'x')          {             isbncodereform[i] = 10;          }                    products[i] = isbncodereform[i] * (10 - i);             }        vector<int>::const_iterator itr;        (itr = products.begin(); itr != products.end(); itr++)       {          sumofproducts += products[*itr];       }        if ((sumofproducts % 11) == 0)       {          validations.push_back(true);       }       else       {          validations.push_back(false);       }    } }  void printvalidations(const list<isbn> listofisbn,       const list<bool> validations) {    list<isbn>::const_iterator itr;    list<bool>::const_iterator itr2 = validations.begin();      for(itr = listofisbn.begin(); itr != listofisbn.end(); itr++)    {       string validate = btos(*itr2);       isbn isbn = *itr;       cout << isbn.getisbn() + ": " + validate + "\n";       itr2++;    } }  string btos(const bool bvalue) {    if(bvalue == 0)    {       return "false";    }    else    {       return "true";    } } 

any appreciated!

i've tried detail many of obvious errors below. there plenty more. many errors obvious taking wrong approach programming. firstly should think code writing, it's not enough write looks right, programming have exactly right. secondly writing far low quality code. should write few lines of code, working before write more. have piled error upon error here it's obvious have been doing 0 testing. that's no way professional work, let alone beginner. , working means working, not compiling. code shows it's easy write loads of code compiles. learn use debugger, loads. apparently have sort of complicated client/server set up. forget that, install compiler , debugger on own machine.

isbncode.append(reinterpret_cast<const char*>(ch)); 

is wrong

isbncode += ch; 

is want. don't use reinterpret_cast unless know doing.

while (!fin.eof())  {      fin.get(ch); 

is wrong way of checking end of file

while (fin.get(ch))  {  

is want.

in fact whole void setlistofisbn(const string filename, list<isbn> &listofisbn) way complicated. same in 3 times less code

void setlistofisbn(const string filename, list<isbn> &listofisbn) {     ifstream fin(filename.c_str());     string isbncode;     while (getline(fin, isbncode))         listofisbn.push_back(isbncode); } 

this code incorrect

  string isbncodereform = "";   (i = 0; < isbncode.length(); itr++)   {      if(isalnum(isbncode[i]))      {         isbncodereform[i] = isbncode[i];      }   } 

because isbncodereform 0 length string isbncodereform[i] going fail. meant this

  string isbncodereform = "";   (i = 0; < isbncode.length(); itr++)   {      if(isalnum(isbncode[i]))      {         isbncodereform.push_back(isbncode[i]);      }   } 

this same error

vector<int> products; (i = 0; < (isbncodereform.length() - 1); itr++) {      if(isbncodereform[i] == 'x')      {         isbncodereform[i] = 10;      }                products[i] = isbncodereform[i] * (10 - i);       } 

again products 0 length array, products[i] fail. again meant like

     products.push_back(isbncodereform[i] * (10 - i)); 

this confusion how iterators work

  (itr = products.begin(); itr != products.end(); itr++)   {      sumofproducts += products[*itr];   } 

you write

  (itr = products.begin(); itr != products.end(); itr++)   {      sumofproducts += *itr;   } 

or write

  (i = 0; < products.size(); i++)   {      sumofproducts += products[i];   } 

what have mix of 2 doesn't work.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -