For loop import txt file data

I have a problem with importing some data from txt file.

Formatted file data in .txt file is:

( int int double ) e.g. ( 16 21 18.0 )

I have created a function to get data from file and it works but it just gives me the first line then it stops with return 0, with no errors. I guess I have a problem with the loop but I cannot understand which issue is.
Thank you in advance

Header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#pragma once

struct Reading
{
	int day, hour;
	double temperature;
	char ch1, ch2;
	Reading();
	Reading(char c1, int d, int h, double t, char c2);
};

istream& operator>>(istream& is, Reading& r);   // Setting helper input function
ostream& operator<<(ostream& os, const vector<Reading>& r);  // Setting helper output function
void vec_import(vector<Reading>& v);   // Read data from txt file 


Source file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "stdafx.h"
#include "std_lib_facilities.h"
#include "Header3.h"

void vec_import(vector<Reading>& v)
{
	string iname;
	cout << "Type input filename: ";
	cin >> iname;

	ifstream ist{ iname };
	if (!ist) error("Cannot read from filename ", iname);

	for (Reading r; ist >> r;)
	{
		v.push_back(r);
	}
}

Reading::Reading()
	:ch1{ ' ' }, day{ 0 }, hour{ 0 }, temperature{ 0 }, ch2{ ' ' } {};

Reading::Reading(char c1, int d, int h, double t, char c2)
	:ch1{ c1 }, day{ d }, hour{ h }, temperature{ t }, ch2{ c2 } {};

istream& operator>>(istream& is, Reading& r)
{
	int d, h;
	double t;
	char c1;
	char c2;

	is >> c1;
	switch (c1) 
	{
	case '(':
		break;
	default:
		break;
		return is;
	}

	is >> d >> h >> t;

	is >> c2;
	switch (c2)
	{
	case ')':
		break;
	default:
		break;
		return is;
	}

	r = Reading(c1, d, h, t, c2);

	return is;

}

ostream& operator<<(ostream& os, const vector<Reading>& r)
{
	for (int i = 0; i < r.size(); ++i)
	{
		return os << r[i].ch1 << " " << r[i].day << " " << r[i].hour
			<< " " << setprecision(1) << fixed << r[i].temperature
			<< " " << r[i].ch2 << endl;
	}
}


main cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Temperature Reading 2.0.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "std_lib_facilities.h"
#include "Header3.h"


int main()
{
	Reading r;
	vector<Reading> v;
	vec_import(v);
	cout << v;

	keep_window_open();

    return 0;
}
this return statement is never reached because you break the switch/case before the return.
1
2
3
4
5
6
7
8
	switch (c1) 
	{
	case '(':
		break;
	default:
		break;
		return is;
	}


why not just write:
if(c1 == '(') return is;

same thing with c2.

Tbh I don't know what you want to do, why not just write it like this:
1
2
3
4
5
6
7
istream& operator>>(istream& is, Reading& r)
{
    is.ignore(1); // ignore first bracket
    is >> r.d >> r.h >> r.t;
    is.ignore(256, ')'); // jump to point after last bracket
    return is;
}
Hi Gamer2015!

I wrote that way because I want to store it in my object. I followed Stroustrup's code in his book and I have already tried to use if statement. It did not work anyway! I solved because I had an issue in ostream& operator<<(ostream& os, const vector<Reading>& r) where I set not property the for loop! :)

Thank you anyway for answering me!
Last edited on
Topic archived. No new replies allowed.