I am trying to create a simple database. Here is the code:
#include <iostream>
#include <fstream>
#include "conio.h"
#include <string>
using namespace std;
struct Station{
string url;
string name;
string genre;
}temp;
char userInput;
string output;
int fileSize;
string searchParameter;
int index;
int results[1000];
int j;//temporary storage for a counter
void addStation(){ //problem here
fstream hfile;
hfile.open("database.txt", ios::binary|ios::in|ios::out);
hfile.seekp(0, ios::end);
cout<<"Enter URL:"<<endl;
cin>>temp.url;
cout<<"Enter Station Name"<<endl;
cin>>temp.name;
cout<<"Enter Genre"<<endl;
cin>>temp.genre;
hfile.write((char*)&temp, sizeof(temp));
hfile.close();
};
void viewDataBase(){ //problem here
fstream hfile;
hfile.open("database.txt", ios::binary|ios::in|ios::out);
hfile.seekg(0, ios::end);
fileSize = hfile.tellg();
hfile.seekg(0, ios::beg);
index = 0;
if (fileSize > 0){
do{
cout<<"---------"<<endl;
cout<<index<<endl;
hfile.read((char*)&temp, sizeof(Station));
cout<<temp.url<<endl;
cout<<temp.name<<endl;
cout<<temp.genre<<endl;
index++;
}while(fileSize != hfile.tellg());
}else{
cout<<"The database is empty... press any key to continue"<<endl;
}
hfile.close();
getch();
};
void wipeDataBase(){
fstream hfile;
cout<<"are you sure? Y/N:"<<endl;
cin>>userInput;
if (userInput == 'y' ||userInput == 'Y'){
hfile.open("database.txt", ios::binary|ios::in|ios::out|ios::trunc);
hfile.close();
}
}
void searchDataBase(char userDecision, string searchParameter, int results[1000]){
fstream hfile;
hfile.open("database.txt", ios::in|ios::out|ios::binary);
hfile.seekg(0, ios::end);
fileSize = hfile.tellg();
hfile.seekg(0, ios::beg);
index = 0;
for(int x = 0; x<1001; x++){
results[x] = -1;
}
if (fileSize > 0){
do{
hfile.read((char*)&temp, sizeof(Station));
if (userInput == '1'){
if (temp.url == searchParameter){
for(int x = 0;x < 1001; x++){
if (results[x] = -1){
results[x] = index;
break;
}
}
}
}
if (userInput == '2'){
if (searchParameter == temp.name){
for(int x = 0;x < 1001; x++){
if (results[x] = -1){
results[x] = index;
break;
}
}
}
}
if (userInput == '3'){
if (searchParameter == temp.genre){
for(int x = 0;x < 1001; x++){
if (results[x] = -1){
results[x] = index;
break;
}
}
}
}
index++;
}while(fileSize != hfile.tellg());
}else{
cout<<"the database is empty ... "<<endl;
}
hfile.close();
}
void displayData(int results[1000]){
fstream hfile;
hfile.open("database.txt", ios::binary|ios::in|ios::out);
j = 0;
do{
hfile.seekg(results[j]*sizeof(Station), ios::beg);
hfile.read((char*)&temp, sizeof(Station));
cout<<"---------"<<endl;
cout<<index<<endl;
cout<<temp.url<<endl;
cout<<temp.name<<endl;
cout<<temp.genre<<endl;
j++;
}while(j != 1000 && results[j] != -1);
}
int main(){
do{
system("cls");
cout<<"Welcome to DataBass"<<endl
<<"Please Choose one of the following options"<<endl
<<"1 - add DataBase"<<endl
<<"2 - view dataBase"<<endl
<<"3 - quit . . ."<<endl
<<"4 - wipe dataBase"<<endl
<<"5 - search dataBase"<<endl;
do{
cin>>userInput;
}while(userInput != '1' && userInput != '2' && userInput != '3' && userInput != '4' && userInput != '5');
if (userInput == '1'){
addStation();
}
if (userInput == '2'){
viewDataBase();
}
if (userInput == '4'){
wipeDataBase();
}
if (userInput == '5'){
cout<<"Search by:"<<endl
<<"1 - url"<<endl
<<"2 - name"<<endl
<<"3 - genre"<<endl;
do{
cin>>userInput;
}while(userInput != '1' && userInput != '2' && userInput != '3');
cin>>searchParameter;
searchDataBase(userInput, searchParameter, results);
displayData(results);
getch();
}
}while(userInput != '3');
return 0;
}
i apologise for the lack of comments. The two functions im concentrating on just now are marked as "problem here". Basically my problem is that if you enter a certain amount of data using the addStation() function then the viewDatabase() function crashes the program, or alternately prints random symbols in place of the data that was entered. I have tried using the getline(cin, struct.string) comand aswell as cin>>struct.string but both give the same result.
the other thing i am wanting to do is tie it all into a windows form. If anyone could help me with that, it would be much apreciated. A link to a site explaining them well would be best. I have tried a few tutorials but i can't work out how to get from creating a windows form with certain functions like inputs and outputs, to tieing it with the functions i have created.
Any help would be much apreciated.
Currently i am using bloodshed to work on this, however i do have a copy of visual studio professional 2005 and 2008.