Simple Program Book Entry using Structure Variable in C++ Programming
On this page (5sections)
Simple Program Book Entry Using structure Variable
#include<iostream.h>
#include<stdio.h>
struct books {
char name[20], author[20];
} a[50];
int main() {
int i, n;
cout << "No Of Books[less than 50]:";
cin>>n;
cout << "Enter the book details\n";
cout << "----------------------\n";
for (i = 0; i < n; i++) {
cout << "Details of Book No " << i + 1 << "\n";
cout << "Book Name :";
cin >> a[i].name;
cout << "Book Author :";
cin >> a[i].author;
cout << "----------------------\n";
}
cout << "================================================\n";
cout << " S.No\t| Book Name\t|author\n";
cout << "=====================================================";
for (i = 0; i < n; i++) {
cout << "\n " << i + 1 << "\t|" << a[i].name << "\t| " << a[i].author;
}
cout << "\n=================================================";
return 0;
}
Sample Output
No Of Books[less than 50]:2
Enter the book details
----------------------
Details of Book No 1
Book Name :Programming
Book Author :Dromy
Details of Book No 2
Book Name :C
Book Author :Byron
=======================================================
S.No | Book Name |author
=======================================================
1 |Programming | Dromy
2 |C | Byron
=======================================================
How It Works
Here is a step-by-step walkthrough of how the C++ program for Simple Program Book Entry using Structure Variable in C++ Programming runs, line by line:
struct books {— performs part of the program’s logic.char name[20], author[20];— declares or assigns a value the program uses.} a[50];— performs part of the program’s logic.int main() {— defines a function used by the program.int i, n;— declares or assigns a value the program uses.cout << "No Of Books[less than 50]:";— writes a line of output shown in the sample output.cin>>n;— reads a value entered by the user.cout << "Enter the book details\n";— writes a line of output shown in the sample output.
After running it, compare your console output with the Sample Output above. Try changing the values and re-running the program to see how the result changes — experimenting is the fastest way to understand the logic.
Related Pages
Learn the concept first, then study the code:
- C++ Programs — Browse all C++ Programs.
Frequently Asked Questions
What does this C++ program do?
It is a C++ example program that demonstrates Simple Program Book Entry using Structure Variable, including the complete source code and the expected sample output.
How do I compile and run this C++ program?
Save the code in a `.cpp` file, compile it with `g++ filename.cpp -o program`, then run it with `./program` (or `program.exe` on Windows).
What concepts does this example use?
This example uses loops to iterate over data, arrays and reading user input, illustrating a common pattern in C++ programming.