0
#include <iostream>  
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <sstream>

using namespace std;

int main(int argc, char* argv[])
{;
    int arraylen = 7, arraylen_new = 0, i = 0, nextstep = 0;
    string line, field, junkText;
    string* randNum = new string[arraylen];
    string* phrase = new string[arraylen];

    ifstream randomTxt;
    arraylen = 5;

    randomTxt.open("random.txt");

    while(!randomTxt.eof())
    {
        getline(randomTxt,line);
        stringstream ss(line);
        nextstep = 0;
        while(getline(ss,field,','))
        {
            cout << i << endl;
            /*if(nextstep == 0)
            {
                randNum[i] = field;
                cout<< "number " << i << ": " << randNum[i] << endl;
                nextstep = 1;
            }

            else if(nextstep == 1)
            {
                junkText = field;
                cout<< "junk " << i << ": " << junkText << endl;
                nextstep = 2;               
            }
            else
            {
                items[i] = field;
                cout<< "items " << i << ": " << items[i] << endl;
            }*/

            if(i == (arraylen-1))
            {
                arraylen_new = arraylen + 1;
                string* temp1 = new string[arraylen_new];
                string* temp2 = new string[arraylen_new];
                copy(randNum, (randNum+arraylen), temp1);
                delete [] randNum;
                randNum = temp1;

                copy(phrase, (phrase + arraylen), temp2);
                delete [] phrase;
                phrase = temp2;
                arraylen = arraylen_new;
            }
        }
        cout<<"test"<<endl;
        i++;

    }    
    randomTxt.close();

    /*for(int i = 0; i < arraylen;i++)
    {
        cout<< "UPC " << i << ": " << UPC[i] << "\t" << "Phrase " << i << ": " << phrase[i] << endl;

    }*/

    return 0;
}

When checking the value for i the print out is as follows:

0
0
0
test
test
2
2
2
test
test
4
4
4

And so on until my file is read completely. My iteration needs to match up properly because I use it to save each piece of the line into separate arrays. Any ideas why it seems to skip the inner while loop?

Here is a snippet of the text file:

