0

I want to obtain integers from file that has strings too, and store them into array to do some operation on them. the integers can be 1 or 12 or 234, so 3 digits. I am trying to do that but the output stops when I run the code

void GetNumFromFile (ifstream &file1, char & contents)
{
    int digits[20];
    file1.get(contents);
    while(!file1.eof())
    {
        for (int n = 0; n < 10; n++)
        {
            if(('0' <= contents && contents <= '9') && ('0' >= contents+1 && contents+1 > '9'));
            digits[n]=contents;
            if(('0' <= contents && contents <= '9') && ('0' <= contents+1 && contents+1 < '9'));
            digits[n]=contents;
            if(('0' <= contents && contents <= '9') && ('0' <= contents+1 && contents+1 <= '9') && ('0' <= contents+2 && contents+2 < '9'));
            digits[n]=contents;
        }
        continue;
    }
    for (int i = 0; i <= 20; i++)
    {
        cout << *(digits + i) << endl;
    }
}
Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
Mar
  • 43
  • 5
  • 2
    What does the file actually look like, and what are you having trouble with exactly? Please be more specific. Also, your `if` statements have erroneous `;` on them. Also, see [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/). – Remy Lebeau Oct 09 '19 at 22:16
  • Your code has so many errors in it that your best next step might be to [do some debugging](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) on your own. Once you see the more specific problems, see if they are already addressed on SO, and ask for a solution if they are not. – JaMiT Oct 10 '19 at 01:25

3 Answers3

0

First observation: you iterate out of bounds of the array:

int digits[20];
for (int i = 0; i <= 20; i++)

20 elements and 21 iteration. That is an undefined behavior, so everything is possible here (if your program eventually gets here).

Next, you read from file once and then you have an infinite loop because the expression !file1.eof() is either true or false for the rest of the program run. Isn't that the reason of "output stops"?

The third finding: your if statements are useless because of the semicolon after the statement:

if(('0' <= contents && contents <= '9') && ('0' >= contents+1 && contents+1 > '9'));
            digits[n]=contents;

You just assign digits[n]=contents; without any check.

I neither see any reason of providing a reference to char in the function. Why not to make it a local variable?

Dmitry Kuzminov
  • 4,427
  • 5
  • 12
  • 26
0

You will need first to add get() functionality inside the loop as well in order to reach end of file.

Forthmore try to add a while loop once a char was found to be an integer to continue in asking for the next character.

e.g.

int digits[20];
int i = 0;
ifstream file1("filepath");
char contents;
while (!file1.eof())
{
    file1.get(contents); // get the next character

    if (contents <= '9' && contents >= '0' && i < 20) // if character is in number range
    {
        digits[i++] = contents - '0'; // converting the chat to the right integer
        file1.get(contents);

        while (contents <= '9' && contents >= '0' && i < 20) // while is integer continue on
        {
            digits[i++] = contents - '0';
            file1.get(contents);
        }
    }
}

// do other stuff here
Josh W.
  • 87
  • 7
0

You have to deal with the number of digits of the number found:

int digits[20];
int i = 0;
short int aux[3]; // to format each digit of the numbers

ifstream file1("filepath");
char contents;

file1.get(contents); //first char

if (!file1.eof()) //test if you have only one char in the file
{
    while (!file1.eof() && i < 20) // limit added to read only 20 numbers
    {
        if (contents <= '9' && contents >= '0') // if character is in number range
        {
            aux[0] = contents - '0'; // converting the char to the right integer
            file1.get(contents);

            if (contents <= '9' && contents >= '0') // if contents is number, continue on
            {
                aux[1] = contents - '0';

                if (!file1.eof()) // if has mor char to read, continue on
                {
                    file1.get(contents);

                    if (contents <= '9' && contents >= '0') // if is integer, continue on
                    {
                        aux[2] = contents - '0';
                        file1.get(contents); // will read same of last char if eof, but will have no effect at all
                        //aux[0] *= 100; // define houndred
                        //aux[1] *= 10; // define ten
                        digits[i++] = (aux[0] * 100) + (aux[1] * 10) + aux[2];
                    }
                    else
                    {
                        //aux[0] *= 10; // define ten
                        digits[i++] = (aux[0] * 10) + aux[1];
                    }
                }
                else
                {
                    digits[i++] = (aux[0] * 10) + aux[1];
                }
            }
            else
            {
                digits[i++] = aux[0];
            }
        }
    }
}
else if (contents <= '9' && contents >= '0' && i < 20) // check if the only one char is number
{
    digits[i++} = contents - '0';
}

If you want read an undefined size number, then you will have to allocate memory to format each digit of the numers with new (c++) or malloc(c/c++).

Giuliano
  • 90
  • 6