Hello C++ master out ther i need help guys this exercise is running but the desplay record is in row i want it in column. Thanks in advance!
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
using namespace std;
char book_no[5];
char title[30];
char author[20];
char publisher[30];
char filename[256];
fstream file;
void clear_data()
{
book_no[0]=0;
title[0]=0;
author[0]=0;
publisher[0]=0;
}
void prepare_file(char * filename)
{
ofstream myfile;
myfile.open(filename,ios::app);
myfile.close();
}
int menu()
{
int choice;
cout<<"\n\n";
cout<<"1 - Add record\n";
cout<<"2 - Search record\n";
cout<<"3 - Desplay all records\n";
cout<<"4 - Delete records\n";
cout<<"0 - Exit\n";
cout<<endl;
cout<<"Enter choice:";
cin>>choice;
if(choice<0||choice>4)
{
choice=-1;
}
cin.ignore(1);
return choice;
}
void add_record()
{
clear_data();
file.clear();
cout<<"\nEnter Book_No:";
cin.getline(book_no,5);
cout<<"\nTitle:";
cin.getline(title,30);
cout<<"\nEnter author:";
cin.getline(author,20);
cout<<"\nEnter publisher:";
cin.getline(publisher,30);
file.seekp(0,ios::end);
file.seekg(0,ios::end);
if(!file)
{
cout<<"Can't add record!\n";
}
else
{
file<<book_no<<'|'<<title<<'|'<<author<<'|'<<publisher<<'|';
if(file.fail())
{
cout<<"Unable to add record!\n";
}
else
{
cout<<"\nA record has been added!\n";
}
}
}
void search_record()
{
char ch_search_key[16];
string str_search_key;
string str_book_no;
clear_data();
file.clear();
cout<<"\nEnter the Book_no:";
cin.getline(ch_search_key,16);
str_search_key=ch_search_key;
file.seekp(0,ios::beg);
file.seekg(0,ios::beg);
while(!file.eof())
{
file.getline(book_no,5,'|');
file.getline(title,30,'|');
file.getline(author,20,'|');
file.getline(publisher,30,'|');
if(file.fail())
{
cout<<"encountered an error while searching the record!\n";
break;
}
str_book_no=book_no;
if(str_book_no==str_search_key)
{
cout<<"\n\n";
cout<<"We founf the record\n";
cout<<setiosflags(ios::left);
cout<<setw(5)<<"Book_No";
cout<<setw(30)<<"Tiltle";
cout<<setw(20)<<"Author";
cout<<setw(30)<<"Publisher";
cout<<endl;
cout<<setw(5)<<book_no;
cout<<setw(30)<<title;
cout<<setw(20)<<author;
cout<<setw(30)<<publisher;
break;
}
}
}
void display_file()
{
clear_data();
file.clear();
file.seekp(0,ios::beg);
file.seekg(0,ios::beg);
cout<<"\n\n";
cout<<setiosflags(ios::left);
cout<<setw(5)<<"Book_No";
cout<<setw(30)<<"Tiltle";
cout<<setw(20)<<"Author";
cout<<setw(30)<<"Publisher";
cout<<endl;
while(!file.eof())
{
cout<<"\n\n";
file.getline(book_no,5,'|');
cout<<endl;
file.getline(title,30,'|');
cout<<"\n\n";
file.getline(author,20,'|');
cout<<"\n\n";
file.getline(publisher,30,'|');
cout<<"\n\n";
if(book_no[0]!='*')
{
cout<<setw(5)<<book_no;
cout<<setw(30)<<title;
cout<<setw(20)<<author;
cout<<setw(30)<<publisher;
cout<<endl;
}
}
}
void delete_record()
{
char ch_delete_key[16];
string str_delete_key;
string str_book_no;
int str;
int put;
clear_data();
file.clear();
cout<<"\nEnetr the book number:";
cin.getline(ch_delete_key,16);
str_delete_key=ch_delete_key;
file.seekp(0,ios::beg);
file.seekg(0,ios::beg);
while(!file.eof())
{
file.getline(book_no,5,'|');
file.getline(title,30,'|');
file.getline(author,20,'|');
file.getline(publisher,30,'|');
str_book_no=book_no;
if(!file.fail())
{
cout<<"encountered an error while searching the record!\n";
break;
}
if(str_book_no==str_delete_key)
{
str=strlen(book_no)+strlen(title)+strlen(author)+strlen(publisher)+4;
file.seekg(-str,ios::cur);
put=file.tellg();
file.seekp(put,ios::beg);
file<<'*';
cout<<book_no<<"has been deleted!\n";
break;
}
}
}
int main()
{
int choice;
cout<<"Enter the database filename:";
cin.getline(filename,256);
prepare_file(filename);
file.open(filename);
clear_data();
if(file.is_open())
{
while (1)
{
choice=menu();
switch(choice)
{
case 1:
add_record();
break;
case 2:
search_record();
break;
case 3:
display_file();
break;
case 4:
delete_record();
break;
case 0:
exit(0);
break;
default:
cout<<"Invalid!\n";
break;
}
}
}
else
{
cout<<"Error while opening the file\n\n";
}
return 0;
}