I have an assignment in Modular programming..
Have been able to make the program work... the lil problem am having now is that when i try to load the file i created, it keeps adding the memory location. Moe like it's adding an additional data insead of just loading the initial file.. can anyone help me with this?..
have attached a copy of my assignment to this message.. was trying to zip it but the school server was kinda acting funny..
thanks in Advance

PS: it's written in VC++ 6 so i don't know if it can be a problem for Vc++2008...

Attachments
#include <iostream>
#include "prompt.h"
#include <string>
using namespace std;

Get_int_from_prompt(string prompt, int &num)
{
	cout<<prompt<<"==>";
	cin>>num;
	return 0;
}

Get_string_from_prompt(string prompt, string &title)
{
	cout<<prompt<<"==>";
	cin>>title;
	return 0;
}

Get_double_from_prompt(string prompt, double &price)
{
	cout<<prompt<<"==>";
	cin>>price;
	return 0;
}

Get_copy_from_prompt(string prompt, int &copy)
{
	cout<<prompt<<"==>";
	cin>>copy;
	return 0;
}
#include <string>
using namespace std;

Get_int_from_prompt(string prompt, int &num);
Get_string_from_prompt(string prompt, string &title);
Get_double_from_prompt(string prompt, double &price);
Get_copy_from_prompt(string prompt, int &copy);
#include "vcdDB.h"
#include <iostream>
#include <fstream>
using namespace std;

// PRE: true
// POST: vcdDB initialised
void Init_vcdDB(vcdDB_T vcdDB)
{
     vcdDB.vcdIndex=0;
	 cout<<"Reinitializing the Pooter VCD library system"<<endl;
}

// PRE: true
// POST: IF vcdDB is empty
//          RETURNS TRUE
//      ELSE
//         RETURNS FALSE
bool Q_Empty_vcdDB(vcdDB_T vcdDB)
{
       return (vcdDB.vcdIndex==0);
}

// PRE: true
// POST: IF vcdDB is full
//          RETURNS TRUE
//      ELSE
//          RETURNS FALSE
bool Q_Full_vcdDB(vcdDB_T vcdDB)
{
      return (vcdDB.vcdIndex == maxSize);    
}

// PRE: true
// POST:  IF vcd with number Num in vcdDB
//             RETURNS TRUE
//       ELSE
//            RETURNS FALSE
bool Q_Present_vcdDB(int Num, vcdDB_T vcdDB, vcdRec_T CRec)
{
  int i=0;
  bool RESULT = false;
  while ((i <= vcdDB.vcdIndex) && (! RESULT))
  {
    if (Get_num(vcdDB.vcdRecords[i]) == Num)
    {
      RESULT = true;
      CRec = vcdDB.vcdRecords[i];
    }
    else
      i = i + 1;
    
  }
  return RESULT;
}

// PRE:  NOT Q_Full_vcdDB(VCD) AND 
//       NOT Q_Present_vcdDB(Get_num(CRec), vcdDB, _) 
// POST: CRec is added
void Add_vcd(vcdRec_T &vcd, vcdDB_T &vcdDB)
{
     vcdDB.vcdIndex = vcdDB.vcdIndex + 1;
     vcdDB.vcdRecords[vcdDB.vcdIndex] = vcd;     
}     

void Search_vcd(vcdRec_T &vcd, vcdDB_T &vcdDB)
{


}
// PRE: Q_Present(vcdno, vcdDB, _)
// POST:IF vcd with number Num deleted from vcdDB
//                 RETURNS TRUE
//     ELSE
//                RETURNS FALSE
void Delete_vcd(int num, vcdDB_T &vcdDB)
{
     int i=1;
     bool Q_Found=false;
 
     while (! Q_Found) 
     {
        if (Get_num(vcdDB.vcdRecords[i]) == num)
             Q_Found = true;
        else
             i = i + 1;
     }  
     
     while (i < vcdDB.vcdIndex) 
     {
         vcdDB.vcdRecords[i] = vcdDB.vcdRecords[i + 1];
         i = i + 1;
      }
      vcdDB.vcdIndex = vcdDB.vcdIndex + 1;
}     

// PRE:  true
// POST: All records printed to screen
void Print_All_vcdDB(vcdDB_T vcdDB)
{
     int i;

     for (i= 1; i<=vcdDB.vcdIndex; i++)
     {
         cout<<"VCD IN POSITION "<< i<<endl;
//         write_vcdRec(vcdDB.vcdRecords[i]);
         cout<<endl;
     }
}     
// PRE:  true
// POST: Save Records to a File
void Save_vcdDB_To_Text_file(vcdDB_T &vcdDB, ofstream &files)
{

	int i;
	for(i=1;i<=vcdDB.vcdIndex;i++)
	{

		Save_Record_To_File(files, vcdDB.vcdRecords[i]);
		cout<<"#";

	}
	cout<<endl;

}

