Help reading numbers from a file

I'm trying to write a program that will read in some numbers and add them from a file.

This is the file:

1
2
3
4
5
6
7
8
9
10
11
12
7
9
46123
85321
3872663
69826
2222222222222222222222222
7777777777766666666666666
9999999999999999999999999
9999999999999999999999999
5555555555555555555555555
999999999999999


I have to read in each line individually with cin.get() then convert the char to an int. The problem is, I don't know how to discard the newline character and get the number 9 after the number 7.

This is my code:

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
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

const int NUMBERS = 26;

ifstream infile;
ofstream outfile;

void read(int storage[])
{
    int i = 0, k = 0, convert = 0;
    char check;

    infile.open("MyNumbers.txt");

    if (!infile)
    {
        cout << "Failed to open data file!\n";
        cin.get();
        exit(0);
    }

    while (infile.get(check))
    {
        if (check != '\n' && check != '\r')
        {
            convert = check - '0';

            cout << "Convert Variable: " << convert << endl;
        }

        else
        {
            break;
        }

    }

    infile.close();
}

void add_arrays(int first_array[], int second_array[], int sum_array[])
{
    for (int i = 0; i < NUMBERS; i++)
    {

    }
}

int main()
{
    int first_array[NUMBERS] = {0}, second_array[NUMBERS] = {0}, sum_array[NUMBERS] = {0},
    storage[NUMBERS] = {0};

    read(storage);
    add_arrays(first_array, second_array, sum_array);

 //   cin.get();
    return 0;
}


I need to get the first two lines, add them together in an array, then output that result. Then, I need to get the next two numbers in the file, add them together, and output the result and so on. I'm not sure how to ignore the newline and get the next numbers in the file. I'm kind of lost, what would be the best way to do this? Thanks if you can help.
Last edited on
I'm not going to say it's the 'best' way of doing this, as there are a few ways to do this, but...

If you use getline() saving the data to a temp string, then use a stringstream on it.

i.e.
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
#include <iostream>
#include <sstream> //Also includes <string>
#include <fstream>

int main()
{
	std::ifstream iFile;

	iFile.open( "text.txt" );

	std::string input = "";
	int myInt = 0;

	if( iFile.is_open() )
	{
		//Get everything up until the \n
		//Which will also discard the \n
		std::getline( iFile, input, '\n');

		//Create the stringstring object and pass it the input.
		std::stringstream ss( input );

		if( ! ( ss >> myInt ) ) //If the conversion fails...
			std::cout << "Error converting input to an int.\n";

		//More code for getting data...

		iFile.close();
	}
	else
		std::cout << "Error opening file.\n";

	return 0;
}


Hope this helps. (:
Topic archived. No new replies allowed.