Skip to main content
deleted 14 characters in body
Source Link
Havenard
  • 28.1k
  • 5
  • 38
  • 66

Arrays are basic structures and they don't hold information about how many elements you actually filled with data, you can only tell it's total size in memory with sizeof(x) and the size of one of it's elements with sizeof(*x).

However, we are in C++, not C, and there's really no reason you should be using arrays here. Use std::vector instead.

std::vector<double> x; std::vector<double> y; x.reserve(1000); // reserve 1000 elements, optional, y.reserve(1000); // it will resize automatically if needed while (true) { double d; fin >> d; if (fin.eof()) break; // end found x.push_back(d); fin >> d; y.push_back(d); } 

Now you can tell the number of elements from x.size(), you don't have to worry about buffer overflow because it will resize automatically if there are more than 1000 elements, also don't have to worry about counting how many times you looped, etc.

Someone put a lot of work on making those standard C++ templates so that you don't have to waste your own time time reinventing the wheel every time, use them.

Arrays are basic structures and they don't hold information about how many elements you actually filled with data, you can only tell it's total size in memory with sizeof(x) and the size of one of it's elements with sizeof(*x).

However, we are in C++, not C, and there's really no reason you should be using arrays here. Use std::vector instead.

std::vector<double> x; std::vector<double> y; x.reserve(1000); // reserve 1000 elements, optional, y.reserve(1000); // it will resize automatically if needed while (true) { double d; fin >> d; if (fin.eof()) break; // end found x.push_back(d); fin >> d; y.push_back(d); } 

Now you can tell the number of elements from x.size(), you don't have to worry about buffer overflow because it will resize automatically if there are more than 1000 elements, also don't have to worry about counting how many times you looped, etc.

Someone put a lot of work on making those standard C++ templates so that you don't have to waste your own time time reinventing the wheel every time, use them.

Arrays are basic structures and they don't hold information about how many elements you actually filled with data, you can only tell it's total size in memory with sizeof(x) and the size of one of it's elements with sizeof(*x).

However, we are in C++, not C, and there's really no reason you should be using arrays here. Use std::vector instead.

std::vector<double> x; std::vector<double> y; x.reserve(1000); // reserve 1000 elements, optional, y.reserve(1000); // it will resize automatically if needed while (true) { double d; fin >> d; if (fin.eof()) break; // end found x.push_back(d); fin >> d; y.push_back(d); } 

Now you can tell the number of elements from x.size(), you don't have to worry about buffer overflow because it will resize automatically if there are more than 1000 elements, also don't have to worry about counting how many times you looped, etc.

Someone put a lot of work on making those standard C++ templates so that you don't have to waste time reinventing the wheel every time, use them.

added 49 characters in body
Source Link
Havenard
  • 28.1k
  • 5
  • 38
  • 66

Arrays are basic structures and they don't hold information about how many elements you actually filled with data, you can only tell it's total size in memory with sizeof(x) and the size of one of it's elements with sizeof(*x).

However, we are in C++, not C, and there's really no reason you should be using arrays here. Use std::vector instead.

std::vector<double> x; std::vector<double> y; x.reserve(1000);  // reserve 1000 elements, optional, std::vector<double> y.reserve(1000);  // it will resize automatically if needed while (!fin.eof()true) { double d; myfilefin >> d; if (fin.eof()) break; // end found x.push_back(d);   myfilefin >> d; y.push_back(d); } 

Now you can tell the number of elements from x.size(), you don't have to worry about buffer overflow because it will resize automatically if there are more than 1000 elements, also don't have to worry about counting how many times you looped, etc.

Someone put a lot of work on making those standard C++ templates so that you don't have to waste your own time time reinventing the wheel every time, use them.

Arrays are basic structures and they don't hold information about how many elements you actually filled with data, you can only tell it's total size in memory with sizeof(x) and the size of one of it's elements with sizeof(*x).

However, we are in C++, not C, and there's really no reason you should be using arrays here. Use std::vector instead.

std::vector<double> x(1000); // reserve 1000 elements, optional, std::vector<double> y(1000); // it will resize automatically if needed while (!fin.eof()) { double d; myfile >> d; x.push_back(d); myfile >> d; y.push_back(d); } 

Now you can tell the number of elements from x.size(), you don't have to worry about buffer overflow because it will resize automatically if there are more than 1000 elements, also don't have to worry about counting how many times you looped, etc.

Someone put a lot of work on making those standard C++ templates so that you don't have to waste your own time time reinventing the wheel every time, use them.

Arrays are basic structures and they don't hold information about how many elements you actually filled with data, you can only tell it's total size in memory with sizeof(x) and the size of one of it's elements with sizeof(*x).

However, we are in C++, not C, and there's really no reason you should be using arrays here. Use std::vector instead.

std::vector<double> x; std::vector<double> y; x.reserve(1000);  // reserve 1000 elements, optional, y.reserve(1000);  // it will resize automatically if needed while (true) { double d; fin >> d; if (fin.eof()) break; // end found x.push_back(d);   fin >> d; y.push_back(d); } 

Now you can tell the number of elements from x.size(), you don't have to worry about buffer overflow because it will resize automatically if there are more than 1000 elements, also don't have to worry about counting how many times you looped, etc.

Someone put a lot of work on making those standard C++ templates so that you don't have to waste your own time time reinventing the wheel every time, use them.

Source Link
Havenard
  • 28.1k
  • 5
  • 38
  • 66

Arrays are basic structures and they don't hold information about how many elements you actually filled with data, you can only tell it's total size in memory with sizeof(x) and the size of one of it's elements with sizeof(*x).

However, we are in C++, not C, and there's really no reason you should be using arrays here. Use std::vector instead.

std::vector<double> x(1000); // reserve 1000 elements, optional, std::vector<double> y(1000); // it will resize automatically if needed while (!fin.eof()) { double d; myfile >> d; x.push_back(d); myfile >> d; y.push_back(d); } 

Now you can tell the number of elements from x.size(), you don't have to worry about buffer overflow because it will resize automatically if there are more than 1000 elements, also don't have to worry about counting how many times you looped, etc.

Someone put a lot of work on making those standard C++ templates so that you don't have to waste your own time time reinventing the wheel every time, use them.