0

Hello! I need help with this program.. Me and my friend are working on a simple inventory system. The program works well, but we don't know how to retrieve the and delete the data that we inputed.

Thanks for helping!

Here's our code:

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<string.h>
#include<dos.h>
#include<stdlib.h>
void display()
{
clrscr();
cout<<"press:  " ;

cout<<"\n 1:ADD RECORDS \n 2:RETRIEVE RECORDS \n 3:READ RECORDS \n 4:quit";
}

void main()
{
clrscr();
display();
char choice;
cout<<"\nEnter your Choice:";
cin>>choice;
switch (choice)
{
case '1':addrecords();break;
case '2':retrieve();break;
case '3':readrecords();break;
case '4':break;
default: cout<<"Only 1-4:";
}
getch();
}


void addrecords()
{
clrscr();
ofstream outfile;
outfile.open("data.dat");
if (!outfile)
{
   cout<<"Error Accessing";
   return ;
}
char letter[26]={"abcdefghijklmnopqrstuvwxyz"};
char number[10]={"0123456789"};
struct
{
char itemname[25];
char stocknumber[6];
int price;
int deliver;
int current;
}system;

int l=0,total=0,totalprice=0;
stocks:
cout<<"Enter stocknumber:";
cin>>system.stocknumber;

for (int i=0;i<system.stocknumber[i];i++)
{
   if (i>=3&&i<6)
   { for (int k=1;k<=10;k++)
    {if (system.stocknumber[i]==number[k])
   { l++;
     }}}

  if (i<3)
  {for (int j=0;j<26;j++)
  {if (system.stocknumber[i]==letter[j])
    {l++;
    }}}
}

if (l!=6)
{
  cout<<"Invalid stocknumber\n";
  l=0;
  delay(500);
  goto stocks;

}
cout<<"Enter itemname:";cin>>system.itemname;
cout<<"Enter current stock:";cin>>system.current;
deliver:
cout<<"Enter delivered stock:";cin>>system.deliver;
 if (system.current>system.deliver)
 {
 total=system.current-system.deliver;
 }
 else
 {
 cout<<"OUT OF STOCKS\n";
 delay(500);
 goto deliver;

 }
 prices:
 cout<<"\nPrice:";cin>>system.price;
 if (system.price>0)
 {
 totalprice=total*system.price;
 }
 else
 {cout<<"Invalid Price\n";
 delay(500);
 goto prices;
 }

 outfile.write((char *)&system,sizeof(system));
 outfile.close();
}
void readrecords()
{
clrscr();
 ifstream infile;
 infile.open("data.dat");
 infile.seekg(0);
 while (!infile.eof())
 {
 infile.read((char*)&system,sizeof(system));
 cout<<"\nstocknumber:"<<system.stocknumber;
 cout<<"\nitemname:"<<system.itemname;
 cout<<"\ncurrent stock:"<<system.current;
 cout<<"\ndelivered stock:"<<system.deliver;
 cout<<"\nprice:"<<system.price;
 cout<<"\nTotal:"<<total;
 cout<<"\ntotal price:"<<totalprice;
 }
 infile.close();
 return;
}

void retrieve()
{
clrscr();
ifstream infile("data.txt");
if (!infile)
{
cout<<"eRROR accessing ";
return 0;
}

cout<<"\nEnter stocknumber to search:";
cin.get(stocknumber,6);

while(1)
{
infile.read(system.stocknumber,6);
if (infile.gcount()==0)
return 0;

if (strcmp(system.stocknumber,compare)==0)
{
infile.read(system.itemname,25)
readrecords();
}
infile.seekg(53,(seek_dir));
}
return;
}

Edited by Nick Evan: Added code-tags

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Ancient Dragon
0

Hello

I would suggest to use a standard sql database instead of programming all those insert/update/delete functionalities by yourselves. There was a similar question recently. You may read here what I wrote.

To keep it small I suggest to embed a database system into your C++ program. The most embedded database is SQLite3, for example in Mozilla Firefox, Open Office, Skype etc. It is probably the most frequently applied database system worldwide. SQLite3 is easy to handle and very simple to integrate in C++ programs yet it is an almost complete rational database system based on SQL1999/2003.

-- tesu

Edited by tesuji: n/a

0

Thanks for answering.. but I forgot to tell that its an exercise for our data structures subject. Of course when doing an inventory system.. we need to update/input/delete/retrieve the data inputted in the program.

0

First you will have to move the declaration of that structure that's in addrecords() up to the top of that *.cpp file so that it can be used everywhere in the program, not just in addrecords()

Have you tried to compile all that code? I'll bet you haven't because I don't see where the variable system that's used in function retrieve() has been defined anywhere.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.