333331234567,blah,"this is nonsense"
222221234567,word,"yadda yadda words"
111111234567,thing,"total gibberish stuff"
WorthAShot
  • 81
  • 1
  • 5
  • @NathanOliver Ok my post was edited to include the code in the link. Sorry for the confusion. – WorthAShot Feb 06 '15 at 21:15
  • After fixing a lot of wrong statements (but no logic), your code works for me correctly. Are you sure that is the correct code and text file? – StenSoft Feb 06 '15 at 21:24
  • Free advice: get a modern C++ book that talks about RAII, and forget about `string* randNum = new string[arraylen]`. replace with `std::vector randNum(arraylen)`, you will save yourself lots of time. – kebs Feb 06 '15 at 21:24
  • I am restricted to arrays so no vectors. – WorthAShot Feb 06 '15 at 21:25
  • @StenSoft I d not think its the file but can not say for sure. The idea is read each line, parse at the comma, and save each piece to their corresponding array/throw away string. – WorthAShot Feb 06 '15 at 21:27
  • Arrays, great! Then, as the sizes are const, declare them as so, and switch to `std::array randNum;` ! No more ugly `delete[]`... – kebs Feb 06 '15 at 21:33
  • 1
    [This doesn't come close to compiling](https://ideone.com/dFD0IT). Please also remove any comments that do not add information so that we are left with a [minimal complete verifiable example](http://stackoverflow.com/help/mcve). – gha.st Feb 06 '15 at 21:49

1 Answers1

0

I don't reproduce your issue.

But I had to fix some names in your sample code, so maybe I accidentally fixed it?

Here's my version, with some additional DEBUG print statements.

Also note that checking for eof() is rarely useful, so changed the outer loop to while (getline(randomTxt, line)).

Then with the i loop index, it's more natural to write as a for loop, I think.

Live On Coliru

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

/// returns: new array length

int main() {
    size_t arraylen = 7;
    std::string line;
    std::string *randNum = new std::string[arraylen];
    std::string *phrase  = new std::string[arraylen];

    std::ifstream randomTxt;

    randomTxt.open("random.txt");

    for (size_t i=0; getline(randomTxt, line); ++i)
    {
        std::cout << "DEBUG LINE: '" << line << "'\n";
        //nextstep = 0;

        std::stringstream ss(line);
        std::string field;
        while (getline(ss, field, ',')) {
            std::cout << "DEBUG INNER " << i << " '" << field << "'\n";

            if (i == (arraylen - 1)) {
                size_t arraylen_new = arraylen + 1;
                {
                    std::string *tmp = new std::string[arraylen_new];
                    copy(randNum, randNum + arraylen, tmp);
                    delete[] randNum;
                    randNum = tmp;
                }

                {
                    std::string *tmp = new std::string[arraylen_new];
                    copy(phrase, (phrase + arraylen), tmp);
                    delete[] phrase;
                    phrase = tmp;
                }
                arraylen = arraylen_new;
            }
        }
    }
}

Prints

DEBUG LINE: '333331234567,blah,"this is nonsense"'
DEBUG INNER 0 '333331234567'
DEBUG INNER 0 'blah'
DEBUG INNER 0 '"this is nonsense"'
DEBUG LINE: '222221234567,word,"yadda yadda words"'
DEBUG INNER 1 '222221234567'
DEBUG INNER 1 'word'
DEBUG INNER 1 '"yadda yadda words"'
DEBUG LINE: '111111234567,thing,"total gibberish stuff"'
DEBUG INNER 2 '111111234567'
DEBUG INNER 2 'thing'
DEBUG INNER 2 '"total gibberish stuff"'

Notes

  • you may want make a grow_array function because you duplicated that code
  • you may want to grow by factor instead of 1 element at a time; this won't scale at all and lead to abysmal performance/heap fragmentation
sehe
  • 328,274
  • 43
  • 416
  • 565
  • Thank you for this I will check it out in later today. Also I plan on making a function like you suggested this is just a "rough draft" of my project. Once I get this working I will separate it into its own function. – WorthAShot Feb 06 '15 at 21:58
  • [Live On Coliru](http://coliru.stacked-crooked.com/a/d5cbde812e0ddf6b) (fixed link) – sehe Feb 06 '15 at 22:00
  • I tried it with the for loop and had the same issue. Could there be a problem in the commented section I have where I save each piece of the parsed line to separate arrays? – WorthAShot Feb 06 '15 at 22:07
  • No there couldn't because comments don't get executed. Also, you claim the inner loop isn't being run, so that would mean this code is skipped even if not commented (**PRO TIP**: share the _actual_ code. It's annoying waste of time for us to guess) – sehe Feb 06 '15 at 22:08
  • I edited my code to make sure it runs as is. If you take it right now and run it the output should show the duplication issue I am having. Even changes to the while loop to for loop has the same issue. – WorthAShot Feb 06 '15 at 22:34
  • Huh. Duplication? That's the first time you mention it. You said inner loop was being skipped – sehe Feb 06 '15 at 22:35
  • I _think_ I get your point now. Your problem is really that you're using `eof()` in the wrong way. `eof()` is only true _after_ trying to read past-the-end. – sehe Feb 06 '15 at 22:37
  • Here's with the DEBUG messages (**use** that. Debug your own problems!) **[Live On Coliru](http://coliru.stacked-crooked.com/a/a34e5c2c15fa3162)**. You see, you get the `DEBUG OUTER` repeated with an empty at the end. Of course, then `randomTxt` is not `.good()` anymore (it's `.eof()`) so the inner loop gets skipped. – sehe Feb 06 '15 at 22:41
  • See ***[Why is `eof()` inside a loop condition considered wrong](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong)*** – sehe Feb 06 '15 at 22:43