Whitespace in Character Arrays

Hi. I'm trying to write a program that will read a list of numbers from a file
into character arrays. The numbers in the file are arranged similar to this:

12345 54321
11111 22222
00011 55678

As you can see, each line consists of two numbers, separated by whitespace. I
want to read the first half of the number (e.g. 12345) into one array, and then
read the second half into another array (e.g. 54321). I will then add the two
numbers together ( I realize they'll have to converted to int ). After the
numbers are added, the process starts over for the next line of numbers.

I'm having a terrible time trying to understand how to do this. Character
processing just kills me for some reason. I've played around with isspace()
and '\n', trying to create conditions that will cause the second number on a
line go into a second array, but I've had no success.

This is the code I've written to put the char numbers into the array. It does
compile, but obviously doesn't create a second array (I had one created, but it
wasn't working). Nor does it move on to the next line of data.

Could someone give me a clue as to what I'm missing here?

Thanks.

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
#include <cctype>
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
    typedef char value_type;
    typedef size_t size_type;
    
    static const size_type CAPACITY = 15;
    
    value_type str1[CAPACITY];
    size_type index;
    size_type used = 0;
    
    char chardigit;
    
    ifstream inData;
    
    inData.open("stringdata.txt");
    
    inData.get(chardigit);
    
    while (inData && (chardigit != '\n'))
    { 
          str1[used] = chardigit;
          ++used;
          
          inData.get(chardigit);
          
    }
    
    
    
    // OUTPUT TO SEE WHAT IS HAPPENING IN THE ARRAY

    cout << "\n\nYou Entered: " << endl;
    
    used = 0;
    
    for (int i=0; i < CAPACITY; ++i)
    {
        cout << "str1[" << used << "] = " << str1[i] << endl;
        ++used;
    }
    
    cout << "\n\n... Or ... \n" << endl;
    
    used = 0;
    
    for (int i=0; i < CAPACITY; ++i)
    {
        cout << str1[i];
        ++used;
    }

return EXIT_SUCCESS;
}





I haven't recently done any file input, but you should be able to read right into your integers.

inData >> firstInt;
inData >> secondInt;

Something like that should work.
Topic archived. No new replies allowed.