0

The execution of my code crashes when it gets to the "atoi" function, but I cannot understand why. The code is supposed to read a matrix from a .csv file, considering: - the first row (so till the first '\n') and saving each element (separated by a ',') in a vector of ints; - the rest of the matrix, by looking at each element and creating a specific object if the number read is 1 or 2. I don't get any exception while debugging the program, it just crashes during the execution (and using the system ("PAUSE") I could figure out it was the atoi function which didn't work properly). Can you help me understand what is going wrong? Thank you very much. Ps: I also attached all the libraries I'm loading... maybe it can help :)

#include <fstream>
#include <stdio.h>
#include <sstream>
#define nullptr 0
#include <string>
#include "classi.h"
#include <cstdlib>
#include <stdlib.h>
#include <iostream>
#include <vector>
using namespace std;


int main(int argc, char *argv[]) {
    ifstream file("problem.csv");
    unsigned int N = 0;
    unsigned int M = 0;
    char c; //modificato char * c;
    unsigned int i=0,j=0, k=0, n_iter, j_temp =0;
    std::vector<car> v_row;
    std::vector<car> v_col_temp;
    std::vector<int> iter; // location where I want to save the ints corresponding to the elements of the first row of the .csv file //
    std::string iterazioni; //location where I want to save the first row as a string and then cut it into pieces (iteraz) and then convert (atoi --> iter)
    std::string iteraz;

    while(!file.eof()){
        std::getline(file,iterazioni,'\n');
        stringstream it(iterazioni);
        while (it.good()) {
            std::getline(it,iteraz, ',');
            iter[k] = atoi(iteraz.c_str());
            if(iter[k]<0){
                cout<<"Errore: negative #of iterations"<<endl;
                break;
            }
            iter.push_back(0);
            k++;
        }
        iter.pop_back();

        file.get(c);
        if (c=='1'){
            blue_car b(i,j);
            if (v_col_temp[i].get_next() != nullptr)
                v_col_temp[i].insert_tail(&b);
            else
                v_col_temp[i].insert_head(&b);
        }
        if (c=='2'){
            red_car r(i,j);
            if (v_row[i].get_next() != nullptr)
                v_row[i].insert_tail(&r);
            else
                v_row[i].insert_head(&r);
        }
        if (c==',') {
            j++;
            if (i == 0)
                j_temp++;
        }
        if (c=='\n'){
            car p;
            v_row.push_back(p);
            v_col_temp.push_back(p);
            i++;
            if (j != j_temp) {
                std ::cout<<"errore input non valido: numero righe/colonne non coerente"<<endl;
            }
            j=0;
        }

        else if ((c!='\n') && (c!=',') && (c!='0') && (c!='1') && (c!='2'))
            std ::cout<<"errore input non valido"<<endl;
    };
    n_iter = k-1;
    M=i;
    N=j+1;

...
bibi
  • 3,314
  • 4
  • 28
  • 43
  • You may use 'strtol' to get an end-pointer for an integrity test –  Jan 31 '16 at 19:58
  • After fixing the other problems, you might also consider [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Bo Persson Jan 31 '16 at 20:06

1 Answers1

0

Your program crashes because you failed to initialize the contents of the iter vector.

std::vector<int> iter; // location where I want to save the ints corresponding to the elements of the first row of the .csv file //

You declare and construct this vector. The vector is empty at this point, and it has no elements.

At some point later:

iter[k] = atoi(iteraz.c_str());

The initial value of k is 0, so this attempts to assign the return value from atoi() to iter[0].

The problem is, of course, there is no iter[0]. The iter vector is still empty, at this point.

Additional comments, which is sadly true for at least 50% of these kinds of questions on stackoverflow.com:

1) "using namespace std"; is a bad practice, that should be avoided

2) Do not use system("pause") as well, as you referenced in your question.

Community
  • 1
  • 1
Sam Varshavchik
  • 84,126
  • 5
  • 57
  • 106
  • Thank you very much, I'll fix right away. Unfortunately, I had no "instructions" in using C++ (even if I'm following a master degree in engineering...) so I just had to try it all. Can you please explain why they are both a bad practice, please? Thank you again for the help. – Julia_Engl Jan 31 '16 at 20:06
  • The reason it's bad practice is fully explained in the linked articles. Try clicking on the links, and reading further. – Sam Varshavchik Jan 31 '16 at 20:11
  • Can I also ask you one more thing? Is it correct to say that std::vector iter; just allocates memory for an empty vector? (So I should only do something like: std::vector iter(0) or iter[0] = 0?) Thanks again! – Julia_Engl Jan 31 '16 at 20:16
  • Sorry I am reading with a stupid, old Blackberry and couldn't see the links! – Julia_Engl Jan 31 '16 at 20:16
  • The following vector methods increase the size of the array, by adding content: push_back(), resize(), and insert(). You can find more information by reading any C++ tutorial. – Sam Varshavchik Jan 31 '16 at 20:33