Skip to content

Instantly share code, notes, and snippets.

@tudorconstantin
Created March 21, 2015 19:15
Show Gist options
  • Select an option

  • Save tudorconstantin/29661ffa02b20e9f568c to your computer and use it in GitHub Desktop.

Select an option

Save tudorconstantin/29661ffa02b20e9f568c to your computer and use it in GitHub Desktop.
Simple struct to hold book info. Read 3 books and print them
/*This heade file defines a structure for information
about a book*/
#include <stdio.h>
struct bookInfo{
char title[40];
char author[25];
float price;
int pages;
};
/* This program gets the book info structure
by including chapter27-A and asks the users
to fill in the three structures and then prints them*/
int main(void)
{
int ctr;
struct bookInfo books[3]; //Array of there structures variabeles
// Gets information about each book from the user;
for(ctr = 0; ctr < 3; ctr++)
{
printf("What is the name of the book #%d?\n", (ctr+1));
gets(books[ctr].title);
puts("Who is the author ?");
gets(books[ctr].author);
puts("How much did the book cost?");
scanf(" %f", &books[ctr].price);
puts("How many pages in the book?");
scanf(" %d", &books[ctr].pages);
getchar(); //clears last new line for the next loop
}
//Print a heder line and loop through ad print the info
printf("Here is the colection of books\n");
for(ctr = 0; ctr < 3; ctr++)
{
printf(" #%d, %s by %s", (ctr+1), books[ctr].title, books[ctr].author);
printf("\n It is %d pages and costs $ %.2f", books[ctr].pages, books[ctr].price);
printf("\n\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment