C++program that declares a structure to store id,pages and price of a book.Display the least and costly book details | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 3

C++program that declares a structure to store id,pages and price of a book.Display the least and costly book details

Write a c++ program that declares a structure to store id, pages and price of a book. It defines an array of structures to store the records of five books. It inputs the record of five books and display the record of most costly and least costly book.

22nd Jan 2021, 8:36 AM
Arslanzafar Zafar
Arslanzafar Zafar - avatar
3 Answers
+ 5
Arslanzafar Zafar , before we are going to help you, please present your attempt here. if you haven't done a try by yourself so far, please do so, and post it here. please also provide a proper task description with sample of input and output. thanks!
22nd Jan 2021, 10:15 AM
Lothar
Lothar - avatar
+ 1
Lothar #include<iostream> using namespace std; struct book { int BookID; float price; int pages; }; int main() { book b[5]; float least=0,costly=0; cout<<"Enter bookID:"<<endl; for(int i=0;i<5;i++) { cin>>b[i].BookID; } for(int i=1;i<=5;i++) { cout<<"Enter the pages of "<<i<<" book:"<<endl; cin>>b[i].pages; cout<<"Enter the price of "<<i<<" book:"<<endl; cin>>b[i].price; } cin.ignore(); for(int i=1;i<=5;i++) { if(b[i].price>b[i+1].price) { least=b[i+1].price; } else least=b[i].price; } cin.ignore(); for(int i=1;i<=5;i++) { if(b[i].price<b[i+1].price) { costly=b[i+1].price; } else costly=b[i].price; } cout<<"Least book is: "<<least<<endl; cout<<"Costly book is :"<<costly; }
22nd Jan 2021, 10:20 AM
Arslanzafar Zafar
Arslanzafar Zafar - avatar
0
Lothar its a logical error but i don't know which one
22nd Jan 2021, 10:21 AM
Arslanzafar Zafar
Arslanzafar Zafar - avatar