Reading values from text files

Is is possible to retrieve a (double) value from text file for calculation??

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
void billing_system()
{
	int quantity;
	char ans;
	double item_total, total{};
	string product_id;
	int Loc = 0;
	bool found = false;

	int x = 0;

	do{
		cout << "Enter product ID: ";
		getline(cin, product_id);
		for (int i = 0; i < n; i++)
		{
			if (product_id == product[i].productID)
			{
				Loc = i;
				found = true;
				break;
			}
		}
		if (found = true)
		{
			cout << "\nEnter quantity: ";
			cin >> quantity;

			item_total = product[Loc].price_product*quantity;

		}
		cout << setprecision(2) << fixed;
		cout << "Item Price: RM" << item_total;
		total += total + item_total;
		cout << "\nThe current total amount is :RM" << total;
		cout << "\nAdd other product? (Y/N): ";
		cin >> ans;
	} while (ans == 'Y' && ans == 'y');

	cout << "Total Price: RM" << total;
}
This is my 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
void product_menu()
{
	int ans;
	cout << "\n1. Add Product";
	cout << "\n2. Product List";
	cout << "\n3. Back to Admin Menu";
	cin >> ans;
	getchar();
	switch (ans)
	{
	case 1: add_product(); break;
	case 2: product_list(); break;
	case 3: admin_menu(); break;
	}
}
/*******************************************************************************/
void add_product()
{

	int num, ans;
	ofstream grocery;

	productRec addProduct[n];

	cout << "How many products you want to add: ";
	cin >> num;
	grocery.open("d:\\PRODUCT.txt");

	for (int i = 0; i < num; i++)
	{
		cout << "Enter product name: ";
		cin >> addProduct[i].productNAME;
		cout << "Enter product ID: ";
		cin >> addProduct[i].productID;
		cout << "Enter product price: ";
		cin >> addProduct[i].price_product;
		grocery << addProduct[i].productNAME << "\t" << addProduct[i].productID << "\t" << addProduct[i].price_product << endl;
	}

	grocery.close();

	cout << "\n1. Product menu";
	cout << "\n2. Product list";
	cout << "\n3. Admin menu";
	cout << "Choose your option (1-3): ";
	cin >> ans;

	switch (ans)
	{
	case 1: product_menu();
	case 2: product_list();
	case 3: admin_menu();
	}
}
Topic archived. No new replies allowed.