#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Lightbulb String Problem (Problem related to white_space in Output)


    Hi,
    I am a student and for programming practice I am making a small C program for my library.

    I had given a particular code to my every book. For e.g. I have given maths a code 001 and to science 002.

    Now I need admin to register those books with my program.

    For e.g He should first ask to enter code, suppose user enters 003 than than compiler will ask for the name of book to enter at that code.
    Like this user will update all his books to numeric codes in virtual library.
    Now in future if user need to know whether which book is registered on 005 code.
    Than he will enter 005 and compiler will give in output the book's name which he had previously entered.

    Now You have understand what kind of program I am trying to make.
    So my main question is how can I do that.?

    If I use char c[10]; than I can only store name.
    If I use int i[10]; than I can only enter numbers.

    So how can i do that..??
    Please Help me.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Code:
    struct {
      char name[128];
      int number;
    } catalog[10];
    Would an array of struct help?
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Thumbs up


    Thanks for the help...
    Going to implement it...
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    I had made a simple program..

    #include<stdio.h>
    #include<conio.h>
    struct shelf
    { char member_name[20],book_name1[30],book_name2[30],book_name3[30],book_name4[30],book_name5[30];
    } mem[10];
    void main()
    {
    clrscr();
    printf("Enter member name: "); scanf("%s",mem1.member_name);
    printf("Enter member name: "); scanf("%s",mem2.member_name);
    printf("Enter member name: "); scanf("%s",mem3.member_name);
    printf("\n\nMember 3: %s",mem3.member_name);
    getch();
    }
    But it says there are 3 errors
    1) Undefined symbol mem1
    2) Undefined symbol mem2
    3) Undefined symbol mem3
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    It's exactly what it says: mem1, mem2, and mem3 are not defined. You never declared them. They don't exist.

    Now, there is a mem[1], mem[2], and mem[3]. Along with the first struct in that array, mem[0].
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Exclamation Problem with whitespace


    Thanks for the help...

    Now There's one more Problem...

    Below is my Program, Whenever I write name or books name with space it creates problem for me..
    For example in 1st book I wrote "Within Light" than It will not allow me to enter 2nd Book's name and cursor will directly move to 3rd Book's place.
    If I enter two white_spaces than it skips next to scans..

    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    struct shelf
    {
    int member_id;
    char member_name[25],book_name1[50],book_name2[50],book_name3[50],book_name4[50],book_name5[50]; } mem[20];
    void main()
    {
    clrscr();
    int i=0;
    printf("Enter Member's ID: ");
    scanf("%d",&mem[i].member_id);
    printf("Enter Name: "); scanf("%s",&mem[i].member_name);
    printf("1)Enter Issued Book: "); scanf("%s",&mem[i].book_name1);
    printf("2)Enter Issued Book: "); scanf("%s",&mem[i].book_name2);
    printf("3)Enter Issued Book: "); scanf("%s",&mem[i].book_name3);
    printf("4)Enter Issued Book: "); scanf("%s",&mem[i].book_name4);
    printf("5)Enter Issued Book: "); scanf("%s",&mem[i].book_name5);
    i++;
    getch();
    }
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Exclamation Need Help with gets function..


    I am also having a similar problem...

    This is my program

    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    struct shelf
    {
    int member_id;
    char member_name[25],book_name1[50],book_name2[50],book_name3[50],book_name4[50],book_name5[50];
    }
    mem[20];

    void main()
    {
    clrscr();
    int i=0;
    printf("Enter Member's ID: ");
    scanf("%d",mem[i].member_id);

    printf("Enter Name: "); gets(mem[i].member_name);
    printf("1)Enter Issued Book: "); gets(mem[i].book_name1);
    printf("2)Enter Issued Book: "); gets(mem[i].book_name2);
    printf("3)Enter Issued Book: "); gets(mem[i].book_name3);
    printf("4)Enter Issued Book: "); gets(mem[i].book_name4);
    printf("5)Enter Issued Book: "); gets(mem[i].book_name5);
    }
    i++;
    getch();
    }

    Now It does not allow me to type the Member's name and if I use scanf function than I have some problems with white_space...

    Please Help me with gets function because I don't want to use scanf function...
    Still any Help will be appreciated...

    Thanks..
  14. #8
  15. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    The first thing is to forget you ever knew anything about gets() and never use it again

    Next, input is tricky stuff, especially when you start mixing and matching input styles.
    Ideally, you should read each line into a buffer using say fgets(buff,sizeof(buff),stdin), then pick it apart with say sscanf, or any other string functions you like.

    But if you're stuck with scanf, then perhaps this
    Code:
    #include<stdio.h>
    #include<string.h>
    struct shelf {
      int member_id;
      char member_name[25], book_name1[50], book_name2[50], book_name3[50], book_name4[50],
          book_name5[50];
    } mem[20];
    
    int main()
    {
      int i = 0;
      printf("Enter Member's ID: ");
      scanf("%d", &mem[i].member_id);
      printf("Enter Name: ");
      scanf(" %24[^\n]%*c", mem[i].member_name);
      printf("1)Enter Issued Book: ");
      scanf(" %49[^\n]%*c", mem[i].book_name1);
      printf("2)Enter Issued Book: ");
      scanf(" %49[^\n]%*c", mem[i].book_name2);
      printf("3)Enter Issued Book: ");
      scanf(" %49[^\n]%*c", mem[i].book_name3);
      printf("4)Enter Issued Book: ");
      scanf(" %49[^\n]%*c", mem[i].book_name4);
      printf("5)Enter Issued Book: ");
      scanf(" %49[^\n]%*c", mem[i].book_name5);
      
      printf("Id=%d, name=%s\n", mem[i].member_id, mem[i].member_name);
      printf("Books: \"%s\", \"%s\", \"%s\", \"%s\", \"%s\"\n",
             mem[i].book_name1, mem[i].book_name2,
             mem[i].book_name3, mem[i].book_name4,
             mem[i].book_name5 );
      return 0;
    }
    Figuring out what " %49[^\n]%*c" does is an exercise for you (read the manual).
    If you get stuck, at least post what you think it does (to show you've at least tried).

    Oh, example
    Code:
    $ gcc bar.c
    $ ./a.out 
    Enter Member's ID: 123
    Enter Name: fred flintstone
    1)Enter Issued Book: born free
    2)Enter Issued Book: die hard
    3)Enter Issued Book: rabbit stew
    4)Enter Issued Book: war and peace
    5)Enter Issued Book:      infinity
    Id=123, name=fred flintstone
    Books: "born free", "die hard", "rabbit stew", "war and peace", "infinity"
    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
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Post


    I checked the program. Its working perfect.
    But I don't understand all those things you written in scanf..
    So can you provide me with a link containing some more details about it..??

    I am just a very new beginner in C and I don't have any reference book right now. All I do is study on various web sites and I have never gone through such a thing. So please provide me a web link which has some details about what you have written.

    Thank you... :)
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Originally Posted by altaf114
    ... But I don't understand all those things you written in scanf..
    So can you provide me with a link containing some more details about it..??

    ... I don't have any reference book right now. ... So please provide me a web link which has some details about what you have written.
    To answer your request, here is the man page for scanf:
    http://linux.die.net/man/3/sscanf

    On UNIX, the original OS for C (the two practically created each other), documentation for the C functions was provided by the manual utility called man, so the entire body of documentation was called the man pages. There appears to be several tens of thousands of man page collection on the Web, so there's never any reason to say that one does not have the documentation.

    To do this search yourself, go to Google.com and enter the search phrase man page scanf and you will get about 81,900 hits; I just chose to give you the first one.
    Last edited by dwise1_aol; February 16th, 2013 at 01:59 PM.

IMN logo majestic logo threadwatch logo seochat tools logo