Trying to input numbers from text file into array with Stringstream

I'm relatively new to stringstream, and to inputting numbers from a text file into an array. Right now, I just want to store the odd and even numbers from a file into their respective arrays. I think I'm screwing up the stringstream, but am not sure. Right now, it is just storing a few numbers into each array.

Thanks for looking.

INPUT:

46 30 82 90 56 17 95 16 48 26
4 58 0 78 92 60 12 21 63 47
19 41 90 85 14 -9 52 71 79 16
80 51 95 102 34 10 79 95 61 92
89 88 66 64 92 63 66 64 39 5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <sstream>
#include <fstream>

using namespace std;

void oddOrEven(int [], int [], int []);

ifstream fileIn;

int main()
{

int evenArray[] = {};
int oddArray[] = {};
int array[] = {};
string line;

fileIn.open("file.txt");

if (!fileIn){
cout << "\nError, could not open file.\n";
}

oddOrEven(evenArray, oddArray, array);

fileIn.close();

    return 0;
}


void oddOrEven(int evenArray[], int oddArray[], int array[]){

int a = 0;
int b = 0;
string line;

for (int i = 0; getline(fileIn, line); i++){
stringstream ss(line);

ss >> array[i];

if (array[i] % 2 == 0){
evenArray[i] = array[i];
a++;
}

else{
oddArray[i] = array[i];
b++;
}

}

cout << "\nEven Array\n\n";

for (int i = 0; i < a; i++){
cout << evenArray[i] << endl;
}

cout << "\nOdd Array\n\n";

for (int i = 0; i < b; i++){
cout << oddArray[i] << endl;
}}
If your input does indeed have multiple numbers on one line, then your code is not parsing the input correctly. I don't really think you need the extra complexity of using getline() and a stringstream when you could just use the extraction operator on fstream to get one value at a time:

1
2
3
4
5
6
7
int input;
while (fileIn.good())
{
    filein >> input;
    ...
    // Do your sorting to even/odd here.
}


It also seems unnecessary to have an extra array when you're immediately sorting to even and odd arrays.
Well, the stringstream is a bit of an overkill for this task. In its current form, it does more harm than good, as only the very first number from each line is processed.

However, that's a relatively small problem.There are other issues. The array declarations give a compiler error, a zero-size array is not permitted.
Try changing this:
1
2
3
    int evenArray[] = {};
    int oddArray[] = {};
    int array[] = {};
to something like this:
1
2
3
    int evenArray[100] = {0};
    int oddArray[100] = {0};
    int array[100] = {0};
Here I've declared each array can hold 100 elements and initialised the contents with zeros.

Inside the function oddOrEven()
1
2
3
4
    if (array[i] % 2 == 0) {
        evenArray[i] = array[i];
        a++;
    }
the incorrect subscript is used when storing the values.
It should be:
1
2
3
4
    if (array[i] % 2 == 0) {
        evenArray[a] = array[i];
        a++;
    }

and the same applies to the other array, where b should be used in place of i.

That still leaves the stringstream. Either remove it completely (recommended) and just do fileIn >> array[i] rather than getline(). Or if you want to practice using stringstreams, add an inner loop to process the stringstream in this manner, just as though it was a file.

Rather than this:
1
2
3
4
int input;
while (fileIn.good())
{
    filein >> input;
it is preferable to do this:
1
2
3
int input;
while (filein >> input)
{

This way, any read failures including end of file will be correctly handled.
Ah I learned something too. interesting.
Topic archived. No new replies allowed.