Originally Posted by
Salem
> *t.name = getchar(); // not working
Why are you trying to read a string using a single call to getchar()
Try writing something like
t.name[0] = getchar();
But the result isn't a string you can print with %s.
Code:
#include <stdio.h>
#define QTY 320
#define BOOK_NAME_LENGTH 21
#define AUTHOR_NAME_LENGTH 26
#define PUBLISH_NAME_LENGTH 21
#define BOOKS 1
struct bookData
{
int booksQty;
char bookName[BOOK_NAME_LENGTH];
char authorName[AUTHOR_NAME_LENGTH];
char publishName[PUBLISH_NAME_LENGTH];
};
void checkBook(struct bookData b);
/**********************************************************************************************************************/
int main()
{
int i,count = 0;
struct bookData b[BOOKS];
printf("Add %d books \n", BOOKS);
for (i = 0; i < BOOKS; i++)
{
printf("Book #%d \n", i+1);
printf("Please enter the book name size limit is %d\n", BOOK_NAME_LENGTH);
for (int bN = 0; bN < BOOK_NAME_LENGTH-1; bN++)
{
b[count].bookName[bN] = getchar();
if (b[count].bookName[bN] == '\n')
break;
}
printf("Please enter the author name size limit is %d\n", AUTHOR_NAME_LENGTH);
for (int aN = 0; aN < AUTHOR_NAME_LENGTH - 1; aN++)
{
b[count].authorName[aN] = getchar();
if (b[count].authorName[aN] == '\n')
break;
}
printf("Please enter the publisher name size limit is %d\n", PUBLISH_NAME_LENGTH);
for (int pN = 0; pN < PUBLISH_NAME_LENGTH - 1; pN++)
{
b[count].publishName[pN] = getchar();
if (b[count].publishName[pN] == '\n')
break;
}
printf("Please enter quantity of book\n");
//do
//{
scanf("%d", &b[count].booksQty);
//if (b[count].booksQty < 0)
//puts("Quantity of books can't be negative number please reenter\n");
//} while (b[count].booksQty < 0);
_flushall();
}
checkBook(*b);
}
void checkBook(struct bookData b)
{
for (int i = 0; i < BOOKS; i++)
{
printf("Book #%d: Book name %s, Author name %s, Publisher name %s, Book quantity %d, \n", i, b[i].bookName, b[i].authorName, b[i].publishName, b[i].booksQty);
}
}
cant print normally in checkBook function maybe I still can't get it how to transfer structs to functions sorry thanks for help.