Ask the user to fill in three structures and then prints them. - C Structure

C examples for Structure:Structure Value

Description

Ask the user to fill in three structures and then prints them.

Demo Code

#include <stdio.h>

struct bookInfo {
    char title[40];
    char author[25];
    float price;/*  w  w  w  .jav a2s .  c om*/
    int pages;
};

int main(){
    struct bookInfo books[3]; // Array of three structure variables

    for (int i = 0; i < 3; i++){
        printf("What is the name of the book #%d?\n", (i+1));
        gets_s(books[i].title);
        puts("Who is the author? ");
        gets_s(books[i].author);
        puts("How much did the book cost? ");
        scanf(" $%f", &books[i].price);
        puts("How many pages in the book? ");
        scanf(" %d", &books[i].pages);
        getchar(); //Clears last newline for next loop
    }

    for (int i = 0; i < 3; i++){
        printf("#%d: %s by %s", (i+1), books[i].title, books[i].author);
        printf("\nIt is %d pages and costs $%.2f", books[i].pages, books[i].price);
        printf("\n\n");
    }

    return(0);
}

Result


Related Tutorials