void Load_vcdDB_from_Text_file(vcdDB_T &db, ifstream &files)
{

	vcdRec_T Next;

	while (!files.eof() && !Q_Full_vcdDB (db))
	{

		Load_Record_From_File(files,Next);
		Add_vcd(Next, db);
		cout<<"#";

	}
	files.close();

}
#include "vcdrec.h"
#include <fstream>
#define maxSize 20

struct vcdDB_T{
       vcdRec_T vcdRecords[maxSize];
       int vcdIndex;
};

// PRE: true
// POST: vcdDB initialised
void Init_vcdDB(vcdDB_T vcdDB);

// PRE: true
// POST: IF vcdDB is empty
//          RETURNS TRUE
//      ELSE
//         RETURNS FALSE
bool Q_Empty_vcdDB(vcdDB_T vcdDB);

// PRE: true
// POST: IF vcdDB is full
//          RETURNS TRUE
//      ELSE
//          RETURNS FALSE
bool Q_Full_vcdDB(vcdDB_T vcdDB);

// PRE: true
// POST:  IF vcd with number Num in vcdDB
//             RETURNS TRUE
//       ELSE
//            RETURNS FALSE
bool Q_Present_vcdDB(int num, vcdDB_T vcdDB, vcdRec_T CRec);

// PRE:  NOT Q_Full_vcdDB  AND 
//       NOT Q_Present_vcdDB(Get_vcdno(CRec), vcdDB, _) 
// POST: CRec is added
void Add_vcd(vcdRec_T &vcd, vcdDB_T &vcdDB);

// PRE: Q_Present(vcdno, vcdDB, _)
// POST:IF vcd with number Num deleted from vcdDB
//                 RETURNS TRUE
//     ELSE
//                RETURNS FALSE
void Delete_vcd(int num, vcdDB_T &vcdDB);

// PRE:  true
// POST: All records printed to screen
void Print_All_vcdDB(vcdDB_T vcdDB);

void Search_vcd(vcdRec_T &vcd, vcdDB_T &vcdDB);

void Save_vcdDB_To_Text_file(vcdDB_T &vcdDB, ofstream &files);

void Load_vcdDB_from_Text_file(vcdDB_T &db, ifstream &files);
#include "vcdDB.h"
#include "prompt.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void main_loop(vcdRec_T &vcd, vcdDB_T &db);
void Init_vcdDB(vcdDB_T vcdDB);


void User_write_vcdRec(vcdRec_T vcd)
{
	write_vcdRec(vcd);
}

void User_Search_vcd(vcdRec_T &vcd, vcdDB_T &vcdDB)
{
	int num, copy;
	string title;
	double price;
	Get_int_from_prompt ("vcd num", num);
	if (Q_Present_vcdDB (num, vcdDB, vcd))
	{
		Get_string_from_prompt ("vcd title", title);
			Get_double_from_prompt ("vcd price", price);
			Get_copy_from_prompt ("vcd copies", copy);
			Set_num(vcd, num);
			Set_title (vcd,title);
			Set_price(vcd, price);
			Set_copy(vcd, copy);
			Add_vcd(vcd, vcdDB);
	}
	else
	{
		cout<<"vcd number not exisiting"<<endl;
	}

}

void User_Add_vcd(vcdRec_T &vcd, vcdDB_T &vcdDB)
{
	int num, copy;
	string title;
	double price;

	if (!Q_Full_vcdDB (vcdDB))
	{
		Get_int_from_prompt ("vcd num", num);
		if (! Q_Present_vcdDB (num, vcdDB, vcd))
		{
			Get_string_from_prompt ("vcd title", title);
			Get_double_from_prompt ("vcd price", price);
			Get_copy_from_prompt ("vcd copies", copy);
			Set_num(vcd, num);
			Set_title (vcd,title);
			Set_price(vcd, price);
			Set_copy(vcd, copy);
			Add_vcd(vcd, vcdDB);
		}
		else
			cout<<"num already exist"<<endl;
		}
	else
     cout<<"vcd db is full"<<endl;
}

void User_Delete_vcd(vcdRec_T &vcd, vcdDB_T &vcdDB)
{
	int num;
	if (!Q_Empty_vcdDB(vcdDB))
	{
		Get_int_from_prompt("VCD number", num);
		if (Q_Present_vcdDB (num, vcdDB, vcd))
		{
			Delete_vcd(num, vcdDB);
		}
		else
			cout<<"ID is not exist."<<endl;
	}
	else
		cout<<"Product DB is empty."<<endl;
}
void User_Save_vcdDatabase_To_Text_file(vcdDB_T &vcdDB)
{

	ofstream files;

	if(!Q_Empty_vcdDB(vcdDB))
	{
		files.open("vcd.dat");
		Save_vcdDB_To_Text_file(vcdDB, files);
		files.close();

	}
	else
		cout<<"Empty database, There is nothing to save!";

}

void User_Load_vcdDatabase_From_Text_file(vcdDB_T &db)
{
	ifstream files;
	files.open("vcd.dat");
	Load_vcdDB_from_Text_file(db, files);
} 

