Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Beginning to learn C,

Options
  • 08-03-2005 2:27am
    #1
    Closed Accounts Posts: 83 ✭✭


    Started teaching myself C,

    For some reason this program bellow won't work properly wondering can anyone please figure out what the problem might be

    I've been trying various different ways around it for about 3 hours to know avail. thanks


    <<<

    #include <stdio.h>
    #include <string.h>

    main()
    {

    char name1;
    int yearob;
    int hse;

    printf("Please type in your First and Second Names...\n");
    scanf("%s", &name1);

    printf("Please type in the year you were born in...\n");
    scanf("%d", &yearob);

    printf("Please type in your home number...\n");
    scanf("%d", &hse);


    printf("Thank You For Your Time!");
    printf("we have recorded some of your,\n");
    printf("details they are as follows \n\n\n");
    printf("NAME \n\t\t %s \n", name1);
    printf("YEAR OF BIRTH \n\t\t %d \n", yearob);
    printf("HOUSE NUMBER \n\t\t %d", hse);

    printf("\n\n\n\t\t\tGoodbye!!");
    }

    >>>>>

    it has various different Sample out puts but none are right and end up in errors requesting that i close dos.

    Any help would be brilliant.

    Thanks

    neD


Comments

  • Registered Users Posts: 4,276 ✭✭✭damnyanks


    change name1 to a char array?


  • Registered Users Posts: 5,836 ✭✭✭Vokes


    scanf("%s", &name1);

    ....and you don't need the ampersand.


  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    After changing name1 to a char array and dropping the ampersand as suggested by previous posts, you need to change the scanf() call for the name prompt.

    According to 'man scanf' on my machine, the 's' identifier
    Matches a sequence of non-white-space characters;
    the next pointer must be a pointer to char, and the array must be large enough to accept all the sequence and the terminating NUL character. The input string stops at white space or at the maximum field width, whichever occurs first.
    So, entering a first name and a surname will not result in name1 having both, you'll need a second variable for the surname.


  • Registered Users Posts: 1,268 ✭✭✭Zapho


    If you are trying to store a string, and the user types in a space, then the only the first part of the string up to the space character is stored.So if you try to store a first name and a last name into "name1" then only the first name will be stored. Use
    gets(name1) instead. Google it if it doesn't work properly for you!


Advertisement