C++ problem book_store

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 4626 Views - Last Post: 22 May 2012 - 02:04 AM Rate Topic: -----

#1 poolet  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 14-May 12

C++ problem book_store

Posted 15 May 2012 - 09:29 AM

Hello guys, Well I'm desperate... I am trying to build a project... and I really need a hit point to continue since
honestly I have stuck and I don't know where to start...There are 9 functions of this program but I will post only the first 2 since
are the most confused.. So.. The first function called Read_a_customer() the informations of customer : id, name, address
should be added array in the array in the correct place according to alphabetic order of customer name.. also it will check if the customer
is already added... my serious problem is that I can't save an array since the customer_Details must be a structure and instead of it will be added the id,name and address.. This is my first problem, the second one came is on the second function that called print_details_of_customer() I have finally create that, but for some reason if i will added 2 customers in a row, it's replaced the older record by the new one... Now, it's seems logical since may array isn't change position and just overwrite the previous data but I have check it again and again and the position is change, so I think something is going wrong with the structures and the correct declarations please check my code below because am serious desperate... Also check the point with the alphabetic order end let me know if am correct or not since I am not able to check it without complete the first part...!!

::NOTE:: Please ignore all the unuseless variable...
Thank you in advance!! Regards!!!



#include<iostream>
#include<cmath>
#include<string>
#include <stdio.h>       //libraries for strcmp on unix/linux
#include <string.h>     //libraries for strcmp on unix/linux

using namespace std;



int Read_a_customer();
int Print_details_of_a_customer();
int Return_the_number_of_the_customers();


struct CustomerDetails
{
	char name[40];
	char address[40];
	char ID[40];

	char title[40];
	char author[40];
	char ISBN[40];     //BookDetails STRUCT Variables
	char status[40];
	char date[40];
}date;

struct BookDetails
{
	char title[40];
	char author[40];
	char ISBN[40];     //BookDetails STRUCT Variables
	char status[40];
	char date[40];

	CustomerDetails T;
	CustomerDetails F;
};

BookDetails failure_c;
BookDetails c;
BookDetails Array_c[10];

BookDetails failure_d;
BookDetails d;
BookDetails Array_d[10];

BookDetails input;
BookDetails output;

int number_customer = 0;
int const MAX = 100;
char temp[40];

int main()
{
	char opt;

	cout << "************************"<<endl;
	cout << "Welcome to book store!!!"<<endl;
	cout << "************************"<<endl;
    cout <<"1. Read a customer."<<endl;
    cout <<"4. Print details of customer."<<endl;
    cout <<"Select one of the options: ";
    cin >>opt;

    switch (opt){

    case '1':  	Read_a_customer();
    	break;
    case '4':	Print_details_of_a_customer();
    	break;
    default: cout <<"You have enter error input..The program will exit..!!";
    break;
    }
	return 0;

}

