Unable to input plz find error in my c++ code


I am inputing book name, no of pages and price using structures but in 2nd time input some problem is occuring please rectify and help me...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>

using namespace std;
struct book {
    string title[100];
    int pages;
    float price;
};
int main()
{
    int i;
    book b[3],*ptr;
    ptr=b;
    for(i=0;i<3;i++)
    {
        cout<<"Enter the book's name for "<<i+1<<": ";
        getline(cin,ptr->title[i]);
        cout<<"Enter the Price of the book: ";
        cin>>ptr->price;
        cout<<"Enter the number of pages in the book: ";
        cin>>ptr->pages;
        ptr++;
    }
    ptr=b;
    for(i=0;i<3;i++)
    {
        cout<<"The "<<i+1<<"- book's name : "<<ptr->title<<endl;
        cout<<"The Price of the book: "<<ptr->price<<endl;
        cout<<"The number of pages in the book: "<<ptr->pages<<endl;
        ptr++;
        cout<<"-------------------------------------------"<<endl;
    }
    
    
}
Why do you have an array of title in your structure?

Since you only have single instances of pages and price wouldn't a single instance of title make more sense?

You also have an array of your book why not use that array instead of the pointer?

Last edited on
Topic archived. No new replies allowed.