hmm however now my find_free function seems to have stopped working properly, as the input function is having i returned as -1 all the time and saying all records are full :S odd
seems to be ok?
Any ideas why the load function would do that?
Code:
#include "stdafx.h"
#include "iostream.h"
#include "fstream.h"
#include "stdlib.h" // includes standard libary header for clear screen function
#include "string.h"
struct record { /*struct called record*/
char name[30];
char street[50];
char streetii[50];
char postcode[10];
};
record entry[100]; /* delares variable entry with max 100 records*/
//entry(input);
void main(),menu(),options(int choice);
void newrecord(),deleterecord(),findrecord(),displayall(),exit();
void dofirst(), load(), input(),save(int i);
int find_free();
void main ()
{
dofirst();
menu();
}
void menu()
{
int choice; //declares the choice variable,
do
{
cout << "Which option would you like? (1-5) \n\n";
cout << "1: New Entry In Database\n";
cout << "2: Delete Entry In Database\n";
cout << "3: Find and Display A Single Record\n";
cout << "4: Display All Records\n";
cout << "5: Exit Database\n";
cout << "\nPlease Enter Your Choice (1-5): ";//Display some options on screen.
cin >> choice;
options(choice); //opens options function + sends choice to options function.
}while(choice!=5);//The program will continue to run until option 5 (exit) is chosen.
}//end function
void options(int choice)
{
switch (choice)//Goes Through options
{
case 1: //If number 1 is picked, it will go to the function below same each case.
newrecord();
break;
case 2:
deleterecord();
break;
case 3:
findrecord();
break;
case 4:
displayall();
break;
case 5:
exit();
break;
default://THEY DIDNT READ THE MENU, Make Them Choose Correct choice
system("cls"); // Clear Screen
cout << "\a\a\a\nYOU HAVE ENTERED AN INVALID CHOICE YOU FOOL!\n"
<< "Please enter a number from 1 to 5\n\n";
// shown if an incorrect choice is shown.
break;
}
}//end function
//functions
void dofirst() // this function prepares the data.dat file for first use
{
int i; // variable i declared
for ( i=0; i<100; i++ ) // loop until i=99
{
cout << "Preparing Record Line " << i << "\n" << endl; // shows progress of file prep
system("cls"); // clear screen
strcpy(entry[i].name, "*"); // make entry[i].name in struct/array = *
}
load();
} //end function
void newrecord()
{
input(); // calls the input function
system("cls"); // clear screen
}//end function
void input()
{
int i;
i=find_free();
if (i==-1) // if find_free fuction returns -1 do below
{
system("cls");
cout << "THE DATABASE IS FULL PLEASE DELETE SOME RECORDS\n\n";
}
else
{
system("cls"); //clear screen
cout <<"Enter Name:" << endl;
gets((entry[i].name));
cout <<"Enter House number and street address: " << endl;
gets((entry[i].street));
cout <<"Enter Town/City: " << endl;
gets((entry[i].streetii));
cout <<"Enter Post Code:" << endl;
gets((entry[i].postcode));
save(i); //calls save
}
}//end function
int find_free()
{
int i;
for ( i=0; i<100; i++) //loops until i is = 99
{
if (!strcmp ((entry[i].name), "*")) //if String * is the same as, entry.name of number i return true
return i; // if strcmp statement is true return i to the function that called findfree()
}
return -1; //if the above for statement finds nothing then return -1 to the calling funtion.
}//end function
void exit()
{
system("cls");
cout << "\nThankyou For Using Jays' Database. Goodbye :)\n\n";
}//end function
void load()
{
int i;
ifstream fin("data.dat");
for (i=0; i<100; i++)
{
fin.getline((entry[i].name), 30, '\xba');
fin.getline((entry[i].street), 50, '\xba');
fin.getline((entry[i].streetii), 50, '\xba');
fin.getline((entry[i].postcode), 10, '\xba');
}
}//end function
void save(int i)
{
ofstream fout("data.dat", ios::app); // opens file data.dat for output to the program in append mode.
fout << (entry[i].name) << "\xba"; // puts name variable of number [i] into a file then starts new line
fout << (entry[i].street) << "\xba";
fout << (entry[i].streetii) << "\xba";
fout << (entry[i].postcode) << "\xba\n";
fout.close();
}//end function
void findrecord()
{
// int i;
system("cls");
}//end function
void displayall()
{
system("cls");
cout << "free function\n\n";
}//end function
void deleterecord()
{
system("cls");
cout << "free function 2\n\n";
}//end function