Problems with fstream

Hi. My name is Jason. I am very inexperience with c++ programming but have decided to built myself an money tracking application which consists of storing data and read data from a text file. The data are as follows, the date, the product name and the price. Data flows are as follows: Input Date -> Input first item -> input first price ... -> input last item -> input last price. For different date, I can have different quantities of products purchased, meaning the data store is irregular. The price is a double type and is use to calculate total expenses and is unable to use the .getline() function. I have done my school assignment which consists of regular string data store and that was easy but this made me think. I hope that you wont found this silly because I really can't find anything relevant online. Feel free to give me criticism, advise and guidance. Thank you. Here is part of my attempt:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  void record() {
	DATE d1; product prod;
	ofstream ouf("data.txt", ios::app);
	
	cout << "Enter date  \n";
	cout << "Day: "; cin >> d1.day;
	cout << "Month: "; cin >> d1.month;
	cout << "Year: "; cin >> d1.year;
	cout << "Name of product: "; cin >> prod.name;
	cout << "Cost: "; cin >> prod.price;
	ouf << d1.day << '-' << d1.month << '-' << d1.year << ',' << prod.name << '*' << prod.price << endl;

}

void read() {
	DATE d1; product prod;
	ifstream inf("data.txt");
	while (!inf.eof()) {
		inf >> d1.day, '-'; cin.getline(d1.month, 25, '-'); cin >> d1.year, ',';
		inf.getline(prod.name, 50, '*'); inf >> prod.price;
		cout << d1.day + "~" << d1.month << "~" + d1.year << "\t" << prod.name << "  " << prod.price << endl;
	}
}
Can we see your whole code? it's hard to get anything without knowing for instance your DATE and product struct/class?
The price is a double type and is use to calculate total expenses and is unable to use the .getline() function.


getline only works with strings. You can either use getline to read the input into a string and then use something like std::ostringstream or std::stod to convert it to a double, or you can use the >> operator which seems like you're already doing.
It's always good to post the whole code - unless it's very long.
I tried to complete it based on your input and came up with this - correct me if I am wrong.
Changed your read code as well. Try and see if it works.
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct DATE
{
  int day, month, year;
};

struct product
{
  string name;
  double price;
};

void record() 
{
  DATE d1; product prod;
  ofstream ouf("data.txt", ios::app);

  cout << "Enter date  \n";
  cout << "Day: "; cin >> d1.day;
  cout << "Month: "; cin >> d1.month;
  cout << "Year: "; cin >> d1.year;
  cout << "Name of product: "; cin >> prod.name;
  cout << "Cost: "; cin >> prod.price;
  ouf << d1.day << '-' << d1.month << '-' << d1.year << ',' << prod.name << '*' << prod.price << endl;

}

void read() 
{
  DATE d1; 
  product prod;
  char dummy;
  ifstream inf("data.txt");
  while (inf >> d1.day >> dummy >> d1.month >> dummy >> d1.year >> dummy)
  {
    getline(inf, prod.name, '*');
    inf >> prod.price;
  }

}
int main()
{
  record();
  read();
}

Output file:
22-8-2018,Hammer*9.99

Some remarks:
It would be better if you were consistent with your naming conventions. Why DATE and product?
Better would be Date and Product or date and product.
By convention all uppercase is used for constants.
Never ever use eof for streams. They work not as people expect - Google will tell you the details.
Last edited on
Sorry for the mess guys, this is my first time using this forum.
Thanks Peter87 and Thomas1965 for your help, I will bear in mind with everything I learnt here especially the remarks. The case is closed. Thank you guys for your help
Topic archived. No new replies allowed.