0

I'm having an issue with my dynamic array. I'm trying to input the values that are into a text file into a dynamic array of length where size is the size of my text file.

For some reason I'm getting -842150451 repeated, idk if its something simple as not properly intializing it or maybe im using a << in the wrong way.

PS: I tried debugging and even if I print out the individual index, I still get that value.

My code:

    cout << "\nEnter a command and file ";
    getline(cin, line);
    command = line.substr(0, line.find(" "));
    names = line.substr(line.find(" ")+1);
    ifstream readText;
    readText.open(names += ".txt");
    int size;
    readText >> size;
    int *someArray = new int[size];
    
    
    if (command == ("Start"))
    {
        for (int i = 0; i < size; i++)
        {
            readText >> someArray[i];

        }
        for (int i = 0; i < size; i++)
        {
            cout << someArray[i] << endl;
        }
        
    }

    else if (command == "do this")
    {
            
        //used to test to see if it works
        cout << someArray[0] << endl;
        cout << someArray[1] << endl;
        cout << someArray[2] << endl;
     }

the contents are just values like

94032

233332

2121

112

423424

Community
  • 1
  • 1
xiashian
  • 1
  • 4
  • This code is correct as long as it fits the contents of your input stream. Please provide a [mcve] including the input that reproduces the problem. – Baum mit Augen Sep 18 '15 at 23:16
  • i just provided the lines of code that i used for the input thank you for taking the time to look at my problem – xiashian Sep 18 '15 at 23:20
  • Better, but especially the contents of the file you read would be interesting. – Baum mit Augen Sep 18 '15 at 23:21
  • And you should check whether or not the file was opened successfully and the input operations succeeded. – Baum mit Augen Sep 18 '15 at 23:22
  • the values are just plain numbers like 94032 233332 2121 112 423424 except they each have their own line, – xiashian Sep 18 '15 at 23:24
  • when the user enters Start and the name of the file the contents of the file are shown properly – xiashian Sep 18 '15 at 23:25
  • Alright, so the first number you read is used as the size of your array and to determine the length of the input. As your code stands right now, it will attempt to read 94032 integers, and the file must contain at least that many after the initial integer. Is that the case? If not, add a new line containing the number of integers you want to read to the very beginning of the input file. – Baum mit Augen Sep 18 '15 at 23:26
  • ahh i see, and the first value is 397 and the number of elements i need to print is 397 – xiashian Sep 18 '15 at 23:29
  • somethign i noticed is that when i print out the values of the text file the first value is not printed out, but i belive like you said it uses that first value as the size of the text file – xiashian Sep 18 '15 at 23:33
  • IMPORTANT note: initialize `int size` to 0, unless you can point to a spec which says that the `>>` operator will always set a value, even if it doesn't find a number (otherwise, you'll create an array with a garbage-value size if there was no number at the beginning of the stream) – iAdjunct Sep 18 '15 at 23:34
  • Well of course it does, you consume the first integer from the file with `readText >> size;` and save it in `size`. So the first integer needs to be exactly one less then the total number of integers in the file. – Baum mit Augen Sep 18 '15 at 23:35
  • iAdjunct thank you for your feedback i set the size = 0 to, but it returns other garbage values although not the same repeated value – xiashian Sep 18 '15 at 23:36
  • Try `memset ( someArray , 0 , sizeof(someArray) )` post-`new` and see if that changes anything. – iAdjunct Sep 18 '15 at 23:38
  • 1
    Btw, -842150451 is cdcdcd in hex, I believe that MSCV uses something like that to indicate uninitialized memory in debug mode. And if you only want to get the job done, have a look at [this](http://stackoverflow.com/questions/14162293/read-int-array-of-unknown-length-from-file). – Baum mit Augen Sep 18 '15 at 23:40
  • int *someArray = new int[size]; memset(someArray, 0, sizeof(someArray)); – xiashian Sep 18 '15 at 23:41
  • and the first index is 0 followed by garbage values – xiashian Sep 18 '15 at 23:42
  • 1
    @iAdjunct That line is wrong, `sizeof(someArray) == sizeof(void*)`. Also, using `memset` in C++ is stupid, use `std::fill` instead. And `new int[size]` is not really better and should be `std::vector(size)` instead. – Baum mit Augen Sep 18 '15 at 23:42
  • 1
    @BaummitAugen - Right, forgot he defined it that way. And no, it's not [always] stupid - memset can have performance benefits when all you want to do is zero out an array. – iAdjunct Sep 18 '15 at 23:45
  • @iAdjunct I do not believe that without prove. It would be easy for the implementation to detect when it is dealing with POD / trivially copyable types and pointers. I would be *very* surprised if any of the big implementations would not optimize this with `memset` behind the scenes. Heck, compilers have been optimizing entire loops to `memset` for years, even without any template magic. – Baum mit Augen Sep 18 '15 at 23:48
  • i tried it with vector implementation and prints out garbage values , hmm the only other thing i can think of is that i am using an else if statement and printing out the values of the first 3 index and coming out with garbage values – xiashian Sep 18 '15 at 23:53

0 Answers0