0

i've been trying to store a coordinate in a vector array, and reading the coordinates from a .txt file.

there was no error, but the size of the vector is still 0 (i'm sure there was no value in the vector array)

here's my code:

#include <stdafx>
#include<iostream>
#include<fstream>
#include <stdio>
#include <vector>

using namespace std;

int main() {

ifstream myReadFile;
myReadFile.open("input test.txt");

vector<double> xVal,yVal;
int vectorSize = (int)xVal.size();

double x,y;

while (!myReadFile.eof()) {


myReadFile>>x>>y;
xVal.push_back(x);
yVal.push_back(y);

}
cout<<vectorSize<<endl;

myReadFile.close();
return 0;

}

any guide from the experts?

3
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by hayatihamid
0

Looks as if you may not have updated "vectorSize" after loading the file, so you are reporting the size of the vector before populating it.

0
while (!myReadFile.eof()) {


myReadFile>>x>>y;
xVal.push_back(x);
yVal.push_back(y);

}
cout<<xVal.size()<<endl; // try this

myReadFile.close();
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.