Can someone please help me with figuring out why my codes does not run. I have tried to familiarize myself with how to get data from a file and discovered i need to use fstream. The data of the file is somethin like this:

19531029L
20081104L
19770615L
19300723L
19631122L
19410501L

I tried simply to read from the file but got the first number and the rest zeros when i wanted it printed out. IT NEEDS TO BE STORED IN A VECTOR AND AS A LONG INT. Can someone please tell me whats wrong with my code. I think i need to change the value to strings first but am i using the correct type? I keep getting an error that state cannot conv std::string to char.

Any help would be great

Recommended Answers

All 4 Replies

>>Can someone please tell me whats wrong with my code
Weeeeellll, since you didn't post your code and I can't see your monitor from where I sit, the answer is obviously NO.

>>Can someone please tell me whats wrong with my code
Weeeeellll, since you didn't post your code and I can't see your monitor from where I sit, the answer is obviously NO.

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

int main(){
	
	string temp;
	long int x;
	long int num;
	int y=0;
	int countlines;
	long int alongarray[15];
	vector<long int> dateList(11);

	ifstream inFile;
	inFile.open("C:\\inputdata.txt");

	/*if (!inFile) {
		cerr << "Unable to open file datafile.txt";
		exit(1);   // call system to stop
	}
	*/

	while(!inFile.eof()){
		inFile.getline(temp, 100);
		alongarray[countlines] = atol(temp);
		countlines += 1;

		//getline(inFile>>x,line);
		//i=strtol(temp,0,10);
		//num = atol(line);
		//num = strtol(line,0,11);
		//dateList[y] = num;
	}

	for (int i=0; i < dateList.size(); i++)
		cout << dateList[i] << " ";

	inFile.close();

you don't want to use getline() since the data is integers. And you don't need alongarray either. You can replace lines 26-39 with just these few lines.

vector<long int> dateList; // no need to specify size in advance
long int num;
while( inFile >> num )
{
    dateList.push_back(num);
}
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.