int Read_a_customer()
{

char opt;

	cout<<endl;
	for(int i=0;i<1;i++)
	{

        cin.ignore();
        cout	<<"\nPlease enter the customer ID: ";
        cin.getline(Array_c[i+1].T.ID,40);
        cout <<"Hit Enter..!!";

        cin.ignore();
        cout	<<"\nPlease enter the customer name : ";
        cin.getline(Array_c[i].T.name,40);
        cout <<"Hit Enter..!!";

        cin.ignore();
        cout	<<"\nPlease enter the customer address : ";
        cin.getline(Array_c[i].T.address,40);
        cout <<"Hit Enter..!!";

        cout <<"Do you want to borrow one book?(y/n)?";
        cin >> opt;

        if (opt!='y' || opt!='Y')
        {
        	cout<<endl;
        	cout <<"The customer with ID: "<< Array_c[i].T.ID << " was added"<<endl;
        			return main();

        }
        else
        	  if (opt!='n' || opt!='N'){
        		  cout <<"Error argument. you will now return into main menu..memory location reset. Nothing to save..!!!"<<endl;
        		  Array_c[i] = failure_c;
        		  Array_d[i] = failure_d;

        		  return main();

        	  }
        	  else

        cin.ignore();
        cout << "\nPlease enter the title of the book: ";
        cin.getline(d.F.title,40);
        cout <<"Hit Enter..!!";

        cin.ignore();
        cout << "\nPlease enter the author of the book: ";
        cin.getline(d.F.author,40);
        cout <<"Hit Enter..!!";

        cin.ignore();
        cout << "\nPlease enter the ISBN of the book: ";
        cin.getline(d.F.ISBN,40);
        cout <<"Hit Enter..!!";

        cin.ignore();
        cout << "\nPlease enter the date that you borrow the book? (30-12-2012): ";
        cin.getline(d.F.date,40);

		strcpy(temp,Array_c[i].T.ID);
	}
    cout << "\nThe customer with ID: "<< c.T.ID << " was added. Customer also booked 1 book with ISBN: "<<d.F.ISBN<<" on "<< d.F.date<<" date";
    cout <<"------"<<endl;


	for(int i=0;i<3;i++)
    		if (strcmp(Array_c[i].T.name,Array_c[i+1].T.name)>0)				//place the names with alphabetic order..
    		{
    			strcpy(temp,Array_c[i+1].T.name);
    			strcpy(Array_c[i+1].T.name,Array_c[i].T.name);
    			strcpy(Array_c[i+1].T.name,temp);
    		}

    cout <<endl;
	return main();
}

int Print_details_of_a_customer()
{
	const int max=100;


	cout << "***************************"<<endl;
	cout << "Print Details Of A Customer"<<endl;
	cout << "***************************"<<endl;

	cin.ignore();
	cout <<"Please enter the ID of a customer: ";
	cin.getline(c.T.ID,40);


	for(int i=0;i<max;i++)
	{

			if (strcmp(Array_c[i].T.ID,Array_c[i+1].T.ID)>0){
		cout  <<"The customer with ID: "<<c.T.ID<< " has the following details:"<<endl;
		cout  <<"Name: "<<Array_c[i].T.name<<endl;
		cout  <<"Address: "<<Array_c[i].T.address<<endl;
        cout  <<"---------"<<endl;
			}
				else
					cout <<"There isn't any customer with this ID: "<<c.T.ID<<endl;}
		return main();
}




Is This A Good Question/Topic? 0
  • +

Replies To: C++ problem book_store

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,462
  • Joined: 25-December 09

Re: C++ problem book_store

Posted 15 May 2012 - 09:57 AM

First in C++ you can't call the main() function from any function.
int Read_a_customer()
{

	return main(); // Not allowed.
}


Next stop using all the global variables, they are an extremely bad habit and you should stop now, before you go any further. You should learn to properly pass the parameters to and from your functions. You may want to study the tutorials contained in my signature, they should help explain functions.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 poolet  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 14-May 12

Re: C++ problem book_store

Posted 15 May 2012 - 10:42 AM

View Postjimblumberg, on 15 May 2012 - 09:57 AM, said:

First in C++ you can't call the main() function from any function.
int Read_a_customer()
{

	return main(); // Not allowed.
}


Next stop using all the global variables, they are an extremely bad habit and you should stop now, before you go any further. You should learn to properly pass the parameters to and from your functions. You may want to study the tutorials contained in my signature, they should help explain functions.

Jim


Thanks for helping..

I heard a lot to say that... it's not allowed to return main() yes but could somebody explain me, since is not allowed why
we don't get run-time error or syntax or something anyway??

at the end unfortunately I try to pass the parameters throw functions without result it's a mess... a lot of weird error messages without any point... for sure it's my bad.. compiler do what ever u say, but unfortunately until now thats my knowledge
at the end how I can return into main menu since I want the user after each function is finished to go back into int main() without
of returning main() ???

Thank you again for your reply.!!!
Regards!!
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,462
  • Joined: 25-December 09

