ifstream reading numbers wrong?

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

int main(int argc, const char * argv[])
{
    std::ifstream infile;
    infile.open("list.txt");
    
    //Check for error
    if (infile.fail())
    {
        std::cerr << "Error opening file" << std::endl;
        exit(1);
    }
    
    std::string line;
    int num;
    if (infile.is_open())
    {
        int lines(0);
        while (getline(infile,line))
        {
            lines++;
        }
        std::vector<std::vector<int>> triangle;
        int value = 0;
        triangle.resize(lines, std::vector<int>(lines, value));
        for (int i = 0; i < lines; i++)
        {
            for (int j = 0; j < lines; j++)
            {
                if (j <= i)
                {
                    infile >> num;
                    triangle[i][j] = num;
                }
            }
        }
        
        /*
        for (int i = 0; i < lines; i++)
        {
            for (int j = 0; j < lines; j++)
            {
                std::cout << triangle[i][j] << " ";
            }
            std::cout << "\n";
        }
         */
    }
    
    else
    {
        std::cout << "Unable to open file";
    }

    
    
    return 0;
}


the program opens the file successfully, and reads in data I think. The
infile >> num seems to not be working each time num = 0. The text file is a pyramid of numbers I'm trying to make a matrix with them in it.
1) What is the issue with the infile >> num ?
2) Is there an easier way to read different amounts of integers in line by line?
3) Side question, is there a better way to do matrices or does using the vector class make sense?

Edit: Through more checking I think that it isn't reading in at all, whatever I set num equal to it simply repeats, so either not reading it in, reading only 0 somehow, or not making num equal whatever it's reading.
Last edited on
The infile >> num seems to not be working each time num = 0.


It's not. You've already read to the end of the file. Once you're at the end of the file, you can't read any more.. unless you clear the error state of the stream an seek to some earlier position in the file.

http://en.cppreference.com/w/cpp/io/basic_istream/seekg
http://en.cppreference.com/w/cpp/io/basic_ios/clear
Ahh perfect thank you, I completely forgot I already read through it to determine the number of rows of the pyramid. I'll close the file and then reopen it. That should work hopefully, time to find out
realized what you were trying to tell me with the links and came up with this code which works perfectly

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
67
68
69
70
71
72
73
74
75
76

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

long max(long x, long y);

int main(int argc, const char * argv[])
{
    std::ifstream infile;
    infile.open("list.txt");
    
    //Check for error
    if (infile.fail())
    {
        std::cerr << "Error opening file" << std::endl;
        exit(1);
    }
    
    std::string line;
    int num;
    if (infile.is_open())
    {
        int lines(0);
        while (getline(infile,line))
        {
            lines++;
        }
        std::vector<std::vector<int>> triangle;
        int value = 0;
        triangle.resize(lines, std::vector<int>(lines, value));
        infile.clear();
        infile.seekg(0, std::ios::beg);
        for (int i = 0; i < lines; i++)
        {
            for (int j = 0; j < lines; j++)
            {
                if (j <= i)
                {
                    infile >> num;
                    triangle[i][j] = num;
                }
            }
        }
        for (int i = lines - 1; i > 0; i--)
        {
            for (int j = 0; j < lines - 1; j++)
            {
                {
                    triangle[i-1][j] += max(triangle[i][j], triangle[i][j+1]);
                }
            }
        }
        std::cout << triangle[0][0] << std::endl;
    }
    
    else
    {
        std::cout << "Unable to open file";
    }

    
    
    return 0;
}

long max(long x, long y)
{
    if (x > y)
    {
        return x;
    }
    else
        return y;
}


it returns the biggest sum in the triangle!
Thanks cire!
Topic archived. No new replies allowed.