3

So I've been working on this program that is supposed do some operations with numbers in an array. But those operations aren't the problem. The problem is that I can't seem to understand how to put 4 numbers from an input file into an array.

When I check it using an output statement with the array and the index 2, it outputs zero. Instead of the number 4.

All the numbers.txt file would include are: 2 4 3 5

#include <iostream> //cin, cout, endl
#include <iomanip>  //manipulators such as setw, setprecision
#include <fstream>  //File I/O
#include <cmath>    //math operators like pow
#include <string>   //string
#include <cassert>  //function assert

using namespace std;
typedef unsigned int uint;
const string fileName = "numbers.txt";


int main()
{
    uint arrayWithNumbers[100];
    uint currentNumber = 0;
    uint limiter = 0;
    ifstream inData;
    inData.open(fileName);

    if(inData)
    {
        while(inData >> currentNumber)
        {
            arrayWithNumbers[limiter] = currentNumber;
            limiter++;
        }//while for array processing
    }//if check for file

    inData.close();
    cout << arrayWithNumbers[2] << endl;

}//main
Bosco
  • 45
  • 4
  • 1
    Theoretically, it would output 3, since arrays are zero-based. –  Apr 11 '19 at 05:37
  • If you step through the code, line by line, in a debugger, does it behave as you expect? Does it read the values that should be in the file? – Some programmer dude Apr 11 '19 at 05:38
  • what does your `.txt`-file look like? – skratchi.at Apr 11 '19 at 05:41
  • 1
    Oh shit I had commas in between the numbers and then I took them out, and it works. Thanks dude. Why do commas stop the input like that? Or does a comma just equal zero when you output in that way? – Bosco Apr 11 '19 at 05:42
  • I'm going to guess that it counts the comma as a string character, not an int or uint. Thus, it fails. Not sure enough about that to answer that though. Have to research. Maybe @skratchi.at would know. –  Apr 11 '19 at 05:47
  • 2
    When you do `inData >> currentNumber` the code tries to read an unsigned integer (the type of `currentNumber`) and *only* an unsigned integer. If there's any character in the input that can't be parsed as an unsigned integer (for example a comma) then the input will fail. That will lead to the loop ending and you printing an *uninitialized* element of the array, an uninitialized element whose value is *indeterminate* and when you print it you will have *undefined behavior*. – Some programmer dude Apr 11 '19 at 05:47
  • If you want the commas, see [this answer](https://stackoverflow.com/a/40946467/10957435) –  Apr 11 '19 at 05:55
  • Possible duplicate of [Reading a comma separated file into an integer array](https://stackoverflow.com/questions/40945378/reading-a-comma-separated-file-into-an-integer-array) –  Apr 11 '19 at 05:57

1 Answers1

1

As you limit your read operation inData >> currentNumber to the type of uint, it will fail to read a char. A comma is a char. This leads then to an end of the loop.

And you are lucky the output shows 0. You access uninitialized memory, which I strongly advice you should not do.

uint arrayWithNumbers[100] = {0};
skratchi.at
  • 1,151
  • 7
  • 22