Re: C++ problem book_store

Posted 15 May 2012 - 11:21 AM

Quote

we don't get run-time error or syntax or something anyway??

My compiler issues an error message when you try to call main() from a function.

Quote

main.cpp||In function ‘int Read_a_customer()’:|
main.cpp|160|error: ISO C++ forbids taking address of function ‘::main’ [-pedantic]


What compiler are you using?

Quote

a lot of weird error messages without any point

All warnings and errors issued by the compiler have points. They tell you exactly what is wrong with your program, and will also tell you exactly on what line it detects the error. For example in the above error message the error is occurring on line 160 and describes the actual error. The trick is learning to understand your error messages, which will come with experience.

Quote

at the end how I can return into main menu since I want the user after each function is finished to go back into int main() without
of returning main() ???

When you return from a function you return to the line following the function call. So if you are calling the function from main you will return to main when the program encounters a return statement.

Jim

This post has been edited by jimblumberg: 15 May 2012 - 11:22 AM

Was This Post Helpful? 0
  • +
  • -

#5 poolet  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 14-May 12

Re: C++ problem book_store

Posted 15 May 2012 - 11:40 AM

[quote name='jimblumberg' date='15 May 2012 - 11:21 AM' timestamp='1337106061' post='1623466']

Quote

we don't get run-time error or syntax or something anyway??

My compiler issues an error message when you try to call main() from a function.

Quote

main.cpp||In function ‘int Read_a_customer()’:|
main.cpp|160|error: ISO C++ forbids taking address of function ‘::main’ [-pedantic]


What compiler are you using?


I am using eclipse on ubuntu 10.4 and honestly i don't get any type of this message...!!! it just run normally..!!!
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,462
  • Joined: 25-December 09

Re: C++ problem book_store

Posted 15 May 2012 - 11:42 AM

You need to tell your compiler to generate warnings. How are you compiling your program? Are you using an IDE (which one) or the command line?


Jim
Was This Post Helpful? 1
  • +
  • -

#7 poolet  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 14-May 12

Re: C++ problem book_store

Posted 15 May 2012 - 11:57 AM

IDE indigo, command line for some reason isn't works even for a hello project... The truth is that i get tired to install g++ & gcc libraries on linux i don't know if that's a problem...
Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: C++ problem book_store

Posted 15 May 2012 - 03:07 PM

Hi, is this a book shop or a book lending library?

View Postpoolet, on 15 May 2012 - 07:29 PM, said:

So.. The first function called Read_a_customer() the informations of customer : id, name, address
should be added array in the array in the correct place according to alphabetic order of customer name.. also it will check if the customer
is already added...


I would be better to have the function do less - just get a customer's data. Another function could add the data to an array another check whether the customer exists already. Asking if a book is to be borrowed, and borrowing a book can be performed in other functions. Perhaps better names/descriptions would be get_customer() and add_customer() etc.


View Postpoolet, on 15 May 2012 - 07:29 PM, said:

my serious problem is that I can't save an array since the customer_Details must be a structure and instead of it will be added the id,name and address.. This is my first problem,


You can save the data for each structure/record on a line.
Was This Post Helpful? 1
  • +
  • -

#9 poolet  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 14-May 12

Re: C++ problem book_store

Posted 15 May 2012 - 09:10 PM

View Post#define, on 15 May 2012 - 03:07 PM, said:

Hi, is this a book shop or a book lending library?

View Postpoolet, on 15 May 2012 - 07:29 PM, said:

So.. The first function called Read_a_customer() the informations of customer : id, name, address
should be added array in the array in the correct place according to alphabetic order of customer name.. also it will check if the customer
is already added...


I would be better to have the function do less - just get a customer's data. Another function could add the data to an array another check whether the customer exists already. Asking if a book is to be borrowed, and borrowing a book can be performed in other functions. Perhaps better names/descriptions would be get_customer() and add_customer() etc.


