writing multiple records to a file and displaying them all

My program compiles but doesn't actually work. It seems to only display the last record entered where I want it to display all the records that are entered. I know I am close but I just can't see where the issue is! Any Help is appreciated. Thank you in advance.

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
Write two programs which will work with a structure.

a) The first program will use a structure to store the following inventory data in a file:

Item Description
Quantity on Hand
Wholesale Cost
Retail Cost
Date Added to Inventory
The program will write five records to an output file for different items; the quantity, costs, and date for each item will be different from the other items.

b) The second program will read the file created by the first program using the same structure. The second program will calculate and display the following information:

total wholesale value of the inventory
total retail value of the inventory
total quantity of all items in the inventory*/

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

const int DES = 20;

//declare variables
void Add();
void Display();
void Edit();

struct Inventory
{
char item[DES];
int qty;
int wcost;
int rcost;
char date[10];
};

//Main function
int main()
{
int choice;
do
{
cout << "MENU" << endl;
cout << "1. Add Record: " << endl;
cout << "2. Display Records: " << endl;
cout << "Please enter your selection." << endl;
cin >> choice;

switch (choice)
{
case 1: 
Add(); 
break;	//Add record
case 2: 
Display(); 
break;	//Display record

default: cout << "Invalid Selection" << endl;
}
} while 
(choice <= 2);

system("PAUSE");
return 0;
}


//Add funtion
void Add()
{
fstream fout;
const int size = 3;
char ch;
int i = 0;
fout.open("Records.txt", ios::out);
Inventory inv;

//get data
do
{
cout << "Enter item description: " << endl;
cin.ignore();
cin.getline(inv.item, DES);
cout << "Enter quantity: " << endl;
cin >> inv.qty;
cout << "Enter wholesale cost: " << endl;
cin >> inv.wcost;
cout << "Enter retail cost: " << endl;
cin >> inv.rcost;
cout << "Enter date: " << endl;
cin.ignore();
cin.getline(inv.date, 10);

//write record to file
fout.write(reinterpret_cast<char*>(&inv), sizeof(inv));
cout << "Do you want to add another record? " << endl;
cin >> ch;
} while
(ch == 'Y' && 1 < 3);

//close the file
fout.close();
}

//"Display" function 
void Display()
{
fstream fout;
fout.open("Records.txt", ios::in);
Inventory inv;
fout.read(reinterpret_cast <char*> (&inv), sizeof(inv));
while (!fout.eof())
{

cout << "\nDescription\t: ";
cout << inv.item;
cout << "\nQuantity\t: ";
cout << inv.qty;
cout << "\nWholesale Cost\t: ";
cout << inv.wcost;
cout << "\nRetail Cost\t: ";
cout << inv.rcost;
cout << "\nDate\t: ";
cout << inv.date;
fout.read(reinterpret_cast <char*> (&inv), sizeof(inv));
}
//close the file
fout.close();
}
Last edited on
Well, it partially works for me:
MENU
1. Add Record:
2. Display Records:
Please enter your selection.
1
Enter item description:
asd
Enter quantity:
4
Enter wholesale cost:
55
Enter retail cost:
56
Enter date:
12/10/10
Do you want to add another record?
Y
Enter item description:
www
Enter quantity:
5
Enter wholesale cost:
1
Enter retail cost:
55
Enter date:
34/56/78
Do you want to add another record?
N
MENU
1. Add Record:
2. Display Records:
Please enter your selection.
2

Description     : asd
Quantity        : 4
Wholesale Cost  : 55
Retail Cost     : 56
Date    : 12/10/10
Description     : www
Quantity        : 5
Wholesale Cost  : 1
Retail Cost     : 55
Date    : 34/56/78MENU
1. Add Record:
2. Display Records:
Please enter your selection.


Your problem is with line fout.open("Records.txt", ios::out);
It will set write pointer to the beginning of the file. Meaning that you will start writing from the beginning, overwriting previous information.

Use either ios::cout | ios::ate or ios::cout | ios::app (read what they do to select one) or manually seek to the end of the file
MiiNiPaa! Thank you! I can't believe it was that simple. I kept trying to rewrite a whole portion of my code and was getting no where! Thank you kindly
Topic archived. No new replies allowed.