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:
My actual text file is this:
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!
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.
convert the line-by-line input from the file from string into integer first.
Suspected Problem 2:
your for loop:
you forgot to declare what i is. (compilation will have error)
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
1 2 3 4
1 2 3 4
You can also but it inside the loop condition if you like for (i=0;i<SIZE && inputFile>>values;i++)
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.