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;
}

Recommended Answers

All 5 Replies

what do you mean by

desplay record is in row i want it in column.

display the output vertically?
you need to be more elaborate about this

Run the program sir and enter any file name with extension file of txt and choice number 1 add records then desplay the record by chosing number 3 that code is in parallel i want in vertical.

Run the program sir and enter any file name with extension file of txt and choice number 1 add records then desplay the record by chosing number 3 that code is in parallel i want in vertical.

No sir, you post what the program displays and what you want it to display. That's the price of asking for help -- giving us the information we need to understand.

Be sure to use the proper tags so we see exactly what you want us to see.

Ok sir for example i use filename: low.txt then they ask you to enter book no.

book no: 10
title: Hello
author: william
publisher: warmer bro

then it will say A Record has beed added

then choice number 3 to display the record, when display the record its shown like this:

Book no title author publisher
10 hello william warmer bro

what i want is just like this

book no: 10
title: hello
author: william
publisher: warmer bro


thanks

Problem solve guys for 3 hours of analyzing the code finally i got it. I just change the code from line 141 to 169 by this code:

void display_file()
{
	clear_data();
	file.clear();
	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(book_no[0]!='*')
	{

		cout<<setiosflags(ios::left);
		cout<<"Book_no - " <<book_no;
		file.getline(book_no,5,'|');
		cout<<"\nTitle - " <<title;
		file.getline(title,30,'|');
		cout<<"\nAuthor -" <<author;
		file.getline(author,20,'|');
		cout<<"\nPublisher - " <<publisher;
		file.getline(publisher,30,'|');
		cout<<endl;
		
		}
	}
	cout<<"\n\n";
}

thank you all guys for your time!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.