#include <iostream>

using namespace std;

const int SIZE=30;
const int bSIZE=3;
void Initialize(struct Book*[], int size);

struct Book
{string title[SIZE]; string author[SIZE];};

int main()
{
	int choice1, choice2;
	string title;
	string author;

	Book *arrbook[bSIZE];
	
	Initialize(arrbook,bSIZE);

	system ("pause");
	return 0;

}
void Initialize(struct Book *[],int size)
{
	for(int i=0; i<size; i++)
	{
		arrbook[i]= new Book;
		arrbook[i]={"None","None"};	
	}
		

}
When I run this program, it say "arrbook undeclared identifier"
I don't know why. Can you fix my code? what is wrong with passing structure code?

Recommended Answers

All 7 Replies

Line 7 should be

void Initialize(Book[], int);

And line 26 should be

void Initialize(Book arrbook[],int size)

I must use this function.
void Initialize(struct Book *[],int size)
because this function is requirement.

is there anyway to pass the structure to function?

Here is how you should do it:

#include <iostream>
#include <string>

using namespace std;

const int SIZE = 30;
const int bSIZE = 3;


struct Book{
  string title; 
  string author;
  Book(string t, string a):title(t),author(a) {}
};


/*'struct Book *arrbook[]' is the same thing as 'struct Book **arrbook' */
void Initialize(struct Book *arrbook[], int size){
  for(int i = 0; i < size; i++)
    arrbook[i] = new Book("None", "None");
}

int main(){
  int choice1, choice2;
  string title;
  string author;

  /* This declares an array of pointers to Book structures */
  Book *arrbook[bSIZE];
	
  Initialize(arrbook, bSIZE);

  system ("pause");
  return 0;
}



/* Here is a different way to do the same thing but it looks better */

void Initialize_NEW(struct Book **arrbook, int size){
  arrbook = NULL;
  arrbook = new Book*[size];
  for(int i = 0; i < size; i++)
    arrbook[i] = new Book("None", "None");
}

int main_NEW(){
  int choice1, choice2;
  string title;
  string author;

  Book **arrbook;
	
  Initialize(arrbook, bSIZE);

  system ("pause");
  return 0;
}

-Sam

Thank you very much. Can you help me one more question?

#include <iostream>
#include <string>

using namespace std;

const int SIZE=30;
const int bSIZE=3;
void Initialize(struct Book*arrbook [], int size);

struct Book{
	string title; string author; 
	Book(string t, string a):title(t),author(a) {}};
//{char title[SIZE], author[SIZE];};

int main()
{
	int choice1, choice2;
	string title[SIZE]; string author[SIZE];
	

	
	Book *arrbook[bSIZE];

	Initialize(arrbook,bSIZE);
	
	
	
	do
	{
	cout<< "Enter 1 to View, or 2 to Modify, or 3 to Quit: ";
	cin >> choice1;

	
	switch (choice1)
	{
		case 1:
                           for (int i=0; i<3; i++)
			{
				cout<<"Title "<< (i+1) << " is :"<< arrbook[i]-> title <<endl;
				cout<<"Author "<< (i+1) << " is :"<< arrbook[i]-> author<< endl;

			}			
			}			


			break;

               case 2:
			
			
			cout<< "Enter which book to modify: "<<endl;
			cin>> choice2;
			
			if(choice2 = 1)
			{
				cout<< "Enter the title: ";
				cin>>title[0];
				arrbook[0]->title= title[0];
				
				
				cout<< "Enter the author: ";
				cin>>author[0];
				arrbook[0]->author= author[0];
			}

			else if (choice2 = 2)
			{ 
				cout<< "Enter the title: ";
				cin>>title[1];
				arrbook[1]->title= title[1];

				
				cout<< "Enter the author: ";
				cin>>author[1];
				arrbook[1]->author= author[1];
				

			}

			else if (choice2 = 3)

			{ 
				cout<< "Enter the title: ";
				cin>>title[2];
				arrbook[2]->title= title[2];
				
				cout<< "Enter the author: ";
				cin>>author[2];
				arrbook[2]->author= author[2];
				

			}			break;
			

		case 3:

			break;

		default: 

			cout<< "The valid choices are 1 through 3. Run the program again and select one of those."<<endl;

	}
	}while(choice1 != 3);

	
	
	

	system ("pause");
	return 0;

}

