-1

I have to read a file input.txt which always has N lines. Each line consist of two integers. Specifically, I am reading a file where both integers are 2^(line_index -1).

int temp1, temp2;
std::vector<int> vec1, vec2;
std::fstream fh("input.txt", std::ios_base::in);    
for (int i = 0; i < N; i++) {
    fh >> temp1 >> temp2;
    vec1.push_back(temp1);
    vec2.push_back(temp2);
}
//first few lines of input are
//1 1
//2 2
//4 4
// . . . 
//Line 31 should be: 2147483647 2147483647
//but my code read it as 2147483647 1073741824
//This is always the case for N>30

After line 30, as you can see on the snippet above, file reading became weird. Is there a problem in my code? Or my method of reading the file sort of limits the variable that I can input?

BlueAxis
  • 3
  • 2
  • Can you post full code needed to reproduce the problem? Including relevant data? Like declaration of `temp1` and `temp2` and `vec2` and `vec1` and `N` and so on? – KamilCuk Mar 15 '19 at 11:42
  • 2
    2147483647 is a pretty big number, in fact it is the largest number possible to hold in a 32 bit integer. If you declared `temp` as an integer this problem will occur for large numbers. You might want to try the `long` datatype – Hoog Mar 15 '19 at 11:44
  • I've updated the code snippet. Yes you are right that I used only int. I think that was the problem in the syntax. Thanks – BlueAxis Mar 15 '19 at 11:53

1 Answers1

1

You are reaching the integer limit, see for example this question. Not sure why you want to do this, but if you want to save bigger values, you need a different data type.

Thomas
  • 4,696
  • 5
  • 36
  • 71