Search
 
SCRIPT & CODE EXAMPLE
 

C

Answer to storing information in array

#include <stdio.h>

#define MAXS 256

struct book
{
    char bname[20];
    int pages;
    char author[20];
    long price; 
};

int main () {

    struct book books[MAXS] = {{ {0}, 0, {0}, 0 }}; /* initialize all values to zero (null) */
    int nbooks = 0;
    int i = 0;

    printf ("
Enter number of books to store: ");
    scanf("%d%*c",&nbooks);                         /* read nbooks, and consume newline     */

    if (nbooks < 1) {                               /* validate number of books to enter    */
        fprintf (stderr, "error: invalid entry for 'nbooks'
");
        return 1;
    }

    for (i = 0; i < nbooks; i++)                    /* enter values for each book, use      */
    {                                               /* scanf to read each value AND the     */
        printf ("
  book[%2d] name  : ", i + 1);   /* newline character, emptying stdin    */
        scanf ("%[^
]%*c", books[i].bname);        /* after each read.                     */
        printf ("  book[%2d] pages : ", i + 1);
        scanf ("%d%*c", &books[i].pages);
        printf ("  book[%2d] author: ", i + 1);
        scanf ("%[^
]%*c", books[i].author);
        printf ("  book[%2d] price : ", i + 1);
        scanf ("%ld%*c", &books[i].price);
    }

    printf ("

The Books Entered Were:
");       /* output info for each book entered    */
    i = 0;
    while (*books[i].bname)
    {
        printf ("
  Book %-3d "%s"
", i + 1, books[i].bname);
        printf ("    author : %s
", books[i].author);
        printf ("    pages  : %d
", books[i].pages);
        printf ("    price  : %ld
", books[i].price);
        i++;
    }

    printf ("
");

    return 0;
}
Comment

PREVIOUS NEXT
Code Example
C :: how to convert c program in to assembly 8051 online 
C :: C Keyword typedef 
C :: semicolong after for() loop stackoverflow 
C :: User input in struct 
C :: Fibonacci program c pthread 
C :: error: argument 1 range [18446744071562067968, 18446744073709551615] exceeds maximum object size 9223372036854775807 [-werror=alloc-size-larger-than=] 
C :: can torch light bring change in chemical reaction 
C :: winautomation block input not working 
C :: Unix socket I/O primitives 
C :: worst fit program in c 
C :: set all pins as input for loop 
C :: BSTNode root 
C :: compil cywin cgi 
C :: how many archaeologists are there 
C :: converting time in c 
C :: C Why enums are used? 
C :: shortest job first 
C :: How to scale all columns in dataframe in R- Method 2? 
C :: C Create union variables 
C :: lazer codechef 
C :: is 0 true or false 
C :: letter in alphabet or not 
C :: in C char to string 
C :: strncmp 
Dart :: flutter keyboard causes overflow 
Dart :: change color of drawer icon flutter 
Dart :: how to get whatsapp groups in app flutter programmatically 
Dart :: flutter appbar trailing icon 
Dart :: put container in bottom column flutter 
Dart :: flutter await http.get timeout 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =