-3

Im still quite new in programming c++ - so please forgive me if my code is not perfect yet (still im open to any suggestions to improve ;) )

I have a problem running this code (only part of larger project).

int stock::import(string file){
ifstream input;
input.open(file.c_str());


input.ignore(100000, '\n');

while(!input.eof()){
    int y = 0;
    int m = 0;
    int d = 0;
    string year;
    string month;
    string day;
    string open;
    string high;
    string low;
    string close;
    string volume;
    string adj;


    {
       getline(input, year, '-');
        y = atoi(year.c_str());
    }
    {
        getline(input, month, '-');
        m = atoi(month.c_str());
    }
    {
        getline(input, day, ',');
        d = atoi(day.c_str());
    }



    int index = findplace(y, m, d);


    values[index].year = y;
    values[index].month = m;
    values[index].day = d;

    {
        getline(input, open, ',');
        values[index].open = open;
    }


    {
        getline(input, high, ',');
        values[index].high = high;
    }

    {
        getline(input, low, ',');
        values[index].low = low;
    }

    {
       getline(input, close, ',');
        values[index].close = close;
    }

    {
       getline(input, volume, ',');
        values[index].volume = volume;
    }

    {
        getline(input, adj, '\n');
        values[index].adj = adj;
    }



}

return 0;}

Project compiles fine but as the function "import" is called my programm crashes immediately. The debugger gives me back the error "cannot find bound of current function" at this part of the code:

{
        getline(input, open, ',');
        values[index].open = open;
}

the strange thing is, that if I delete this part of the code:

{
       getline(input, year, '-');
        y = atoi(year.c_str());
}

a few lines obove the critical section my programm works fine (meaning it doesn't crash but of course doesn't do what it is supposed to do).

I'm currently unsing CodeBlocks as an IDE.

Can anyone help me with this? I am really desperate by now as I already tried out everything I know...

As suggested here is more information about values and findplace():

class stock{
    private:
    string name = "";
    string code = "";
    string number = "";
    stock* next = NULL;
    int findplace(int year, int month, int day);

    public:
    day* values = new day[30];
    stock(string c);
    string getindex();
    void setnext(stock* n);
    stock* getnext();
    int import(string file);

};

so basically values[] is just an array of pointers to objects from class "day" class "day" looks like this:

class day{
            public:
                int year = 0;
                int month = 0;
                int day = 0;
                string open;
                string high;
                string low;
                string close;
                string volume;
                string adj;
};

and in function findplace() I tired to insert a new day into the array, so that the array is sorted by date and always keeps the newest days and discards the oldest when inserting a new one. It looks like this:

int stock::findplace(int year, int month, int day){
int j = 29;
for(j; values[j].year > year; j--){

}
for(j; values[j].month > month; j--){

}
for(j; values[j].day > day; j--){

}
if(!(values[j].year == year && values[j].month == month && values[j].day == day)){
    delete &values[0];
    for(int i = 0; i < j; i++){
        values[i] = values[i+1];
    }
}
return j;

}

  • 6
    You provided a lot of code that is not relevant and left out important parts. Please read this link on producing a [MCVE]. Notably, it's hard to help you without knowing more about the `values` variable. – François Andrieux Mar 21 '17 at 14:08
  • 1
    `while(!input.eof()){` [why-is-while-feof-file-always-wrong](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – synchronizer Mar 21 '17 at 14:09
  • 2
    You never check for any errors.... – SingerOfTheFall Mar 21 '17 at 14:11
  • "_without knowing more about the values variable_" or the `findplace()` function. (Which is _probably_ returning an invalid index, at a guess). – TripeHound Mar 21 '17 at 14:22
  • @SingerOfTheFall as this is an assignment for a basic programming class we are allowed to assume correct imput (so error checking for things the user types in is not necessary) – DasKöpfchen Mar 21 '17 at 15:33
  • @FrançoisAndrieux I will add more information about values[] and findplace() in a minute - thanks for trying to help me – DasKöpfchen Mar 21 '17 at 15:35
  • @TripeHound I added the information - thanks for trying to help me – DasKöpfchen Mar 21 '17 at 16:13
  • Your problem is _probably_ that the searches in `findplace()` may end-up decrementing `j` below zero (depending on what's already there, and what the passed-in value is). – TripeHound Mar 21 '17 at 16:37
  • @TripeHound this solved the problem!!! I was looking for this for 2,5 days now and couldn't find it. Thank you so so much!! – DasKöpfchen Mar 21 '17 at 17:02

1 Answers1

0

try printing the values of year/y month/m day/d. I would assume that somehow you are reading theses values incorrectly from your input file and that is generating an out-of-bounds index to your values array.

s_b
  • 501
  • 5
  • 16