Thread: Help me ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    22

    Help me ...

    Hello@

    i have written this
    Code:
    #include <stdio.h>
    
    
    struct account_info
    {
            char name;
            char surname;
            int  account;
            int  rest;
    } a;
    
    
    main()
    {
            printf("Give name:");
            scanf("%c\n", &a.name);
    
    
    
            printf("You gave %c\n", a.name);
    }

    i put John, but in the end the programme print Only the 1st letter, J.

    what is the problme?

    THXXXXXXXXXXXXXX

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    'name' is only a single character. You will need an array, and you'll need to use something other than %c to read input. Read the FAQ for how to get input from a user.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    22
    Quote Originally Posted by quzah View Post
    'name' is only a single character. You will need an array, and you'll need to use something other than %c to read input. Read the FAQ for how to get input from a user.


    Quzah.
    you have right..

    I change it char -> char* and then i put %s...

    but now the exit is

    you gave (null)

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Quote Originally Posted by johnybe View Post
    you have right..

    I change it char -> char* and then i put %s...

    but now the exit is

    you gave (null)
    try defining your name variable as

    Code:
    char name[15];
    that defines an array of chars 15 bytes long. plenty of space for a first name

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    22
    Quote Originally Posted by ollie88r View Post
    try defining your name variable as

    Code:
    char name[15];
    that defines an array of chars 15 bytes long. plenty of space for a first name
    Well, you had right!!! Thxxx!!!

    But now i have problem with this. If i want to do the same thing not for the name, but forthe account

    Code:
    #include <stdio.h>
    
    typedef struct account_info
    {
            char* name[15];
            char* surname[15];
            int  account;
            float  rest;
    } a;
    
    
    main()
    {
            a x;
            printf("Give account:");
            scanf("%i", x.account);
    
    
    
            printf("You gave %i \n", x.account);
    }

    i have "segmentation fault" when i try to put 12234.

    ????

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    scanf takes an address, not a value.
    Code:
    scanf("%i", &x.account);
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    22
    thx you!!!!!



    thxxxxxxxxxxxxxxxxxxxxxx

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    {
            char* name[15];
    You have an array of pointers to characters. You don't want that. You want to remove the *, or remove the [15] and allocate memory dynamically. Pick one, not both.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    22
    Quote Originally Posted by quzah View Post
    Code:
    {
            char* name[15];
    You have an array of pointers to characters. You don't want that. You want to remove the *, or remove the [15] and allocate memory dynamically. Pick one, not both.


    Quzah.
    ok...!! thx you tooo!!!!!!!!!!!!

Popular pages Recent additions subscribe to a feed