Aj Jadoon
Aj Jadoon

Reputation: 31

Structures in C Language

what is mistake ? i cant find any mistake in my code but it does not take input properly and it also displays some garbage values. Thanks:

int main()
{
    struct book
    {
        char name;
        int price;
        int pages;
    };
    struct book b[5];
    int i;

    for (i = 0; i < 5; i++)
    {
        printf("enter name price pages\n");
        scanf("%c", &b[i].name);
        scanf("%d", &b[i].price);
        scanf("%d", &b[i].pages);
    }
    for (i = 0; i < 5; i++)
        printf("%c %d %d\n", b[i].name, b[i].price, b[i].pages);
    return 0;
}

Upvotes: 0

Views: 136

Answers (5)

wbt11a
wbt11a

Reputation: 818

Since I've been informed to specifically answer the question (sorry, Im new here), the problem lies in the %c picking up whitespace and/or newline characters. The easiest way to prevent this is put a " " in front of it.

#include <stdio.h>

int main()
{
    struct book
    {
        char name; 
        int price;
        int pages;
    };

    struct book b[5];
    int i;
    for(i=0;i<5;i++)
    {
        printf("enter name price pages\n");
        scanf(" %c %d %d",&b[i].name, &b[i].price, &b[i].pages);  //here
        //%c was grabbing whitespace and/or newline causing the problem.
    }
    for(i=0;i<5;i++)
        printf("Name:  %c Price:  %d Pages:  %d\n",b[i].name,b[i].price,b[i].pages);

    return 0;
}

Output:

enter name price pages

a 1 2

enter name price pages

b 3 4

enter name price pages

c 5 6

enter name price pages

d 7 8

enter name price pages

e 9 10

Name: a Price: 1 Pages: 2

Name: b Price: 3 Pages: 4

Name: c Price: 5 Pages: 6

Name: d Price: 7 Pages: 8

Name: e Price: 9 Pages: 10

Upvotes: 1

thrax
thrax

Reputation: 365

try it like this :

scanf(" %c %d %d",&b[i].name, &b[i].price, &b[i].pages);
while(getchar()!='\n');

Upvotes: 0

Jonatan Goebel
Jonatan Goebel

Reputation: 1139

What happens is that the last scanf("%d"), reads the number and let a '\n' at the buffer, so next time you call scanf("%c"), it will read the '\n' in the buffer, and not the new character.

One possible solution to use scanf, is to tell scanf to read everything in the line, and discard it:

    scanf("%c%*[^\n]\n", &b[i].name);
    scanf("%d%*[^\n]\n", &b[i].price);
    scanf("%d%*[^\n]\n", &b[i].pages);

This way, scanf will block until you press enter, and will read the '\n'.

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 753465

Most books have a name that is longer than a single letter.

You need to adjust the structure accordingly:

enum { MAX_BOOKNAMELEN = 32 }; // Probably too short

struct book
{
    char name[MAX_BOOKNAMELEN];
    int price;
    int pages;
};

You also need to adjust the input:

    if (scanf("%31[^\n]", b[i].name) != 1)
        ...report error; do not use this book entry...

The '31' is magicked out of thin air; it is one less than MAX_BOOKNAMELEN. Note that book titles frequently contain spaces; thus %s which skips leading spaces and stops at the first space after one or more non-space characters is not appropriate for reading titles. One way to create the format string is via sprintf() — this will adapt if MAX_BOOKNAMELEN changes size:

char name_fmt[16];
snprintf(name_fmt, sizeof(name_fmt), "%%%d[^\n]", MAX_BOOKNAMELEN-1);

if (scanf(name_fmt, b[i].name) != 1)
    ...error...

A more thorough revision would probably use fgets() and sscanf() or other tools instead of calling scanf() directly.

Upvotes: 3

MOHAMED
MOHAMED

Reputation: 43518

this line

scanf("%c",&b[i].name);

should be

scanf(" %c",&b[i].name);

See How to do scanf for single char in C

Upvotes: 2

Related Questions