Reading in formatted data from file

So I have to read formatted data from a text file in to a vector.
The data looks like this and is stored in a .txt
The only data I care about for this problem is reading in the data in the right column(the double).
(0,50)
(1,55)
(2,58.4)
(3,55.6)
(4,57)

I have no problem reading in data and through looking up my problem figured out how to read in data such as 1,5,7,5,6,etc.

The basic:
1
2
3
4
5
6
7
int main(){
int hour;
double temperature;
cout<<"Please enter the filepath of the input file"<<endl;
string filepath;
cin>>filepath;
ifstream ist(filepath);

From reading http://www.cplusplus.com/reference/iostream/istream/ignore/
I thought maybe this would work
1
2
3
4
5
6
7
8
9
10
11
12
13
vector<double> temp;
while(!ist.eof()){	
ist.ignore(256,'(');
hour=cin.get();
ist.ignore(256,',');
ist>>temperature;
temp.push_back(temperature);
ist.ignore(256,')');
}

for(int i=0; i<temp.size(); ++i){
	cout<<temp[i]<<endl;
	}

But I am not getting any output for temp[i] or any errors even when I included error statements to check.
Any ideas or a better method?
Last edited on
After a ton of more reading I finally figured it out in case anyone else has this question here is how:

My data was formatted like this
(0,50)
(1,55)

so my code is like this(with a lot left out):
1
2
3
4
5
6
7
8
int hour;
double temperature;
ifstream ist(filepath);
vector<double> temp;
char ch1,ch2,ch3
while(ist>>ch1>>hour>>ch2>>temperature>>ch3){
temp.push_back(temperature);
}
1
2
3
4
5
   double temperature;
   ifstream ist(filepath);
   vector<double> temp;
   while(ist.ignore(256, ',') && ist >> temperature)
       temp.push_back(temperature);


would also work.
Topic archived. No new replies allowed.