void Initialize(struct Book*arrbook[],int size)
{
	for(int i=0; i<size; i++)
	{
		
		
		arrbook[i]= new Book("None","None");
			
	}
		

}

This is my code. The passing structure to function is woring now, but this code has a problem. When I edit the title and author, it won't let me change it. What code should I change??

I solved it. Thanks. I've learnd a lot of things.

#include <iostream>
#include <string>

using namespace std;

const int SIZE=30;
const int bSIZE=3;
void Initialize(struct Book*arrbook [], int size);

struct Book{
  string title; string author; 
  Book(string t, string a):title(t),author(a) {}
};


int main(){
  int choice1, choice2;
  string title, author;
	
  Book *arrbook[bSIZE];
  Initialize(arrbook,bSIZE);

  do{
    cout<< "Enter 1 to View, or 2 to Modify, or 3 to Quit: ";
    cin >> choice1;

    switch (choice1){
      case 1:
        for (int i = 0; i < 3; i++){
	  cout << "Title " << (i+1) << " is :" << arrbook[i]-> title << endl;
	  cout << "Author " << (i+1) << " is :" << arrbook[i]-> author << endl;
        }			
  //} BAD BRACKET		
        break;

      case 2:
        cout << "Enter which book to modify: " << endl;
	cin >> choice2;
        if(choice2 > 3)
          cout << "Must be a book 1 through 3" << endl;
	else{
	  cout << "Enter the title: ";
	  cin >> title;
	  arrbook[choice2 - 1]->title = title; //same as *(arrbook[choice2 - 1]).title = title;
	  cout<< "Enter the author: ";
	  cin >> author;
	  arrbook[choice2 - 1]->author = author; //same as *(arrbook[choice2 - 1]).author = author;
        }
        break;

      case 3:
        break;

      default: 
        cout << "The valid choices are 1 through 3. Run the program again and select one of those." << endl;
    }
  }while(choice1 != 3);
  //system ("pause");
  return(0);
}

void Initialize(struct Book*arrbook[], int size){
  for(int i = 0; i < size; i++)
    arrbook[i]= new Book("None","None");
}

Output:

Enter 1 to View, or 2 to Modify, or 3 to Quit: 2
Enter which book to modify:
1
Enter the title: ThisBook
Enter the author: Sam
Enter 1 to View, or 2 to Modify, or 3 to Quit: 2
Enter which book to modify:
2
Enter the title: MyBook
Enter the author: Sammy
Enter 1 to View, or 2 to Modify, or 3 to Quit: 2
Enter which book to modify:
3
Enter the title: GoodBook
Enter the author: Samuel
Enter 1 to View, or 2 to Modify, or 3 to Quit: 1
Title 1 is :ThisBook
Author 1 is :Sam
Title 2 is :MyBook
Author 2 is :Sammy
Title 3 is :GoodBook
Author 3 is :Samuel
Enter 1 to View, or 2 to Modify, or 3 to Quit: 3

Note that with the cin >> author and cin >> title, you cannot use spaces. I recommend that you use cin.getline instead and when assigning the book names and titles you use the string() function. Otherwise if you use spaces you will get errors. On another note I still don't know why you are using the double pointer array, it is not necessary. Book *arrbook[bSIZE] is a two dimensional array, you can access the member values in this in more than one way:
*(arrbook[choice]).title
arrbook[choice]->title
arrbook[choice][0].title

For you purposes just making an array of Books (i.e. Book arrBook[bSize]) would have been sufficient.

-Sam

becuase my teacher gave the double pointer pointer array, I can't change it. Plus, I used

cin.getlline. Thank you for your reply.

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.