Example of using struct, vectors and functions with C++
This example, written in c++, explains how to use the struct and struct vectors to create a simple program for managing books. The program shows the user a context menu, and depending on your choice of user invokes some functions to insert a new book, to print a list of books, or to print only the books of a given category.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// Struct
typedef struct Book
{
char isbn[16];
char title[50];
char category[30];
float price;
};
// Function prototypes
void print_menu();
int load_books(Book bl[]);
void print_books_list(Book bl[], int c);
int add_book(Book bl[], int c);
void print_books_list_category(Book bl[], int c);
void close();
int main ()
{
// A vector containing the struct of type Book
Book books_list[100];
// Stores the position within the vector
int pos = 0;
// Load some books
pos = load_books(books_list);
// User's choice
int choice = -1;
printf("Program for managing books\n");
do
{
print_menu();
printf("\nEnter the choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
pos = add_book(books_list, pos);
break;
case 2:
print_books_list(books_list, pos);
break;
case 3:
print_books_list_category(books_list, pos);
break;
case 0:
close();
break;
}
}while(choice!=0);
return 0;
}
// Print on screen the menu to show the user
void print_menu()
{
printf("\n");
printf("1. Add a book\n");
printf("2. List of all books\n");
printf("3. List of books by a particular category\n");
printf("0. Exit\n");
}
// Load some book in the vector
int load_books(Book bl[])
{
int n = 0;
strcpy(bl[n].isbn, "9788820499198");
strcpy(bl[n].title, "Elements of programming with C");
strcpy(bl[n].category, "Programming");
bl[n].price = 25.80;
n++;
strcpy(bl[n].isbn, "9788838641527");
strcpy(bl[n].title, "C++ The complete guide");
strcpy(bl[n].category, "Programming");
bl[n].price = 45.96;
n++;
strcpy(bl[n].isbn, "9788871921969");
strcpy(bl[n].title, "MySQL");
strcpy(bl[n].category, "Database");
bl[n].price = 55.00;
n++;
strcpy(bl[n].isbn, "9788883315145");
strcpy(bl[n].title, "Database design");
strcpy(bl[n].category, "Database");
bl[n].price = 55.00;
n++;
strcpy(bl[n].isbn, "9788838641817");
strcpy(bl[n].title, "Java 2");
strcpy(bl[n].category, "Programming");
bl[n].price = 50.61;
n++;
// Return the current position
return n;
}
// Ask the user to enter the data of the book
int add_book(Book bl[], int p)
{
// Cleans the screen
system("cls");
printf("Enter ISBN\n");
scanf("%s", bl[p].isbn);
printf("Enter Title\n");
scanf("%s", bl[p].title);
printf("Enter Price\n");
scanf("%f", &bl[p].price);
printf("Enter Category\n");
scanf("%s", bl[p].category);
// Increase the value of p
p++;
// Return the current position
return p;
}
// Print out the list of all books
void print_books_list(Book bl[], int p)
{
// Cleans the screen
system("cls");
if(p==0)
{
printf("\nThere are no books to display");
}
else
{
printf("\nList of all books");
printf("\nISBN\t\tPRICE\tCATEGORY\tTITLE");
printf("\n--------------------------------------------------------------");
int i;
for(i = 0; i <p; i++)
{
printf("\n");
printf("%s", bl[i].isbn);
printf("\t");
printf("%.2f", bl[i].price);
printf("\t");
printf("%s", bl[i].category);
printf("\t");
printf("%s", bl[i].title);
}
}
printf("\n");
}
// Print out the list of all books by category
void print_books_list_category(Book bl[], int p)
{
// Cleans the screen
system("cls");
printf("\nEnter the category: ");
char cat[20];
scanf("%s", cat);
printf("\nList of all books in the selected category: %s", cat);
printf("\nISBN\t\tPRICE\tCATEGORY\tTITLE");
printf("\n--------------------------------------------------------------");
int i;
for(i = 0; i <p; i++)
{
int find = strcmp(bl[i].category, cat);
if(find==0)
{
printf("\n");
printf("%s", bl[i].isbn);
printf("\t");
printf("%.2f", bl[i].price);
printf("\t");
printf("%s", bl[i].category);
printf("\t");
printf("%s", bl[i].title);
}
}
printf("\n");
}
// Clears the screen and exits the program
void close()
{
system("cls");
exit(0);
}
An example of the running program:
Program for managing books
1. Add a book
2. List of all books
3. List of books by a particular category
0. Exit
Enter the choice: 2
List of all books
ISBN PRICE CATEGORY TITLE
--------------------------------------------------------------
9788820499198 25.80 Programming Elements of programming with C
9788838641527 45.96 Programming C++ The complete guide
9788871921969 55.00 Database MySQL
9788883315145 55.00 Database Database design
9788838641817 50.61 Programming Java 2
1. Add a book
2. List of all books
3. List of books by a particular category
0. Exit
Enter the choice: 3
Enter the category: Database
List of all books in the selected category: Database
ISBN PRICE CATEGORY TITLE
--------------------------------------------------------------
9788871921969 55.00 Database MySQL
9788883315145 55.00 Database Database design
1. Add a book
2. List of all books
3. List of books by a particular category
0. Exit
Enter the choice: 0