View Postpoolet, on 15 May 2012 - 07:29 PM, said:

my serious problem is that I can't save an array since the customer_Details must be a structure and instead of it will be added the id,name and address.. This is my first problem,


You can save the data for each structure/record on a line.



Hello thanks for reply.. Your right..!! is not a book store is a book lending library..!!!Ok my serious problem is how to save 1 structure into [0] position of an array. Finally with this program I don;t save the customers into any type of array, and thats the reason that compiler just cout the last import of the of the last customer it's rewriter the older with the new one.... Thanks an example of saving each structure into array it will serious help me... Thank you in advance!!!
Was This Post Helpful? 0
  • +
  • -

#10 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: C++ problem book_store

Posted 15 May 2012 - 09:58 PM

Compile with ggc with the "-pedantic" option

Quote

I am using eclipse on ubuntu 10.4 and honestly i don't get any type of this message...!!! it just run normally..!!!

Calling main anywhere else in a program results in undefined behavior

This post has been edited by jjl: 15 May 2012 - 09:58 PM

Was This Post Helpful? 1
  • +
  • -

#11 poolet  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 14-May 12

Re: C++ problem book_store

Posted 15 May 2012 - 10:59 PM

View Postjjl, on 15 May 2012 - 09:58 PM, said:

Compile with ggc with the "-pedantic" option

Quote

I am using eclipse on ubuntu 10.4 and honestly i don't get any type of this message...!!! it just run normally..!!!

Calling main anywhere else in a program results in undefined behavior


Thanks jjl I have figure out with a bool variable...!!
Was This Post Helpful? 0
  • +
  • -

#12 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: C++ problem book_store

Posted 16 May 2012 - 07:17 PM

Hi, a thought or two about the design - you want a list of customers and a list of books, using arrays. The id identifies the customer uniquely - ie there are no duplicate id values. The ISBN identifies a book uniquely.


So to connect a book to a customer only the id is needed :

struct Book
{
  char title[40];
  char author[40];
  char ISBN[40];

  char customer_id[40];
};




To store data about books on loan, another possiblity is to create a third structure, which holds the customer id and the book ISBN.

You don't need to bother with lending until you get the customer and book lists up and running.


View Postpoolet, on 16 May 2012 - 07:10 AM, said:

Ok my serious problem is how to save 1 structure into [0] position of an array. Finally with this program I don't save the customers into any type of array, and thats the reason that compiler just cout the last import of the of the last customer it's rewriter the older with the new one.... Thanks an example of saving each structure into array it will serious help me... Thank you in advance!!!


In C++ you can use the = operator to assign/copy a struct variable/object to another. An element of the array is a structure.

customers[number_customers] = customer;




Utilising your code I add data to the array, and pass data to and from functions.

#include <iostream>
using namespace std;

int const MAXCUSTOMERS = 100;

struct Customer
{
  char id[40];
  char name[40];
  char address[40];
};

// get customer data from user
bool get_customer(Customer &customer)
{
  cout << "Enter the customer name : ";
  cin.getline(customer.name, 40);
  return true;
}

// add a customer to the customer list
//   and update number of customers
bool add_customer(Customer customers[], Customer customer, int &number_customers)
{
  customers[number_customers] = customer;
  number_customers++;
  return true;
}

// print the customers in the customer list
void print_customers(Customer customers[], int number_customers)
{
  for(int i=0; i<number_customers; i++)  {
    cout << customers[i].name
         << endl;
  }
}


int main()
{
  Customer customer_list[MAXCUSTOMERS];
  Customer customer_temp;
  int number_customers = 0;

  get_customer(customer_temp);

  add_customer(customer_list, customer_temp, number_customers);

  cout << "Number of customers = " << number_customers << endl;
  print_customers(customer_list, number_customers);

  cin.get();
  return(0);
}


Was This Post Helpful? 1
  • +
  • -

#13 poolet  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 14-May 12

Re: C++ problem book_store

