mardi 2 avril 2019

When I write number to an array, there is always one extra and repetitive last number

For my homework, I need to open a file and read numbers form it. Then assigning each number to an array. However, the last number always added twice to the array.

the numbers in the file 8 1 9 1 7 2 10 8 2 6 2 7 3 10 10 10 8 1

the output looks like this pin[0]=8 pin1=1......pin[16]=8 pin[17]=1 pin[18]=1

my code void arrayFiller(int arrayP[]) {

ifstream cleanout9;

cleanout9.open("cleanlane9.txt");

while(cleanout9.fail())
{
    cout<< "fail to open file";
    exit(-1);
}

int i=0;

while(! cleanout9.eof())//&&i<18) If I add this, I can control the output, do I have other met[enter image description here][1]hods?
{
    int num;
    cleanout9>>num;
    arrayP[i]=num;
    cout<<"\npin["<<i<< "]"<<"="<<arrayP[i]<<"   ";
    i++;
}

cleanout9.close();

}

Aucun commentaire:

Enregistrer un commentaire