### Problem with arrays

Hello everyone, for this program im trying to simply read a text file which contains 12 numbers total. And i must separate each in an even and odd array. So far i can open the file, and im able to put the even and odd numbers in their proper array. But when i checked the output, I got this:

20012148000040
045000

My actual text file is this:
20
45
12
14
8
7
3
1
25
40
34
22

So i know there are 7 evens, and 5 odds. I believe its the size that is wrong, but the computer doesnt know that size until i start to input it in its proper array.
Basically I want to find out the number of evens and odds so i can put that size in the beginning of my array so it can output it correctly, because right now it looks like its just filling in some spaces with zero's.
ANY help will be appreciated. Thank you!

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main ()
{
ifstream inputFile;
inputFile.open("textfile.txt");

if (!inputFile.is_open())
{cout<<"File did not open."<<endl;
return 0;}

const int SIZE=15;
int arrayEven[SIZE]={};
int arrayOdd[SIZE]={};
int values, i,count1=0, count2=0;

while (!inputFile.eof())
{
for (i=0;i<SIZE;i++)
{
inputFile>>values;
if (values%2==0)
{arrayEven[i]=values;
count1++;}
else
{arrayOdd[i]=values;
count2++;}
}

}
for (i=0;i<count1;i++)
{cout<<arrayEven[i];}
cout<<endl;
for (i=0;i<count2;i++)
{cout<<arrayOdd[i];}
cout<<endl;

inputFile.close();

system ("pause");
return 0;
}
very simple.

Suspected Problem 1:
I am suspecting that you're inputting using a file.
It is passed in as a string/char.. not integer as expected (because you're passing into "values" variable, which is declared as int right?)
Hence, you can try manually input the numbers first using cin..
if you can do that, it means it's just simple I/O problem.

Solution:
convert the line-by-line input from the file from string into integer first.
function: atoi

Suspected Problem 2:
 `` `` ``for (i=0;i

you forgot to declare what i is. (compilation will have error)

Thanks

When you write the number to the array you write it to position i. Shouldn't you write it to position count1 for even numbers and count2 for odd numbers?

The loop for reading in the numbers always iterate 15 times even if the file contains less numbers. This will probably result in the last number being added multiple times at the end. To fix this you should stop the loop when `inputFile>>values` fails. You can check that the number was read successfully by putting it inside an if statement like this
 ``1234`` ``````if (inputFile>>values) { // success }``````

or
 ``1234`` ``````if (!(inputFile>>values)) { // failure }``````

You can also but it inside the loop condition if you like
`for (i=0;i<SIZE && inputFile>>values;i++)`

`while (!inputFile.eof())`
I don't think you really want this. If there are more numbers in the file you don't want to run what's inside again because if you do then you are likely to run out of bounds.

Many of these problems could be solved much easier with the use of std::vector. If you are allowed to use it and you know how to use I recommend you use it. It's definitely something worth learning about.
Last edited on
Topic archived. No new replies allowed.