I am trying to use a integer vector to store an unknown number of positive integers from a text file. In general I have no problems pushing data into vectors, but in this case I am getting compile errors when I try to push an integer from the fstream to my vector. I could use a dynamic array or a stack, but I am adamant on learning to use vectors. My code is as follows:
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
vector<size_t> nTxtToVec(string);
void main() {
vector<size_t> allP = nTxtToVec("primes.txt");
for (size_t i : allP) // test all integers inside vector
cout << i << ' ';
cout << "\n\nPress any key to return...";
cin.clear();
cin.ignore(10000, '\n');
return;
}
vector<size_t> nTxtToVec(string fin) {
vector<size_t> v;
size_t n = NULL;
char c;
fstream is;
is.open(fin, ios::in);
while (!is.eof()) {
is.get(c);
while (c >= '0' && c <= '9') {
if (n == NULL)
n = c - '0';
else
n = (n * 10) + (c - '0');
is.get(c);
}
if (n != NULL) {
v.push_back(n);
n = NULL;
}
}
is.close();
return v;
}
With this code I get one error and one warning:
error C2440: 'initializing' : cannot convert from 'std::vector<int,std::allocator<_Ty>>' to 'int'
and
warning C4018: '<' : signed/unsigned mismatch
I used "size_t" to replace all my "int" datatypes to try and avoid the signed/unsigned mismatch warning, but the compiler says the same thing even though the text file contains only positive integers and whitespace. I have also tried initializing the vector in main() and passing the vector to a void function rather than using a vector as the function return type. I am new to C++ so forgive me if didn't do something blatantly obvious or stupid.