Thread: Hello need help with struct

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    21

    Hello need help with struct

    Code:
    #include <stdio.h>
    struct testData
    {
    	char name[10];
    	int num;
    };
    
    
    void test(struct testData t);
    
    
    int main()
    {
    	struct testData t;
    	printf("Enter name\n");
    	*t.name = getchar();  // not working
    
    
    	//scanf("%s", t.name); // working perfect 
    
    
    	puts("Enter number\n");
    	scanf("%d", &t.num);
    	printf("Information you entered name is %s and number is %d \n", t.name, t.num);
    	test(t);
    }
    void test(struct testData t)
    {
    	printf("Printing from test\n");
    	printf("Information you entered name is %s and number is %d \n", t.name, t.num);
    	printf("Done\n");
    }
    getchar(); I doesn't compile and not working any advise please ..

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > *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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    21
    Quote Originally Posted by Salem View Post
    > *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.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > b[count].bookName[bN] = getchar();
    Each one of these also needs
    b[count].bookName[bN+1] = '\n';
    to make sure you have a \0 terminated string at all times.

    > void checkBook(struct bookData b)
    Try declaring and defining it as
    void checkBook(struct bookData b[])
    I'll leave you to figure out how to call it as an exercise
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. typedef struct vs struct declaration error in code
    By shaswat in forum C Programming
    Replies: 3
    Last Post: 07-25-2016, 06:45 AM
  2. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  3. 2 problems: Struct array and pointer + struct
    By v1n1c1u5 in forum C Programming
    Replies: 0
    Last Post: 12-13-2009, 05:38 PM
  4. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  5. returning a pointer of a struct of a struct array...
    By myrddinb in forum C Programming
    Replies: 1
    Last Post: 04-13-2004, 06:49 PM

Tags for this Thread