0

invalid types ‘int[int]’ for array subscript

#include <iostream> using namespace std; class Book{ // declaring private class data members private: char book_name[50]; float book_price; int bookid; char author[20]; bool available; int year; public: // declaring constructor Book() { cout<<"Default constructor is called"<<endl; name = "student"; age = 12; } void input (){ int n=4; cout<<"input 4 books"; for (int i=0; i<4 ; i++){ cout<<"Enter details for book "<<i<<":\n"; cout<<"\n book_name"; cin>>book_name[i]; } } // print function to print the class data members value void print() { for(int i=0;i<4;i++){ cout<<"Details for book "<<i<<":\n"; cout<<"\nbook_name:"<<book_name[i]; cout<<"\nbookid:"<<bookid[i]; cout<<"\nbook_price:"<<book_price[i]; cout<<"\nauthor:"<<author[i]; cout<<"\navailable:"<<available[i]; cout<<"\nyear:"<<year[i]; } } }; int main() { // creating object of class using default constructor Book obj; // printing class data members obj.print(); return 0; } along with the other errors i want to solve the int[int] error how may i?

2nd Oct 2022, 1:10 PM
Khaled
Khaled - avatar
4 Answers
+ 1
Your book price ,bookid year also need to make array type
2nd Oct 2022, 1:42 PM
ASR
ASR - avatar
+ 2
Your variables bookid, book_price, year are inter, float, int variables (single). Not arrays. So bookid[i], book_price[i], year[i] are invalid. Errors.
2nd Oct 2022, 1:29 PM
Jayakrishna🇮🇳
Jayakrishna🇮🇳 - avatar
+ 2
Declare name, age also.. If you want to add 4 array of values to all variables then make array types and character array to character 2d array as book_name[4][50]; Otherwise, if it need just a single values then don't need loop. don't use subscript for variables for character: cin >> book_name; cout<<book_name; and other types cout << year ; //example.
2nd Oct 2022, 2:20 PM
Jayakrishna🇮🇳
Jayakrishna🇮🇳 - avatar
0
so how may i solve the problem
2nd Oct 2022, 1:30 PM
Khaled
Khaled - avatar