sstream problem



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
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

void display(string &rec)
{
	int code;
	string name;
	float price;
	char del;
	// the istringstream object receives the string
	// and separates it to its fields(code, label, price)
	istringstream iss(rec);
	iss >> code;
        iss >> del;
	iss >> name;
        iss >> del;
	iss >> price;
	// display the fields of the record
	cout << "**************************\n";
	cout << "code: " << code << endl;
	cout << "label: " << name << endl;
	cout << "price: " << price << endl;
	cout << "**************************\n";

	return ;
}


int main()
{
	bool more = 1;
	ifstream fin;
	ofstream fout;
	string record;
	int code; string name; float price; // the field of the record

	fout.open("data.txt", ios_base::app);

	while(more)
	{
		// the user enters the fields of the record
		// code, label, price
		cout << "Enter code: ";
		cin >> code;
		cin.ignore();
		cout << "Enter label: ";
		getline(cin, name);
		cout << "Enter price: ";
		cin >> price;
		// takes the fields and unites them in
		// a string that should have the structure:
		//        field1,field2,field3
		ostringstream oss(ostringstream::trunc);
		oss << code << "|" << name << "|" << price << endl;
		record = oss.str();
		// the record as as string is now written in the file 
		fout << record;
		cout << "Enter another?";
		cin >> more;
	}
	
	fout.close();
	fin.open("data.txt");

	while (!fin.eof())
	{
		// read the record from the file
		getline(fin, record);
		// display them
		display(record);
	}

	fin.close();

	return 0;
}


I have a problem in the display function!
The istringstream obj is supposed to read each
field of the record and store them in their
respective variables!
But this only works for the code field.
When it reads the name field it reads the price field too!
For example a here is a sample run:

Enter code: 147
Enter label: ruffles
Enter price: 1.75
Enter another?0
**************************
code: 147
label: ruffles|1.75
price: -1.07374e+008
**************************


Why this is happening and how can i fix it?
Any help would be appriciated!

Last edited on
On line 56...

 
oss << code << "|" << name << "|" << price << endl;


...you are separating your data using a '|' character. Well reading in a std::string will read in the '|' character as part of the string.

What you could do is use std::getline() to read the std::string like this in line 17:

 
std::getline(iss, name, '|'); // read everything up to the '|' 


http://www.cplusplus.com/reference/string/getline/

Also note that std::getline() removes the deliminator '|' so you don't need to remove it yourself with iss >> del;

Last edited on
Also, just a note about your reading loop. The eof flag is not always set until after the attempted read fails.

So doing the loop this way...

1
2
3
4
5
6
7
	while (!fin.eof())
	{
		// read the record from the file
		getline(fin, record);
		// display them
		display(record);
	}


...will not always detect the failure due to eof() until it has already tried to convert one blank record.

A way to avoid this is doing the loop this way:

1
2
3
4
5
	while (getline(fin, record)) // read the record from the file		
	{
		// display them ONLY IF the getline() succeeded
		display(record);
	}

Thanks a lot Galik.Problem solved!
Last edited on
Topic archived. No new replies allowed.