Oct 1, 2008 at 4:34am UTC
I am reading a file that has a number that is 1000 ineters long. It is 3156353653.... but I want to fill a vector for each number separately. That is make my program read one number at a time.
My program looks like so as of now
#include <iostream>
#include <vector>
#include <stdexcept>
#include <ios>
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::cerr;
//prototype
void read_file(vector<int>&);
int main(){
vector<int> Integers;
//read in vectors
try{
read_file(Integers);
}
catch(std::domain_error e) {
cerr << "You had the following error: " << e.what() << endl;
return 1;
}
cout << Integers[0] << " " << Integers[1] << endl;
return 0;
}
void read_file(vector<int>& values){
unsigned long int x;
while(cin >> x )
values.push_back(x);
if(!cin.eof() && cin.fail())
throw std::domain_error("You have entered a wrong character (enter an integer).");
}
Oct 1, 2008 at 12:03pm UTC
You have to read the file one character at a time.
Oct 1, 2008 at 12:31pm UTC
I'm not really sure about this but I guess you could use .get() to get a character, then atoi it and then push it into a vector in a for loop.