Home > Net >  request for member ‘nickname’ in ‘a’, which is of non-class type ‘Author [1000]’
request for member ‘nickname’ in ‘a’, which is of non-class type ‘Author [1000]’

Time:03-21

#include <iostream>

using namespace std;

struct Author {

int id ;
string fullname;
string nickname;
int age ;

};


struct Book {

int id ;
string title;
int year;
float price ;

};


void menu (){

cout << "Add New Author (1) \n" ;
cout << "Display Author List (2)\n " ;
cout << "Add New Book (3) \n" ;
cout << "Display Book List (4) \n" ;
cout << "Edit Author nickname (5) \n" ;
cout << "Quit The Program (0) \n\n" ;

cout << "Please Enter The Number : " ;

}


Author addauthor(Author *v ){

cout << "Enter ID: ";
cin  >> v->id;
cout << "Enter Full Name: ";
cin.ignore();
getline(cin ,v->fullname) ;
cout << "Enter nickname: ";
cin  >> v->nickname;
cout << "Enter Age: ";
cin  >> v->age;

cout << endl ;

return *v ;

}

void displayauthor(Author *v ){

cout << "ID: " << v->id << endl;
cout << "Full Name: " << v->fullname << endl;
cout << "Nickname: " << v->nickname << endl ;
cout << "Age: " << v->age << endl << endl;


}


Book addbook(Book *v ){

cout << "Enter ID: ";
cin  >>  v->id;
cout << "Enter Title: ";
cin.ignore();
getline(cin ,v->title);
cout << "Enter Year: ";
cin  >>  v->year;
cout << "Enter Price : RM";
cin  >>  v->price;

cout << endl ;

return *v ;

}

void displaybook(Book *v ){

cout << "ID: " << v->id << endl;
cout << "Title: " << v->title << endl;
cout << "Year: " << v->year << endl ;
cout << "Price: RM" << v->price << endl << endl;

}




int main()
{

int n ;
Author a[1000];
Book b[1000];
int i, j , num1 , num2 ,id ;
string sr, rp;





 do {   

 menu();
 cin >> n ;

 {

if (n==1) {

cout << "Please Enter the number of Author (Max 100): ";
cin >> num1;

cout << endl ;
for(i = 0 ;i < num1 ; i   )

{
    
 a[i]= addauthor(&a[i]);

    }
}

 else if (n==2) {


 cout << "\nAuthor Information." << endl << endl;
 
 for(i = 0 ;i < num1 ; i   )

{
    
 displayauthor(&a[i]);

    }
}

else if (n==3) {

cout << "Please Enter the number of Book (Max 100): ";
cin >> num2;

cout << endl ;
for( i = 0 ;i < num2 ; i   )

{
    
 b[i]= addbook(&b[i]);

}
}

else if (n==4) {

 cout << "\nBook Information." << endl << endl;

 for(i = 0 ;i < num2 ; i   )

{
    
 displaybook(&b[i]);
 
}

}

else if (n==5) {

cout << "Please Enter Old Nickname : " << endl ;
cin >> sr ;
cout << "Please Enter Author New Nickname : " << endl ;
cin >> rp ;

for (int j = 0 ; j < num1 ; j  ){

     if (a.nickname[j] == sr){

         cout  << " The nickname has been Found." ;
         a.nickname[j]=rp;
         
     }
  
  else {
      
      cout << " Nickname not found ." ;
  }
     
 
   }
}
  

else {

cout << "The number you've enter is not available"<< endl << endl ;

}

}

}while (n !=0);



cout << "Program end" ;

return 0;
}

I tried to replace a nickname for the selected author which happen on this part

else if (n==5) {

cout << "Please Enter Old Nickname : " << endl ;
cin >> sr ;
cout << "Please Enter Author New Nickname : " << endl ;
cin >> rp ;

for (int j = 0 ; j < num1 ; j  ){

     if (a.nickname[j] == sr){

         cout  << " The nickname has been Found." ;
         a.nickname[j]=rp;
         
     }
  
  else {
      
      cout << " Nickname not found ." ;
  }
     
 
   }
}

So basically, the user tried to enter a new nickname for the Author by having the user to enter an old one and the error is "error: request for member ‘nickname’ in ‘a’, which is of non-class type ‘Author [1000]’" which happened in this 2 lines :

  1. if (a.nickname[j] == sr){
    
  2. a.nickname[j]=rp;
    

CodePudding user response:

The problem you're facing is just the order of operations you're performing.

a is an array of Authors, nickname is the member variable you're trying to access. you first need to decide which array entry you want to select by using a[j], then afterwards access the member variable 'nickname' by appending .nickname. this results in the statement a[j].nickname instead of a.nickname[j]

  •  Tags:  
  • c
  • Related