int main()
{
	vcdRec_T vcd;
	vcdDB_T db;
	main_loop(vcd, db);
	return 0;

}

void main_loop(vcdRec_T &vcd, vcdDB_T &db)
{
	char Option;
	Option='m';
	while (Option !='x')
	{
		cout<<"==========vcduct Menu ==========================\n";
		cout<<"== (r)einitialize all database to empty       ==\n";
		cout<<"== (a)dd vcd to DB                       =======\n";
		cout<<"== (d)isplay vcd                         =======\n";
		cout<<"== Sear(c)h Vcd                          =======\n";
		cout<<"== d(e)lete vcd                          =======\n";
		cout<<"==(s)ave record                          =======\n";
		cout<<"==(l)oad record                          =======\n";
		cout<<"==(m)enu                                 =======\n";
		cout<<"==e(x)it                                 =======\n";
		cout<<"================================================\n";
		cout<<"Please Enter Option: "<<endl;
		cin>>Option;
		switch (Option)
		{
			case 'r': case 'R': Init_vcdDB(db); break;
			case 'a': case 'A': User_Add_vcd(vcd, db); break;
			case 'd': case 'D': User_write_vcdRec(vcd);break;
			case 'e': case 'E': User_Delete_vcd(vcd, db); break;
			case 'c': case 'C' : User_Search_vcd(vcd, db); break;
			case 's': case 'S': User_Save_vcdDatabase_To_Text_file(db); break;
			case 'l': case 'L': User_Load_vcdDatabase_From_Text_file(db); break;
		} 
	}
}
#include "vcdrec.h"
#include <string>
#include <iostream>
#include <fstream>
using namespace std;

void Set_num(vcdRec_T &vcd, int num)
{
	vcd.num=num;
}
void Set_title(vcdRec_T &vcd, string title)
{
	vcd.title=title;
}
void Set_price(vcdRec_T &vcd, double price)
{
	vcd.price=price;
}
void Set_copy(vcdRec_T &vcd, int copy)
{
	vcd.copy=copy;
}
// Get value

int Get_num(vcdRec_T vcd)
{
	return vcd.num;
}
string Get_title(vcdRec_T vcd)
{
	return vcd.title;
}
double Get_price(vcdRec_T vcd)
{
	return vcd.price;
}
int Get_copy(vcdRec_T vcd)
{
	return vcd.copy;
}

// write

void write_vcdRec(vcdRec_T vcd)
{
	cout<<"NUMER"<< vcd.num<<endl;
	cout<<"TITLE"<< vcd.title<<endl;
	cout<<"PRICE"<< vcd.price<<endl;
	cout<<"COPIES"<< vcd.copy<<endl;
}


void Load_Record_From_File(ifstream &files, vcdRec_T &vcd)
{	

	files>>vcd.num;
	files>>vcd.title;
	files>>vcd.price;
	files>>vcd.copy;
}

void Save_Record_To_File(ofstream &files, vcdRec_T vcd)
{

	files<<vcd.num;
	files<<vcd.title;
	files<<vcd.price;
	files<<vcd.copy;
}
#include <string>

using namespace std;

struct vcdRec_T{
	int num;
	string title;
	double price;
	int copy;
};

void Set_num(vcdRec_T &vcd, int num);
void Set_title(vcdRec_T &vcd, string title);
void Set_price(vcdRec_T &vcd, double price);
void Set_copy(vcdRec_T &vcd, int copy);

int Get_num(vcdRec_T vcd);
string Get_title(vcdRec_T vcd);
double Get_price(vcdRec_T vcd);
int Get_copy(vcdRec_T vcd);
int Get_vcd(vcdRec_T vcd);

void write_vcdRec(vcdRec_T vcd);
void Load_Record_From_File(ifstream &files, vcdRec_T &vcd);
void Save_Record_To_File(ofstream &files, vcdRec_T vcd);

1) prompt.h and prompt.cpp -- you need to specify function return values.

2) program is using uninitialized struct member in function Add_vcd(). You can fix that problem by adding a class constructor to the structure which initializes it.

struct vcdDB_T{
       vcdRec_T vcdRecords[maxSize];
       int vcdIndex;
public:
    vcdDB_T() {vcdIndex = 0;} 
};

There appear to be other problems too, such as displaying junk records.

Thanks.. But, When i run the program, my prompt is working fine.. my guess is that am missing something in the vcdDB.h and vcdDB.cpp.

Well you do use eof() in a control loop, which is bad
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046476070&id=1043284351

eof() is a state (the result of a past failure), not a prediction (of a future failure).

That is, if you have a 10 char file and read 10 chars, then eof() will NOT be true. It's only when you try to read (and fail) to read the 11th char that eof() will become true.

The corrollary of all this is that ALL your file read functions should check their return result to see that they succeeded.

This article has been dead for over six months. Start a new discussion instead.