Posted 18 May 2012 - 02:15 PM

Thanks since I am able to finished that and seriously I give up, it;s a mess, what's the purpose of that?? I really don't understand and I am very angry with the person that gave me that project... Whats the meaning to creating loan and reserve / return of any kind of object without to exist in a database?? Seriously is the most silly thing, (may am stupid I don't know.. ) but this isn't have any logic I will try to post you exactly what the project says, I don't know what to say... Anyway for now, if someone wants to check out what I have done until now see below my code:: Special thanks to friend define and for all the users that they help!!! Thanks guys..!!!

Problems::: Isn't save second book in 1 customers
::: Isn't find the id of a second customer if u try to borrow a book
::: Is overwrites the 1 book with the second on
//============================================================================
// Name        :
// Author      : 
// Version     :
// Copyright   : 
// Description : 
//============================================================================

#include<iostream>
#include<cmath>
#include<string>
#include <stdio.h>       //libraries for strcmp on unix/linux
#include <string.h>

using namespace std;

int const MAXCUSTOMERS = 100;
int number_customers = 0;

struct Customer
{
  char id[40];
  char name[40];
  char address[40];
};

struct Book
{
  char title[40];
  char author[40];
  char ISBN[40];
  char date[40];

  char customer_id[40];
};





int borrow_books(Customer customers[], Customer &customer,Book books[], int &number_customers,int &number_books,int &total_books_customer, Book &book_id, bool& return_main);
bool get_customer(Customer customers[], Customer &customer, Book book_list[], Book &books, int& number_customers, int &number_books, bool& return_main);
bool return_num_customers(int &number_customers,bool& return_main);
bool add_customer(Customer customers[], Customer customer, Book book_list[], Book &books,int &number_customers,int &number_books, bool& return_main);
bool print_customers(Customer customers[], Book books[], Book &Books,Book book_list[],int &number_customers,int &number_books, bool& return_main);
bool find_customers(Customer customers[],Customer &customer,Book books[],int &number_customers,int &number_books,bool& return_main);

// get customer data from user
bool get_customer(Customer customers[], Customer &customer, Book book_list[], Book &books, int &number_customers, int &number_books, bool& return_main)
{
	  Customer customer_list[MAXCUSTOMERS];
	  Customer customer_temp;
	  Book book_id;
      int total_books_customer = 0;
      char opt;

for (int i=0; i<1; i++)
{ 	cin.ignore();
    cout << "Enter the customer id : ";
    cin.getline(customer.id, 40);

    if ((strcmp(customer.id,customers[i].id)==0))
    {
    	cout <<"*******************************"<< endl;
    	cout <<"    Error On -> 'Read Customer'"<< endl;
    	cout <<" The customer with id: "<<customer.id<<" exist."<< endl;
    	cout <<"           ** END OF **       "<< endl;
    	 return (0);
    }
    else
    {cout <<    "Hit enter to continue. "<< endl;
    cin.ignore();
    cout << "Enter the customer name : ";
    cin.getline(customer.name, 40);
    cout <<    "Hit enter to continue. "<< endl;
    cin.ignore();
    cout << "Enter the customer address:";
    cin.getline(customer.address,40);
    cout <<    "Hit enter to continue. "<< endl;
    cin.ignore();
    cout << "Hit enter to continue. "<< endl;
    }

    cout << "Do you want to add 1 book on customer with id: "<<customer.id<<" (Y/N?)"<< endl;
    cin >> opt;

    if (opt=='Y')
    {
    	borrow_books(customer_list, customer_temp,book_list,number_customers,number_books,total_books_customer,book_id,return_main);

    }
    else if(opt=='N')
    	{
    	number_books=0;
        	return(0);
    	}
    }
return(1);
    	}
// add a customer to the customer list
//   and update number of customers
bool return_num_customers(int &number_customers,bool& return_main)
{
	 cout << "*********************************"<< endl;
	 cout << "Return the number of customer(s)  "<< endl;
     cout << "Total number of customer(s) = "<<number_customers<< endl;
     cout << "        ** END OF **          "<< endl;
     cout << "*********************************"<< endl;
     return return_main = true;
}

bool add_customer(Customer customers[], Customer customer, Book book_list[], Book &books,int &number_customers,int &number_books, bool& return_main)
{
	for (int i=0; i<number_customers; i++)
	{
	    if ((strcmp(customer.id,customers[i].id)==0)){
	    	  return return_main = true;
	    	  break;
	}}

  customers[number_customers] = customer;
  number_customers++;
  book_list[number_books] = books;
  return return_main = true;
	}
// print the customers in the customer list
bool print_customers(Customer customers[],Customer &customer,Book books[],Book &Books, Book book_list[], int &number_customers,int &number_books,bool& return_main)
{

	cin.ignore();
	cout <<"Enter the customer id: ";
	cin.getline(customer.id,40);
	for(int i=0; i<number_customers;i++){

	if ((strcmp(customer.id,customers[i].id)!=0))
	{
		cout <<"There isn't any customer with this ID:"<<customer.id<<endl;
	}
	else
	    {cout << "The customer was found.."<< endl;
	     cout << "************************"<< endl;

	     cout << "ID: "<<customers[i].id<<endl;
	     cout << "Name: "<<customers[i].name<<endl;
	     cout << "Address: "<<customers[i].address<<endl;
	     if (number_books>=1){ for(int j=0; j<number_books; j++){
	     cout << "Total book(s): "<<number_books <<endl;
	     cout << "Book title: "<<books[j].title<< endl;
	     cout << "ISBN book: "<<books[j].ISBN<< endl;
	     cout << "Date: "<<books[j].date<<endl;
	     cout << "********************************" << endl;
         books[number_books] = Books;
	     }
	  }
	     else
	    	 cout <<"This customer was not added any book(s)."<<endl;
	     	 return return_main = true;

	}

	}
	return return_main = true;
	}

bool find_customers(Customer customers[],Customer &customer,Book books[],int &number_customers,int &number_books,bool& return_main){

cin.ignore();
cout <<"Enter the customer id: ";
cin.getline(customer.id,40);
for(int i=0; i<number_customers;i++){
if ((strcmp(customer.id,customers[i].id)!=0))
{
	cout << "\n*******************************************"<< endl;
	cout << "             Find Customer                 "<< endl;
	cout <<"There isn't any customer with this ID: "<<customer.id<<endl;
    cout << "             ** END OF **                  "<< endl;
    cout << "*******************************************"<< endl;
}
else
    {
     cout << "******************************************"<< endl;
     cout << "             Find Customer                 "<< endl;
     cout << "The customer with id: '"<<customer.id<<"' was found."<< endl;
     cout << "  There are "<<number_customers<<" customer in database."<<endl;
     cout << "             ** END OF **      "<<endl;
     cout << "******************************************";
      	 return return_main = true;
}
}
return return_main = true;
}

int borrow_books(Customer customers[], Customer &customer,Book books[], int &number_customers,int &number_books,int &total_books_customer, Book &book_id, bool& return_main){

	cin.ignore();
    cout <<"Enter the customer id: ";
    cin.getline(customer.id,40);

    for (int i=0; i<number_customers; i++)
    if ((strcmp(customer.id,customers[i].id)!=0))
    {
    	cout <<"There isn't any customer with this ID: "<<customer.id<<endl;
    	cout << "***************************************"<< endl;
    	return(0);
    }
    else
    cout <<"Hit enter to continue. "<< endl;
    cin.ignore();
    cout <<"How many book(s) want to borrow?(<5)";
    cin >>number_books;
    total_books_customer = number_books;
    for (int i=0; i<total_books_customer; i++)
       {
    if (total_books_customer>5) {
        cout <<"Each customer has a maximum number of 5 book(5)"<<endl;
        cout <<"Returning to main.. You can't borrow up to 5 books..."<<endl;

        return return_main = true;
    }
    else


    	   if (number_books>5){
    	        	cout <<"You can't continue you this customer has reach the limit of 5 book(s)"<< endl;
    	        	  return return_main = true;
    	        }
    	   else

    	   	   	   	  cin.ignore();
    	   	   	   	   	   	   	cout << "\nPlease enter the title of the book: ";
    	                        cin.getline(books[i].title,40);                 //TEST
    	                        cout <<"Hit Enter..!!";

    	                        cin.ignore();
    	                        cout << "\nPlease enter the author of the book: ";
    	                        cin.getline(books[i].author,40);
    	                        cout <<"Hit Enter..!!";

    	                        cin.ignore();
    	                        cout << "\nPlease enter the ISBN of the book: ";
    	                        cin.getline(books[i].ISBN,40);
    	                        cout <<"Hit Enter..!!";

    	                        cin.ignore();
    	                        cout << "\nPlease enter the date that you borrow the book? (30-12-2012): ";
    	                        cin.getline(books[i].date,40);

    	                        cout <<"The customer with id: "<<customer.id<<" was added "<<total_books_customer<<" of book(s);"<<endl;
    	                        total_books_customer++;

    	                        return return_main = true;
       }

        return 0;
}
int main()
{

  Customer customer_list[MAXCUSTOMERS];
  Customer customer_temp;
  Book book_id;
  Book books_temp;
  Book book_list[MAXCUSTOMERS];


  int number_books = 0;
  int total_books_customer = 0;
  char option;
  bool return_main = true;

  do {
  cout << "\n";
  cout << "*********************************"<<    endl;
  cout << "    Welcome to book store!!!        "<<    endl;
  cout << "*********************************"<<    endl;
  cout <<"1. Read a customer.                   "<<    endl;
  cout <<"2. Return the number of customers."<<    endl;
  cout <<"3. Find a customer.                "<<    endl;
  cout <<"4. Print details of customer.        "<<    endl;
  cout <<"5. Borrow book(s).                "<<    endl;
  cout <<"6. Reserver book(s).                "<<    endl;
  cout <<"7. Return book.                    "<<    endl;
  cout <<"8. Reports.                        "<<    endl;
  cout <<"9. Calculate and print fines.        "<<    endl;
  cout << "*********************************"<<    endl;
  cout <<"Select one of the options: ";
  cin >> option;

  switch (option)
  {
        case '1':{ get_customer(customer_list, customer_temp,book_list, books_temp,number_customers,number_books,return_main);
                         add_customer(customer_list, customer_temp, book_list,books_temp,number_customers,number_books, return_main);
                         cout << "-> "<<number_customers <<" customer(s) in database." << endl;
                         return_main = true;
                          }break;

  case '2':return_num_customers(number_customers,return_main);return_main = true;
      break;
  case '3': find_customers(customer_list,customer_temp,book_list,number_customers,number_books,return_main);
      break;
  case    '4': print_customers(customer_list,customer_temp,book_list, books_temp, book_list,number_customers,number_books,return_main);return_main = true;
      break;
  case '5':borrow_books(customer_list, customer_temp,book_list,number_customers,number_books,total_books_customer,book_id,return_main);
      break;
  case '6':
      break;
  case '7':
      break;
  case '8':
      break;
  case '9':
      break;
  }
  }
  while (return_main!=false);
     cout <<"Have a nice day"<<endl;
     }


Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: C++ problem book_store

Posted 18 May 2012 - 05:34 PM

Hi, I think you are confusing yourself a little. I'll point out some things.

You could have #include <cstring> here instead.

013   #include <string.h>



Having your data in main helps reduce problems. So this could be moved into main.

018	int number_customers = 0;




In get_customer the customer_list array and the customer_temp record are already held in main. They are passed to get_customer as the first two parameters.

049	bool get_customer(Customer customers[], Customer &customer, 
                          Book book_list[], Book &books, 
                          int &number_customers, int &number_books, 
                          bool& return_main)
050	{
051	      Customer customer_list[MAXCUSTOMERS];
052	      Customer customer_temp;




You could have the function as this :

049	bool get_customer(Customer customers_list[], Customer &customer_temp, 
                          Book book_list[], Book &books, 
                          int &number_customers, int &number_books, 
                          bool& return_main)
050	{
              // following no longer required 
051	      //Customer customer_list[MAXCUSTOMERS];
052	      //Customer customer_temp;




Here the break cannot be reached.

118	              break;




You use cin.ignore a lot, if you use it here you can reduce the usage.

286	  cout <<"Select one of the options: ";
287	  cin >> option;
   	  cin.ignore();





Some functions seem complicated - they could be broken into more sub-functions. The get_customer function doesn't look quite right. The index i is always 0.

062	    if ((strcmp(customer.id, customers[i].id)==0))




Why borrow books in the function? The menu just says 'Read a customer', why make it more complicated?


For get_customer you could pass the Customer list a temporary Customer object and the number of customers. The parameter for the number of customers is referenced with & so that the variable in main is updated.

void my_get_customer(Customer customers_list[],
                     Customer &customer_temp,
                     int &number_customers)




Then get a Customer id from the user.

  char customer_id[40];

  cout << "Enter the customer id : ";
  cin.getline(customer_id, 40);




Check whether the Customer id already exists.

  if(customer_id_exist(customers_list, customer_id, number_customers))
  {
    cout << "A customer with id: "
         << customer_id
         << " exists."
         << endl;
  }





If it doesn't exist add to list.

  else
  {
    strcpy(customer_temp.id, customer_id);

    cout << "Enter the customer name : ";
    cin.getline(customer_temp.name, 40);

    cout << "Enter the customer address:";
    cin.getline(customer_temp.address,40);

    my_add_customer( customers_list, customer_temp,
                     number_customers);
  }
}




To check the id just iterate through the list

bool customer_id_exist(Customer customers_list[],
                       char* customer_id,
                       int number_customers)
{
  for (int i=0; i<number_customers; i++)
  {
    if ( strcmp(customers_list[i].id, customer_id) == 0 )
    {
      return true;
    }
  }

  return false;
}





You could save data to files. If you save id, name and address to separate lines of a customer file it would be simple to load the data into the array using getline.

  for (int i=0; i<number_customers; i++)
  {
    outfile << customers_list[i].id << endl;
    outfile << customers_list[i].name << endl;
    outfile << customers_list[i].address << endl;
  }


Was This Post Helpful? 1
  • +
  • -

#15 poolet  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 14-May 12

Re: C++ problem book_store

Posted 18 May 2012 - 06:20 PM

View Post#define, on 18 May 2012 - 05:34 PM, said:

Hi, I think you are confusing yourself a little. I'll point out some things.
......

Why borrow books in the function? The menu just says 'Read a customer', why make it more complicated?

....
...
..
.
You could save data to files. If you save id, name and address to separate lines of a customer file it would be simple to load the data into the array using getline.

  for (int i=0; i<number_customers; i++)
  {
    outfile << customers_list[i].id << endl;
    outfile << customers_list[i].name << endl;
    outfile << customers_list[i].address << endl;
  }



Thank you very much for your help..I appreciate that..!!!!!!
borrow books is in the function get_customers since the project asked..(I know is wrong..) I should create a recursion function of that
or may set the get_customer() back to int main() and call borrow_books() with boolean for the 1.Read Customer...

if allowed to used files (ifstream/ofstream) believe me I would have had finished the project..!! The bad here is that the books aren't
save somewhere before the program start... For my opinion thats wrong..and a calling 2nd fun from another?? Talking with other classmates of structures they have exactly the same problem...

Thanks a lot my friend!!!!!!!!!!!!!!!!!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2