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?

Recommended Answers

All 3 Replies

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.

while (!myReadFile.eof()) {


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

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

myReadFile.close();

silly me.. thanks, it does work!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.