In the below code I create an object based off of the books structure, and to have it hold multiple "books" I set is an array (the object that is defined/initiated, that is). However, whenever I went to test my knowledge of pointers (the practice helps) and attempted to make a pointer that points to the created object, it gives me the error:
C:\Users\Justin\Desktop\Project\wassuip\main.cpp|18|error: incompatible types in assignment of 'books' to 'books* [4]'|*
May I ask, is this because the object book_arr[] is already considered a pointer as it's an array? Thanks (new to C++ and just want to verify).
#include <iostream>
#include <vector>
#include <sstream>
#define NUM 4
using namespace std;
struct books {
float price;
string name;
int rating;
} book_arr[NUM];
int main()
{
books *ptr[NUM];
ptr = &book_arr[NUM];
string str;
for(int i = 0; i < NUM; i++){
cout << "Enter book name: " << endl;
cin >> ptr[i]->name;
cout << "Enter book price: " << endl;
cin >> str;
stringstream(str) << ptr[i]->price;
cout << "Enter book rating: " << endl;
cin >> str;
stringstream(str) << ptr[i]->rating;
}
return 0;
}
*NEW CODE AFTER ANSWERS (NO ERRORS) *
#include <iostream>
#include <vector>
#include <sstream>
#define NUM 4
using namespace std;
/* structures */
struct books {
float price;
string name;
int rating;
} book[NUM];
/* prototypes */
void printbooks(books book[NUM]);
int main()
{
string str;
books *ptr = book;
for(int i = 0; i < NUM; i++){
cout << "Enter book name: " << endl;
cin >> ptr[i].name;
cout << "Enter book price: " << endl;
cin >> str;
stringstream(str) << ptr[i].price;
cout << "Enter book rating: " << endl;
cin >> str;
stringstream(str) << ptr[i].rating;
}
return 0;
}
void printbooks(books book[NUM]){
for(int i = 0; i < NUM; i++){
cout << "Title: \t" << book[i].name << endl;
cout << "Price: \t$" << book[i].price << endl;
cout << "Racing: \t" << book[i].rating << endl;
}
}