i have this problem to write and read data, here's what im trying to make :
Write a program that uses 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 should have a menu that allows the user to perform the following tasks:
-Add new records to the file.
-Display any record in the file.
-Change any record in the file.
and this is the code i made =
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
struct Info
{
string desc;
int quantity;
double saleCost;
double retCost;
string added;
};
void getData(Info&);
void displayData(Info);
void changeData(Info&, fstream&);
int main()
{
Info data;
fstream file("asd.txt", ios::in | ios::out | ios::app | ios::binary);
char ch;
long recNum;
long byte;
do
{
cout<<"do you want to : \n";
cout<<"1. add new record\n";
cout<<"2. display record\n";
cout<<"3. change record\n";
cout<<"pick 1, 2, or 3\n";
cin.get(ch);
switch(ch)
{
case '1' : getData(data);
file.write(reinterpret_cast<char*>(&data), sizeof(data));
file.close();
break;
case '2' : cout<<"which record u want to see = ";
cin>>recNum;
recNum--;
file.seekg(recNum * sizeof(data), ios::beg);
file.read(reinterpret_cast<char*>(&data), sizeof(data));
displayData(data);
break;
case '3' : cout<<"which record u want to change = ";
cin>>recNum;
recNum -=1;
file.seekp(recNum * sizeof(data), ios::beg);
changeData(data, file);
break;
}
cout<<"\ndo u want to do anything else (Y/N)? ";
cin.get(ch);
}while(toupper(ch)!= 'N');
}
void getData(Info &asd)
{
cin.ignore();
cout<<"enter description = ";
getline(cin, asd.desc);
cout<<"\nenter quantity = ";
cin>>asd.quantity;
cout<<"enter wholesale cost = ";
cin>>asd.saleCost;
cout<<"enter retail cost = ";
cin>>asd.retCost;
asd.added = "u had added " + asd.desc;
cout<<endl;
}
void displayData(Info d)
{
cout<<"item desciption = "<<d.desc<<endl;
cout<<"item quantity = "<<d.quantity<<endl;
cout<<"item wholesale cost = "<<d.saleCost<<endl;
cout<<"item retail cost = "<<d.retCost<<endl;
cout<<"last thing u did on this record = "<<d.added<<endl;
d.added = "u had displayed "+ d.desc;
}
void changeData(Info &DATA, fstream &FILE)
{
cin.ignore();
cout<<"enter description = ";
getline(cin, DATA.desc);
cout<<"\nenter quantity = ";
cin>>DATA.quantity;
cout<<"enter wholesale cost = ";
cin>>DATA.saleCost;
cout<<"enter retail cost = ";
cin>>DATA.retCost;
DATA.added = "u had edited " + DATA.desc;
cout<<endl;
FILE.write(reinterpret_cast<char*>(&DATA), sizeof(DATA));
}
erm the problems with it are :
A. if i add record example
item description = A
quantity = 5
whole sale cost = 20.5
retail cost = 10.5
then when i pick to add new record again, example
item description = B
quantity = 10
wholesale cost = 30.5
retail cost = 20.0
after that i try to view the record, but if i pick record 1, it just display the :
item description = B
quantity = 10
wholesale cost = 30.5
retail cost = 20.0
so its like the item"B" replace the item "A" which means i cant see the item's A record
B. when i let the program just to add record, then i close the program and then open the program again just to view the record, it shows unknown data like some random number and strings etc in the screen.
anyone can help?
note : sorry ofr the bad english