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

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2

    Input with space[strings]


    hi, i have a problem with my inputs, whenever i input a name with space in the part asking for the FIRST NAME,it will jump off into asking LAST NAME. it skips the part where it asks for a middle initial. so how am i gonna fix the problem? how can i make it to accept characters with space?,below is the code:

    #include <stdio.h>
    #include <conio.h>

    main()
    {
    char fn[25], mn[25], ln[25], adr[25], dob[25], corz[25];
    int idnum;
    printf("PERSONAL INFORMATION DATA");
    printf("\n\n\nFIRST NAME: ");
    scanf("%s", &fn[25]);


    printf("MIDDLE INITIAL: ");
    scanf("%s", &mn[25]);


    printf("LAST NAME: ");
    scanf("%s", &ln[25]);


    printf("DATE OF BIRTH");
    scanf("%s", &dob[25]);


    printf("ADDRESS: ");
    scanf("%s", &adr[25]);



    printf("ID NUMBER: ");
    scanf("%d", &idnum);


    printf("COURSE: ");
    scanf("%s", &corz[25]);


    printf("PERSONAL INFORMATION DATA");
    printf("\n\nNAME: %s %s. %s", fn[25], mn[25], ln[25]);
    printf("\n\nDATE OF BIRTH: %s\tADDRESS: %s", dob[25], adr[25]);
    printf("\n\nID NUMBER: %d\tCOURSE: %s", idnum, corz[25]);
    getch();
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    > printf("\n\n\nFIRST NAME: ");
    > scanf("%s", &fn[25]);
    You point to an array by specifying the start subscript, not the size in the declaration.

    So perhaps
    scanf("%s", &fn[0]);

    which is more conveniently (and usually) written as
    scanf("%s", fn);

    Oh, and next time, use [code][/code] tags around your code, to preserve indentation and formatting.
    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
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2
    Originally Posted by salem
    > printf("\n\n\nFIRST NAME: ");
    > scanf("%s", &fn[25]);
    You point to an array by specifying the start subscript, not the size in the declaration.

    So perhaps
    scanf("%s", &fn[0]);

    which is more conveniently (and usually) written as
    scanf("%s", fn);

    Oh, and next time, use [code][/code] tags around your code, to preserve indentation and formatting.

    thank you =) and sorry about that. i do not know how to use it. ill get to it to learn how.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    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..
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    149
    Rep Power
    36
    gets is bad mmkay.

    use fgets with stdin for stream:

    http://www.cplusplus.com/reference/cstdio/fgets/

IMN logo majestic logo threadwatch logo seochat tools logo